php script test max execution time - php

2 questions for PHP in Linux:
1.) will register_shutdown_function be called if a script exceeds the maximum time limit for the script to run?
2.) if #1 is true, how can I test a script going beyond its max time limit if sleep() doesn't count towards execution time?

A function registered with register_shutdown_function will be called when the execution time limit is reached. You can test the limit with a busy wait:
<?php
register_shutdown_function(function() {
echo "shutdown function called\n";
});
set_time_limit(1); // Set time limit to 1 second (optional)
for (;;) ; // Busy wait

Related

How to check if execution of script exceeds x seconds?

I have a PHP script that uses an API. Sometimes the API can be fast, sometimes it’s slow. Is there a way to check if execution time of the get_contents(); is above ex. 2 seconds?
If it goes over this amount i want the included script to stop, and execute the original file.
Ex:
<?php
include("file.php"); //if more than 2 sec, continue
echo "Hello world";
?>
You may use set_time_limit function to set the maximum time limit
.... //some logic
const MAX_EXECUTION_TIME = 2; // for 2 seconds
set_time_limit(self::MAX_EXECUTION_TIME);
file_get_contents(...);
.... // some other logic

usleep reaches max limit very quickly, how to prolong

I put my code on sleep when no data returned by server. But it gives me this error after a while being idle.
<b>Fatal error</b>: Maximum execution time of 120 seconds exceeded in
I know, changing the max limit in php.ini would help. But i don't want to do this, because I don't own the server...so I can't be changing each clients server limit.
how can I set the max limit to infinity here or probably how to reconnect if reached max?
while ($currentmodif <= $lastmodif)
{
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
$currentmodif = filemtime($filename);
}
You'll have to set it to zero. Zero means the script can run forever. Add the following at the start of your script:
ini_set('max_execution_time', 0);
set_time_limit() does not return anything so you can't use it to detect whether it succeeded. Additionally, it'll throw a warning:
Warning: set_time_limit(): Cannot set time limit in safe mode
ini_set() returns FALSE on failure and does not trigger warnings.

How can I delay a php functions for days?

Function, which waits about one day in the background and then execute another.
Like:
function Sleep(){
sleep( /* One Day */ );
Run();
}
function Run(){
//One Day later,
//execute code.
}
Or maby something like this (this is fictional):
class Waiter extends Timer{
$time = 0;
function __construct($time){
$this->time = $time;
}
function onDelay(){
//One day Later.
}
}
$wait = new Waiter( /* One Day */ );
Is there a good solution?
Or is the sleep() function also okey?
But I have to say, that the execution timeout is 30 seconds.
Using a cronjob is the correct solution for this problem. If for some reason you cannot use it, make sure to add ignore_user_abort(1) and set_time_limit(0); at the top of the php script.
int ignore_user_abort ([ bool $value ] )
When running PHP as a command line script, and the script's tty goes
away without the script being terminated then the script will die the
next time it tries to write anything, unless value is set to TRUE
bool set_time_limit ( int $seconds )
Set the number of seconds a script is allowed to run. If this is
reached, the script returns a fatal error. The default limit is 30
seconds or, if it exists, the max_execution_time value defined in the
php.ini.
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.
As you said, the execution time is 30 seconds, the overall script is forced to an end after 30 seconds. It can't wait longer.
As already suggested, you could use a cron job.
If your problem is not regular recurrent, you could write a little script, which writes the time (with date) when the function should execute into a file (or a database). The cron then would execute every hour (or if its important, every minute) and checks, if the function needs to be executed
Crons have been mentioned, but there's a second option - queueing.
https://en.wikipedia.org/wiki/Message_queue
There is a wide variety of queue software available, from ones you install yourself like Beanstalk or RabbitMQ to hosted ones in the cloud like Amazon SQS or IronMQ.

php- set a script timeout and call a function to cleanup when time limit reached

I am writing a PHP script that will do some processing, but I only want it to run if the script isn't already running. I also only want the script to run for a maximum of 5 minutes.
I know I can set the timeout using set_time_limit, but doing so will not remove the lock files that I create during the execution.
Is their a way to call a function when the time limit is reached so I can perform a cleanup?
Tried this out in a test environment and got it working with the following (error reporting off + shutdown function):
<?php
$test = function() {
print("\n\nOOPS!\n\n");
};
error_reporting(0);
set_time_limit(1);
register_shutdown_function($test);
while (true) {
}

PHP Max Exection Timeout Notification

Is it possible to get Max Execution timeout notification so that i can use code clean up operations before the php script stops running?
Basically, i am trying to create a script that can do some changes/modifications to my database which has huge data pile. Also, this php script execution can be paused/resumed using a filelock.
You can either use set_error_handler() or register_shutdown_function().
Edit: See also: max execution time error handling
If you set 0 to set_time_limit, it will never stop :)
You can increase the limit time inside your code using:
set_time_limit(300); //time in seconds
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
or
changing the max_execution_time parameter inside your php.ini.
The default time is 30 seconds.

Categories