Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are PHP scripts run using the "php" command affected by the timeout limit?

Are PHP scripts run using the "php" command affected by the timeout limit? I plan to schedule php scripts using cron.

like image 589
Dave Avatar asked May 18 '11 02:05

Dave


People also ask

Does PHP have a timeout?

The default timeout is 30 seconds. It can be changed using the max_execution_time php.

How long can a PHP script run?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.

What is set time limit in PHP?

The set_time_limit() function lets you set how long a script should be allowed to execute. This value is usually set inside php. ini under the max_execution_time setting; however, you can override that here. The function takes one parameter, which is the number of seconds you want the script to have.

How can we increase the execution time of a PHP script?

To increase the script execution time, you can use the following snippet at the top of your script. ini_set ( 'max_execution_time' , '300' ); In the above example, it would set the max_execution_time directive to 300 seconds.


3 Answers

Yes, but you can set an unlimited timeout by adding this to the top of your script:

set_time_limit(0);
like image 167
cailinanne Avatar answered Oct 14 '22 15:10

cailinanne


Some systems, such as Ubuntu, actually already start with separate CLI and Apache configurations in /etc/php5.

The relevant command in the ini file is:

max_execution_time = 30      ; Maximum execution time of each script, in seconds

However, if you are unable to modify the php.ini for whatever reason, you can create a new php.ini with configuration settings favourable to the command-line, and point to the file like so:

php -c /path/to/ini/php.ini -f script.php

Or, you can use Cailin's solution, and set the time limit at the top of the file - but if you are running on a server with PHP 'safe mode' enabled, then you will have to use your own ini file.

like image 31
HorusKol Avatar answered Oct 14 '22 15:10

HorusKol


Depends. If your php binary is the PHP CLI interface, the default max_execution_time is zero (meaning there is no limit).

On the other hand, if it's the older-style CGI binary, you will be affected by the max_execution_time limit, and you'll need to call set_time_limit to get rid of it (assuming you're not in the dreaded PHP safe mode).

like image 25
John Flatness Avatar answered Oct 14 '22 13:10

John Flatness