Note that there are some explanatory texts on larger screens.

plurals
  1. POphp long running process with 'at' acting very strangely
    text
    copied!<p>Firstly, I am far from a Linux expert so that might be the issue here, but anyway, to the problem:</p> <p>I followed what is written here : <a href="http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html" rel="noreferrer">http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html</a></p> <p>to launch a long-running PHP process. This works flawlessly in my MAMP config on my Mac. However once I deployed it to our VPS I got some really weird results.</p> <p>So first I do a simple test, using an SSH connection:</p> <pre><code>echo '/usr/local/php53/bin/php -d memory_limit=512M -q /home/user/www/Update/Update.php;' | at now + 2minutes </code></pre> <p>The result:</p> <pre><code>warning: commands will be executed using /bin/sh job 2300 at 2012-04-29 19:24 </code></pre> <p>and indeed, 2 minutes later the php script is executed. So far so good.</p> <p>Next I try the following approach:</p> <p>in my browser I open:</p> <pre><code>www.myserver.com/Update/LaunchUpdates.php </code></pre> <p>this php script contains the line:</p> <pre><code>exec("echo '/usr/local/php53/bin/php -d memory_limit=512M -q /home/user/www/Update/Update.php;' | at now + 2minutes"); </code></pre> <p>What happens is the following: I check with at -l the state and I see:</p> <pre><code>job 2304 at 2012-04-29 19:32 </code></pre> <p>Then I wait 2 minutes and run at -l again. I expect to see an empty result but instead I get:</p> <pre><code>job 2305 at 2012-04-29 19:34 </code></pre> <p>and 2 minutes later I get</p> <pre><code>job 2306 at 2012-04-29 19:36 </code></pre> <p>I haven't got the slightest idea of what is going on there. The php script is not executed and the job seems to reschedule itself 2 minutes later. And this goes on and on until i atrm the job.</p> <p>Does anyone know what might be going on? </p> <p>Some more info:</p> <pre><code>cat /etc/*-release Gentoo Base System version 1.6.14 </code></pre> <p>Some more details. Here is the content of the at job when it is scheduled: (at -c [ID])</p> <pre><code>#!/bin/sh # atrun uid=1002 gid=100 # mail user 1 umask 33 SERVER_SIGNATURE=\&lt;address\&gt;Apache/2.2.20\ \(Unix\)\ mod_ssl/2.2.20\ OpenSSL/0.9.8o\ Server\ at\ xxx.yyyyy.com\ Port\ 80\&lt;/address\&gt;" "; export SERVER_SIGNATURE HTTP_USER_AGENT=Mozilla/5.0\ \(Macintosh\;\ Intel\ Mac\ OS\ X\ 10_7_3\)\ AppleWebKit/534.55.3\ \(KHTML,\ like\ Gecko\)\ Version/5.1.5\ Safari/534.55.3; export HTTP_USER_AGENT HTTP_HOST=xxx.yyyyy.com; export HTTP_HOST SERVER_PORT=80; export SERVER_PORT DOCUMENT_ROOT=/home/user/www; export DOCUMENT_ROOT SCRIPT_FILENAME=/home/user/www/Update/LaunchUpdates.php; export SCRIPT_FILENAME REQUEST_URI=/Update/LaunchUpdates.php; export REQUEST_URI SCRIPT_NAME=/Update/LaunchUpdates.php; export SCRIPT_NAME HTTP_CONNECTION=keep-alive; export HTTP_CONNECTION REMOTE_PORT=36291; export REMOTE_PORT PATH=/bin:/usr/bin; export PATH PWD=/home/user/www/Update; export PWD SERVER_ADMIN=webmaster@abcdef.com; export SERVER_ADMIN REDIRECT_STATUS=200; export REDIRECT_STATUS HTTP_ACCEPT_LANGUAGE=en-us; export HTTP_ACCEPT_LANGUAGE HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml\;q=0.9,\*/\*\;q=0.8; export HTTP_ACCEPT REMOTE_ADDR=83.101.41.41; export REMOTE_ADDR SHLVL=764; export SHLVL SERVER_NAME=xxx.yyyyy.com; export SERVER_NAME SERVER_SOFTWARE=Apache/2.2.20\ \(Unix\)\ mod_ssl/2.2.20\ OpenSSL/0.9.8o; export SERVER_SOFTWARE QUERY_STRING=; export QUERY_STRING SERVER_ADDR=1.2.3.4; export SERVER_ADDR GATEWAY_INTERFACE=CGI/1.1; export GATEWAY_INTERFACE SERVER_PROTOCOL=HTTP/1.1; export SERVER_PROTOCOL HTTP_ACCEPT_ENCODING=gzip,\ deflate; export HTTP_ACCEPT_ENCODING REQUEST_METHOD=GET; export REQUEST_METHOD cd /home/user/www/Update || { echo 'Execution directory inaccessible' &gt;&amp;2 exit 1 } /usr/local/php53/bin/php -d memory_limit=512M -q /home/user/www/Update/Update.php; </code></pre> <p>When waiting for the job to reschedule after 2 minutes I get the new job's contents and it is identical except for:</p> <p>SHLVL=764 that has become SHLVL=765</p> <p>More info!</p> <p>As a user suggested I tried using nohup instead of at. So what I did was the following:</p> <p>Generate the command to be run by nohup in a .sh file (with execute permissions). and then do exec('nohup .....')</p> <p>I also added a check in LaunchUpdates to make sure it is not called again before the nohup batch has done running (I basically rm the .sh file and the end of its batch, and in LaunchUpdates I check for the existence of that file).</p> <p>So in short.</p> <p>batchProcess.sh contains:</p> <pre><code>/usr/local/php53/bin/php -d memory_limit=512M -q /home/user/www/Update/Update.php; rm /home/user/batchProcess.sh </code></pre> <p>my LaunchUpdates php code contains:</p> <pre><code>$batchFile = "/home/user/batchProcess.sh"; if (file_exists($batchFile)) { echo 'Process still running. Try again later!'; exit; } exec('nohup /home/user/batchProcess.sh &gt; ~/process.out 2&gt; ~/process.err &lt; /dev/null &amp;'); </code></pre> <p>No what happens:</p> <p>I comment out the exec line in my php script so the file does not get executed but generated. I test the file by hand by logging in with ssh, change to user "user" and run:</p> <pre><code>nohup /home/user/batchProcess.sh &gt; ~/process.out 2&gt; ~/process.err &lt; /dev/null &amp; </code></pre> <p>all works fine (and the .sh file is deleted at the end)!</p> <p>Next I uncomment the exec line and rerun the php script. process.out contains:</p> <pre><code>Process still running. Try again later! </code></pre> <p>This means that it IS executing the base-script again and not the exec statement??? I am COMPLETELY lost here! Since on both accounts I run the same bash script there can be no error as to what commands get executed.</p> <p>Should I start digging in the apache logs?</p> <p>This was supposed to take little time, boy was I wrong ....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload