I am using PHP script for some backend tasks(not web) and I found sometime curl_exec will terminate the executing of script without outputing any errors. I want my script to run in a loop forever and any idea about this?
-
yes, it's for a backend task. I need it to loop as long as I need. I need to know why curl_exec will terminate the script and how to avoid this – Mickey Shine May 14 '10 at 14:07
-
Show some code please. You are aware of PHP's max_execution_time setting? – Pekka May 14 '10 at 14:09
-
Does this task run constantly (i.e. process starts and continues for hours), or is it some kind of scheduled task? – Tim Post May 16 '10 at 05:19
4 Answers
If you want the process to be able to start and run infinitely, you need to:
set_time_limit(0);
Otherwise, as soon as your script has run for 30 seconds (or whatever max_execution_time specifies in php.ini) the script will exit with an error. This interrupts anything the script might be doing, e.g. getting a return from curl_exec()
.
A caution regarding using PHP to write a daemon (which is essentially what it seems like you are doing) can be found here.
That could easily be a timeout problem.
You can set the curl timeout (for fetching a website) e.g. to 60 seconds via
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
Then you need to set the PHP timeout to something higher than 60 seconds, e.g.
set_time_limit(90);
This timeout is real time on Windows and CPU time on Unix, therefore you would need much less in Unix. It is important to set the PHP timeout within your loop, otherwise it's the overall limit and would never be enough for your endless loop.
do {
set_time_limit(90);
// curl stuff
} while (true);

- 597
- 5
- 8
-
-
Yes, if the reason for the interruption was the PHP timeout. If you have your (intentional) endless loop without setting the timeout, then the default timeout will interrupt the script sooner or later. We don't know, of course, whether your script has been interrupted for some other reason before the timeout would have stopped it. – Sam May 16 '10 at 07:22
In your php, you can set the CURLOPT_VERBOSE variable to trace the issue :
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
This then logs to STDERR, or to the file specified using CURLOPT_STDERR:
curl_setopt($curl, CURLOPT_STDERR, './path/to/file.log');
From the command line, you can use the following switches:
* --verbose to report more info to the command line
* --trace <file> or --trace-ascii <file> to trace to a file
You can use --trace-time to prepend time stamps to verbose/file outputs

- 6,377
- 14
- 59
- 91
-
after you get the error using the above options please post it so we can help you to fix it :) – Michael May 16 '10 at 05:20
Make sure that you have error reporting set, put this on top of your php file:
ini_set('display_errors', true);
error_reporting(E_ALL);
Also you can extend the script's execution time:
ini_set('max_execution_time', 50000);

- 377,238
- 77
- 533
- 578