Jump to content

Php As Background Script


Recommended Posts

Anyone successfully run a php script in the background?


I've tried various configurations using exec() and shell_exec() without success. I can run a command, such as 'ls', but cannot seem to get the trick of running a php script.


I've tried to mimic a cron job that runs fine. This is the cron statement


>php -q /home/myaccount/public_html/cgi-bin/backup_dbs.php


such as


>shell_exec("php -q /home/myaccount/public_html/cgi-bin/backup_dbs.php");
shell_exec("/usr/bin/php  /home/myaccount/public_html/cgi-bin/backup_dbs.php");
shell_exec("/usr/bin/php ./home/myaccount/public_html/cgi-bin/backup_dbs.php");


or variations such as


>exec("wget [url="http://myaccount.com/cgi-bin/backup_dbs.php&quot%3b%29;"]http://myaccount.com/cgi-bin/backup_dbs.php");[/url]


Permission is set to 755 and like I said the script runs fine as a cron job.

Link to comment
Share on other sites

Why do you need to run it in the background? Looks like a backup script, just let cron handle it.

I'm using that script simply to test if I can get a script to run in the background (since I know the backup script runs.) My goal is to modify a mailing list script I have. TCH has limits on the number of emails per time period. It is easy enough to put the script to sleep for, say, five minutes between two batches, but then the page appears to hang. If I could fork the send routine off to run in the background, then the page could finish loading.

Link to comment
Share on other sites

In *nix you run a process in the background by adding an ampersand at the end of the command. I cannot guarantee that it will work on the TCH servers configuration. You would have to ask the help desk.

Yesterday, I did try adding the ampersand and also combinations of redirecting to dev/null, but that wasn't the problem. The problem is that the script wasn't running at all. I wasn't concerned at that moment about background, just wanted to test exec() or shell_exec() to fork a process.


I began to think there might have been something weird going on with my backup script - it runs fine as cron, but maybe there is something in there that is not compatible as a command script (I didn't write this script.) So I wrote the simplest, tiny script to send a test email to me. And it works both with and without the ampersand and output redirection. (An ampersand and output redirection will be important in the actual application where the script will take some time to run.)


In case someone is following along, all of these worked:

>shell_exec("/usr/bin/php /home/myaccount/public_html/cgi-bin/send-mail-fork.php 2> /dev/null &");
shell_exec("/usr/bin/php /home/wscp/public_html/cgi-bin/send-mail-fork.php");
exec("/usr/bin/php /home/myaccount/public_html/cgi-bin/send-mail-fork.php 2> /dev/null &");

Thanks for your time. Now I'll have to see if I can do something a little more useful :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...