Page 1 of 1

Restarting cron PHP script

PostPosted: Wed Jan 11, 2012 1:37 pm
by pagestep
I'm working on a PHP script that will be run daily via cron. My concern is that the PHP execution time limits may be reached, so am looking for the best possible way to re-start the PHP script from within itself. I was thinking of using exec(), but not sure if this is the right way to go about it, or if it's even possible. I.e.
Code: Select all
<?php
if ($restart) {
  // cron command is: php -q /home/myaccount/cron/script.php
  exec('php -q /home/myaccount/cron/script.php');
  exit;
}
else {
  // rest of script
}
?>

Suggestions?

Also, I've seen extensions used for exec() and am not sure of how these could be used to track issues:
Code: Select all
<?php
exec('nohup php -q /home/myaccount/cron/script.php > script.out 2> script.err < /dev/null &');
?>

Questions:
- Is the "nohup" needed?
- Should any/all of the "-q" and/or "< /dev/null" and/or trailing "&" be included?
- Should script.out and script.err be prefaced with the full path (i.e. /home/myaccount/cron/script.out)?

Thanks in advance for any help/suggestions.

Re: Restarting cron PHP script

PostPosted: Thu Jan 12, 2012 4:06 pm
by Ray
Normally vulnerable functions like 'exec' will be disabled in servers and so it can't be used in PHP scripts. Due to this, the options like 'nohup' , '<' , '>' and its related options won't work correctly. But 'php -q' options can be used without any problem. If you have any further queries on this, please open a ticket to support.

Re: Restarting cron PHP script

PostPosted: Thu Jan 12, 2012 5:44 pm
by pagestep
Ray wrote:Normally vulnerable functions like 'exec' will be disabled in servers and so it can't be used in PHP scripts. Due to this, the options like 'nohup' , '<' , '>' and its related options won't work correctly. But 'php -q' options can be used without any problem. If you have any further queries on this, please open a ticket to support.

I am already successfully using exec() in a cron PHP script on a CWH shared server. I'm just experimenting with the redirectors to see if they are of value/use.

It seems the nohup is not necessary (the cron "user" is never logged out), and I believe the & is to start a process in the background (which is also unnecessary for anything but daemons).

Thank you for your response, Ray.