Invoking background php process

Ok, I am getting frustrated! I have been trying to get this silly thing to work for the last few hours and I can’t seem to get it working. We currently have a newsletter process that sends emails out to our newsletter list of about 2500. At one point it became so large that the php script would timeout, so I converted it to a “job” which I would logon and manually invoke from the command-line. My wife wants to be able to trigger the newsletter herself from the website, so I wanted to create a page that let her trigger the process to run in the background. Seems simply enough.

However, each time I try to get the thing to work from the website the “background” php process seems to get killed as soon as the parent page is completed. I have tried lots of different ways of invoking it; but the net result i the same. It works from the command-line, but I can’t get it to work when triggered from a web page.

This website is hosted on a unix box. Here is my little test “driver page” which I was trying to run a php script that would email her about bad categories on her website. Normally this guy is triggered from a Cron job (which also works fine).

echo "Before the shell ...<br/>";

$runCommand = "";
$pid = shell_exec("nohup $runCommand > /dev/null & echo $! &");

echo "Here is the pid: ";
echo $pid;
echo "<br/>I hope this works";

What happens is that I get the PID for the new process, but as soon as this page ends and I check the executing jobs I see nothing and the script has not been executed. The script in this case “” strarts with a #!/usr/local/bin/php -q before the script starts which enables it to run as a script (like when it is scheduled from Cron).

Anyone have any idea what I am doing wrong? I have tries shell_exec, exec and so forth. Launching php with a filename, an executable script (like here), fully qualifying the paths, etc. I have tried piping the results to a file and the funny thing was that this script seemed to run over and over again – not sure what was going down there. :)

I am in no way experienced with PHP processes, least of all on a *nix box, but I think a vital piece of information would be: does this script continue running, i.e. does it give back the PID instantly?

When that page runs it reports a new PID – I suspect this is a different PID from the process that is running the current PHP request, but I am not 100% of that face. It appears once my web request is handled any spawned processes immediate get killed as soon as that page has completed its rendering. (?)

Is it possible that my web hosting company have put something in place to ensure that any PHP script is not allowed to spawn processes in what I believe would be the parent process? Just a thought here because I am certainly not doing anything different than what is documented in the PHP manual and others have posted in various examples.

Unless someone really has a good idea I think I am going to implement a simple queue and have a Cron process handle the processing using an arbitrary timespan for the polling. The idea is that once they click the button I will fire a request onto a queue table and then my PHP process that is scheduled through cron will see that request, mark it “inflight” and then being processing the request. :-?

Sponsor our Newsletter | Privacy Policy | Terms of Service