Restarting cron PHP script

Restarting cron PHP script

Postby pagestep » Wed Jan 11, 2012 1:37 pm

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.
pagestep
 
Posts: 13
Joined: Thu Sep 11, 2008 1:19 pm

Re: Restarting cron PHP script

Postby Ray » Thu Jan 12, 2012 4:06 pm

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.
Ray
Ray
 
Posts: 20
Joined: Sun Nov 08, 2009 9:25 pm

Re: Restarting cron PHP script

Postby pagestep » Thu Jan 12, 2012 5:44 pm

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.
pagestep
 
Posts: 13
Joined: Thu Sep 11, 2008 1:19 pm


Return to PHP

Who is online

Users browsing this forum: No registered users and 0 guests

cron