i have a script in php extract_data.phpand it takes 20 minutes to run the script.
I activated cron to run the script. but the cron has a time of only 30 seconds (this time cannot be increased).
the problem is that i always get the timeout error.
I would like it to appear: file loading... while the script is running.
i test exec("extract_data.php"." > /dev/null &");
does not work
It seems that you have a default configuration for the timeout.
Please take a look here: https://www.php.net/manual/en/function.set-time-limit.php
set_time_limit(0); // To run without timeout.
Or
set_time_limit(20*60); //to allow to run for only 20 minutes
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'm using https://cron-job.org/ for a cron job but I have a problem. When running the script manually (for example, a script that gets data from a csv file), it works but if I run the script through that cron job, it fails because of that 30 seconds max_execution_time I guess.
But the problem is that in my script I'm already using:
ini_set('max_execution_time', 300);
It should be 5 minutes instead of 30 seconds before the cron job fails. What am I doing wrong?
Here is an image with that cron job history:
cron-job.org has some limits:
How and how long does cron-job.org visit my URLs?
cron-job.org visits your URLs at the configured dates/intervals and waits for the URL/script to finish execution. If your URL/script does not finish after 30 seconds, it will timeout and we will close the connection to prevent delays in the execution of the jobs of other users. Our system reads up to 1024 bytes of the output of your URLs/scripts. In case your script sends more data, job execution will be aborted. (Please also see the next question.)
You can read more here: FAQ.
Any idea why a simple infinite loop stops on the very first minute of every hour? For instance, 21:01, 22:01, 23:01, etc.
The server is running Ubuntu 12.04 and the PHP script is launched using command: "php -f test.php"
while (1 == 1) {
echo "test";
sleep(30);
}
Any help is appreciated.
UPDATE: It doesn't matter whether I run the script 16:05 or 16:49, it will stop on 17:01, so the issue is not related to the set_time_limit value.
UPDATE: If the script has to sleep for an hour and then echo "test", the "test" will not be echoed. The script stops it's work without finishing the loop itself.
UPDATE: It seems that I've found out what's killing the script. I've set the PHP script to report All errors and right before the *:01 time comes up, I get the text: Terminated. I've Googled that the script might get Terminated by OOM killer and, unfortunately, I don't have permission to change it's settings on my current VPS. I'm switching to VDS and will try to modify OOM killer settings.
i guess it's because of Limits the maximum execution time on php .
if you want repeat a script every day , hour and minute you should use Cronjob to call your script every time you need.
cronjob available in popular web hosts like direct admin and cpanel
for example you can use below command for run your script every one minute
*/1 * * * * /usr/local/bin/php -q /home/user/domains/domain.com/public_html/script.php
if you want run script every second you can set maximum execution to one minute and repeat a code every second in it and use cronjob to call it every minute
If you are using php file through apache i.e. as a URL then you should do
ini_set('max_execution_time', 0); //0=NOLIMIT
If you are running through CLI this should not be happening generally
UPDATE
Below are few Notes form php.net. I hope it will help you
Warning This function has no effect when PHP is running in safe mode.
There is no workaround other than turning off safe mode or changing
the time limit in the php.ini.
Note: The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.
So, it is possible that PHP code doesn't actually breaks. But, any other conenction/call is getting broken.
On my ubuntu system there is a script in /etc/cron.d/php5
# /etc/cron.d/php5: crontab fragment for php5
# This purges session files older than X, where X is defined in seconds
# as the largest value of session.gc_maxlifetime from all your php.ini
# files, or 24 minutes if not defined. See /usr/lib/php5/maxlifetime
# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime)
That script /usr/lib/php5/maxliftime will check for param
session.gc_maxlifetime
in any php.ini file located at /etc/php5/*/php.ini
Check for that value via
cd /etc/php5
grep -ri gc_maxlifetime .
If there is a value 3600 you should got it.
I have a long-running PHP script with set_time_limit(0) set. It works very good for 15 minutes (900 sec) but then become something very strange: a second process with the same parameters starting! I see it because I am starting a new log file at the beginning of the script and there is two log files processing same data!
BTW script runs in background from PHP with
exec('wget http://example.com/script.php?id=NNN > /dev/null &');
This instruction normally runs only once, and I can not get what runs it second time after 900 seconds (exact time).
This is because wget has a read time limit of 900sec. After it is reached, the download restarts.
You can set the timeout higher with the --timeout=seconds or the --read-timeout=seconds argument.
Or, you can start it directly from the shell(this way is much better).
Here is a link: wget download options
Here is the shell code(for Linux):
exec('php yourscript.php > /dev/null 2>&1 &');
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