Reputation: 1666
I am coding a php script that does some back end stuff and needs to run every 8 hours or so. The script takes a while to execute. For the hell of it, I tried it from my browser and the connection to the server gets reset well before the script terminates. My question is - if I run it directly, ie. php -a file.php as a cron job, are there any internal time constraints on execution? This script may take 2-5 minutes to complete and cannot be interrupted. I've never done this before so I am not sure if php has quirks when running heavy scripts.
Upvotes: 17
Views: 35696
Reputation: 20852
As said before, CLI scripts by default have no time limit.
But I would also like to mention an alternative to your cron job approach:
You can fork a CLI PHP script from a PHP script under webserver control. I have done this many times. It is especially useful if you have a script with long execution time which must be triggered by some website user action (e.g. building a very large archive file and send a download link by email when the file is complete).
I usually fork a CLI script from a webserver PHP script using the popen() function. This allows to nicely transfer parameters to the new script instance like this:
$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
die('Could not open bgrnd process');
}else{
// send params through stdin pipe to bgrnd process:
$p1 = serialize($param1);
$p2 = serialize($param2);
$p3 = serialize($param3);
fwrite($bgproc, $p1 . "\n" . $p2 . "\n" . $p3 . "\n");
pclose($bgproc);
}
In the CLI script you would receive these params like this...
$fp = fopen('php://stdin', 'r');
$param1 = unserialize(fgets($fp));
$param2 = unserialize(fgets($fp));
$param3 = unserialize(fgets($fp));
fclose($fp);
...and do anything with them that would take to long under webserver control.
This technique works equally well in *nix and Windows environments.
Upvotes: 30
Reputation: 1513
As default, PHP scripts timesout after 30 seconds which can be overridden by editing the PHP.ini or by adding this at the top of your script.
set_time_limit(0);
This sets unlimited execution time to your script, that is, it never ends unless the scripts finish execution, or the server goes down, or the file is deleted or any fatal error comes.
Additionally,
You can add this to your script and open it in your browser to initiate the script, and it will run as if you are opening it in a browser and keeping the browser open.
ignore_user_abort();
It simply runs the script in background. These both would be useful for you.
ADD : When the scripts are run from the commandline,Cli, the default timeout is 0.(No Timeout)
Upvotes: 4
Reputation: 5084
No there are no time limits in php itself when executing php from the command line.
But there can be other timeouts, like connections to mysql. So if you have a mysql connection in your code, make sure to keep it alive or set your mysql timeout to something high enough to run your code. Another thing: I've seen some webhosting providers killing php apps running more then a few minutes. So make sure your provider does not do that.
Upvotes: 5