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).
<?php
echo "Before the shell ...<br/>";
$runCommand = "badcats.inc";
$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 “badcats.inc” 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.