I know that PHP CLI is usually used because of none time limits and primary because it is not using Apache threads/processes.
But is there any way how to explicitly
I have a similar problem. But in my case, I had remote database calls taking significant time and I wanted to terminate the script after a certain amount of time. As noted in the other answers, external calls (or sleep()
) doesn't count against the configured timeout setting.
So my approach was to figure out the scripts pid, and then background a kill -9
command after a sleep. Here's a basic example:
$cmd = "(sleep 10 && kill -9 ".getmypid().") > /dev/null &";
exec($cmd); //issue a command to force kill this process in 10 seconds
$i=0;
while(1){
echo $i++."\n";
sleep(1); //simulating a query
}
Note that it's possible that your script ends nicely, and a new, different one starts up with that same pid which this kill command would terminate. That is unlikely, but you should consider that as a possibility if you use this approach.