1

Originally I had a cronjob that executed a php file every day. Apparently the php file was to heavy and the cronjob was not executing it.

I opted for creating a Daemon that sleeps for 24hrs and works perfectly well, except that I believe that the server kills this process after a certain time due to its inactivity. I have a DEAMON that runs every 5 minutes and this one never stops, but the one that waits for 24 hrs always stops working.

My plan is to create a cronjob that checks if the process is running, if not to execute it:

exec("ps auxwww|grep _DAEMON.php|grep -v grep", $output);
if(empty($output)){
   exec("php -f _DAEMON.php");
}

For some reason this is not working, exec("ps auxwww|grep _DAEMON.php|grep -v grep", $output) does detect perfectly well if the script is running or not, so exec("php -f _DAEMON.php") is the one that doesn't work.

I tried to print_r $status from exec("php -f _DAEMON.php",$status); and this is what I got:

Array
(
    [0] => X-Powered-By: PHP/5.2.17
    [1] => Content-type: text/html
    [2] => 
)

I tried exec("php-cli -f _DAEMON.php") and worked perfectly well, but since the file I am calling is a daemon, php-cli makes it part of my original file (some kind of include() I believe) and freezes my original file, it does create a separate new process for the daemon, but the original file loads forever.

Any ideas? thanks!

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
  • `_DAEMON.php` is different from `_RANDOMSD.php`. So what is the result of executing `exec("php -f _DAEMON.php");` ? – Raptor Jul 06 '13 at 04:07
  • so sorry typo. I just fixed it – multimediaxp Jul 06 '13 at 04:10
  • 1
    See this: http://stackoverflow.com/questions/566248/cant-execute-php-script-using-php-exec – Raptor Jul 06 '13 at 04:12
  • Thanks @ShivanRaptor this worked, now the problem is that whatever is executed in php-cli becomes part of the original script, and since my second script is a daemon it freezes. :( – multimediaxp Jul 06 '13 at 04:26
  • @ShivanRaptor please look at the bottom of my question, I added your solution and the problem I got into, thanks! – multimediaxp Jul 06 '13 at 04:32
  • the exec function will probably freeze the current script until _DAEMON.php is finished. Have you `error_reporting(E_ALL);` somewhere to get more errors feedbacks ? – WiMantis Jul 06 '13 at 04:55
  • Yes @WiMantis, no errors, as you say it simply freezes the current script until _DAEMON.php is finished, problem is it will never finish. – multimediaxp Jul 06 '13 at 05:33
  • @WiMantis, I decided to create a new question with my new problem as this one might get too long. http://stackoverflow.com/questions/17500071/detect-when-a-script-delays-and-allow-it-to-skip-or-continue-in-php Thanks! – multimediaxp Jul 06 '13 at 05:42
  • I'll keep commenting here for now since I think things are getting more complicated than they should be lol :P A server killing process due to inactivity, is that really possible with a daemon ? maybe the daemon crashes at some point ? you could also try to cut the long 24h sleep in little naps ^^ lets say you divide it by 24, you do a sleep of 1 hour and increment a counter each time the script pass in the daemon loop, when it reaches 24, execute the daemon code – WiMantis Jul 06 '13 at 06:07

0 Answers0