I want run a php script weekly using a cron job, however the script may take a few minutes or more.
Is there any way i can allow a greater max_execution_time just for this script?
You don't need to set a higher max_execution_time if you use PHP CLI: http://nl3.php.net/manual/en/features.commandline.differences.php
Maybe you should try these answers:
How do you get a Cronjob executing a PHP script to run longer than 30 seconds.
PHP command line: max_execution_time and memory_limit
But of course, using ini_set("max_execution_time",60) as the first php line in your job's script should do the trick.
Regards, Daniel
You can use set_time_limit(). If you want to disable timeout overall, pass it 0 as an argument. Otherwise pass it the number of seconds of max execution time.
You can use set_time_limit(0) at the start of your code: this removes the execution time limit altogether for this script. Note that it means that the script could run "forever", so put some checks in place in case it hangs.
set_time_limit
Related
PHP Has a method called set_time_limit (and a config option max_execution_time), which allows you to specify a time limit which triggers the script to exit when reached. However, this does not take into account time on external calls. Which means that if (for example) the PHP script makes a database call that does something dumb and runs for hours, the set time limit will not trigger.
If I want to guarantee that my PHP script's execution time will not exceed a specific time, is there some way to do this? I'm running the script directly in PHP (rather than via apache or similar), so I'm considering writing a bash script to monitor ps and kill it after a certain time. It occurs to me that I can't be the first person with this problem though, so is there some already-built solution available for this?
Note: This question is NOT about the PHP error from exceeding it's maxed execution time. This is about the case where PHP exceeds a time limit without triggering this error. This question is NOT just about limiting the time of a PHP script, it is about limiting the time allowed to a PHP script making external calls.
When running PHP from the command line the default setting "max_execution_time" is 0.
You can try something similar to solve your problem.
$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
}
I want to run simple code at cli and check the value of php setting max_execution_time. Of course I can check it at php.ini or .htaccess. I tried to do it using cli
php -a
echo ini_get('max_execution_time');
but it return 0 always. Is there any way to check it at cli?
The reason you get 0 is because you're echoing the result of ini_set('max_execution_time').
Per the manual, ini_set() requres two parameters, and returns FALSE upon failure. By providing only one parameter, you cause the function to return FALSE every time.
If you want to get the value of max_execution_time you should call ini_get().
I think what you want to do here is get the value from the ini configuration. For that you'll need to use ini_get()
A max execution time of zero simply means that there is no limit. The script will never timeout. This is the default behavior when running PHP from the command line
Taken from the documentation -
max_execution_time -
This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.
You can use the following to check for execution time
linux-1:~$ php -i | grep max_execution
max_execution_time => 0 => 0
I have made a queue using MySQL and PHP. The PHP script first retrieves all the tasks to be done from database and then executes all the tasks one by one using loop. But since there are many tasks and each task require a lot of time, the result is an error of 'Maximum execution time exceeded'.
How can I fix this? Please don't suggest I edit php.ini. I tested this on my browser but the PHP script will be invoked using cron.
You may set this piece of code on the top of your php code:
ini_set('max_execution_time', 0);
Or create in the same folder a .htaccess file with this code in it:
php_value max_execution_time 0
I have a command that when run direct on the command line works as expected. It runs for over 30 seconds and does not throw any errors. When the same command is called through a PHP script through the php function exec() (which is contained in a script called by a cron) it throws the following error:
Maximum execution time of 30 seconds
exceeded
We have a number of servers and i have run this command on a very similar server with the exact same dataset without any issues so i'm happy there is no script-level issue. I'm becoming more inclined to think this is related to something at the server level - either in the PHP setup or the server setup in some way but really not sure where to look. For those that are interested both servers have a max execution time of 30 seconds.
the command itself is called like this -
from command line as:
root#server>php -q /path/to/file.php
this works...
and via cron within a PHP file as:
exec("php -q /path/to/file.php");
this throws the max execution time error. it was always my understanding that there was no execution time limit when PHP is run from the command line.
I should point out that the script that is called, calls a number of other scripts and it is one of these scripts that is erroring. Looking at my logs, the max execution time error actually occurs before 30 seconds has even elapsed too! So, less than 30 seconds after being called, a script, called by a cron script that appears to be running as CLI is throwing a max execution error.
To check that the script is running as i expected (as CLI with no max execution time) i performed the following check:
A PHP script containing this code:
// test.php
echo exec("php test2.php");
where test2.php contains:
echo ini_get('max_execution_time');
and this script is run like this:
root#server> php test.php
// returns 0
This proves a script called in this way is running under CLI with a max execution time of 0 which just proves my thoughts, i really cannot see why this script is failing on max execution time!
it seems that your script takes too much time to execute, try to
set time limit, http://php.net/manual/en/function.set-time-limit.php
or check this post:
Asynchronous shell exec in PHP
Does the command take over 30 seconds on the command line? Have you tried increased the execution timeout in the php.ini?
You can temporarily set the timeout by including this at the top of the script. This will not work when running in safe mode as is specified in the documents for setting max_execution_time with ini_set().
<?php
ini_set('max_execution_time', 60); // Set to be longer than
// 60 seconds if needed
// Rest of script...
?>
One thing of note in the docs is this:
When running PHP from the command line
the default setting is 0.
What does php -v | grep cli, run from both the shell and in the exec command from the cron-loaded php file show?
Does explictly typing /usr/bin/php (modify as appropriate) make any difference?
I've actually found what the issue is (kinda). It seems that its maybe a bug with PHP reporting max_execution_time to be exceeded when the error is actually with max_input_time as described here
I tried changing the exec call to php -d max_execution_time=0 -q /path/to/file.php and i got the error "Maximum execution time of 0 seconds exceeded" which makes no sense, i changed the code to be php -d max_input_time=0 -q /path/to/file.php and the code ran without erroring. Unfortunately, its still running 10 minutes later. At least this proves that the issue is with max_input_time though
I'm surprised that no one above has actually timed the completed exec call. The problem is that exec(x) is taking a much longer time than command line x. I have a very complex perl script (with 8 levels of internal recursion) that takes about 40 sec to execute from the command line. Using exec inside a php script to call the same perl program takes about 300 sec to execute, i.e., a factor of about 7X longer time. This is such an unexpected effect that people aren't increasing their max execution time sufficiently to see their programs complete. As a result, they are mystified by the timeout. (BTW, I am running on WAMP in a fast machine with nominally 8 cpus, and the rest of my php program is essentially trivial, so the time difference must be completely in the exec.)
create wrapper.sh file as below
export DISPLAY=:0<br>
xhost + 2>>/var/www/err.log<br>
/usr/bin/php "/var/www/read_sms1.php" 2>>/var/www/err.log<br>
and put it in cron as below
bash /var/www/wrapper.sh<br>
y read_sms1.php contain<br>
$ping_ex = exec("/usr/local/bin/gnokii --getsms SM 1 end ", $exec_result, $pr);
and above solution workedfine for me in ubuntu 12.04
This question already has answers here:
Limit execution time of an function or command PHP
(7 answers)
Closed 9 years ago.
I've a script to send packages of 300-500 e-mails hour. That means that this script will be fired once a hour using cron or other feature.
The server has a max execution limit of 30secs and it's not configurable.
I've been thinking if the pseudo-code below should work:
$time=time();
$count=0;
while(condition){
$count++;
send(email);
$now=time();
if($now-$time>=29){break;} //1sec margin
}
echo "$count e-mails sent";
Opinions?
if your script is launched with cron it means that you're using PHP-CLI "PHP Command Line Interface".
As mentioned in the PHP documentation, your have no time limit while using CLI.
So you don't have to worry about that : max_execution_time is set to unlimited.
Just to double-check that you can't set the execution time, here are two suggestions.
You could simply call set_time_limit() before sending an e-mail. According to the PHP docs:
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
For instance:
foreach ($emails as $email) {
set_time_limit(30);
send($email, ...);
}
Another option is via the cron. Since you are running PHP from a cron job, you can specify your own php.ini. You could execute your script as follows:
php -c /custom/directory/my_php.ini my_script.php
Where my_php.ini may specify:
max_execution_time = 0 ; (unlimited)
Break up the task in smaller chunks. Use the database to keep "state" of the actual job execution.
This approach has the advantage of being scalable: you probably will end-up having to send more emails as you grow, won't you?