PHP Script stops working after 60 seconds - php

I'm trying to create a form just to mess around with and I there are two sections of this form. The first input is a number to count up to and the second input is a delay interval in seconds.
I have two ways of doing this. One, is to do this client side and the other is to take the variables and send them via php. This will allow the user to leave the website and have the script still working.
The issue with this being server side is that after 60 seconds, the PHP script stops working. The form is being sent via AJAX. Here is my PHP code. I'm also on shared hosting and do not have the ability to edit PHP.ini.
$amount = $_POST["amount"];
$time = $_POST["time"];
date_default_timezone_set('America/Los_Angeles');
$x = 0;
while($x < $amount) {
echo $x;
$x++;
sleep($time);
}
echo ("Counted to " . $x);

You can use max execution time on your php file, add this line in your php file.
ini_set('max_execution_time', 300); //300 seconds = 5 minutes

You can use set_time_limit to set the max execution time, and 0 means no time limit (not recommanded for cgi, since you web server, apache/iis still have a time limit)
and if you want to change your execution time limit of all the php scripts, you can change the setting max_execution_time in the php.ini

Related

How to stop PHP script after it executes for 3 mins

My website is hosted on digitalocean server and i have full control over the server. I have php script that downloads thousand of images using proxy IP. Sometimes my script takes infinite time and it slows the whole site. I checked max_execution_time and it is set to default 30 secs. But still my script doesn't stop after 30 secs. How can i stop my script from running after 2 mins so that my site doesn't effect other scripts.
The 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.
refer this link for more details.
You can try recording the timestamp at the beginning and referencing it each iteration. Use break; or return; to exit the loop
function getImagesFromRemote($urlArray=[]) {
$start = time();
$maxduration = 3 * 60 ;
foreach ($urlArray as $url) {
// do your code
$something[] = $value;
$elapsed = time() - $start;
if ($elapsed >= $maxduration) break;
}
return $something;
}
}
By using exit() function we can stop the execution of php script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.

Can I run a CRON job for an hour

I am creating an app that requires a CRON job to be executed every 60 seconds.
My host only allows a CRON job once an hour.
I was wondering if I could create a script like this:
$i = 0;
while(1==1){
// update database code
delay(10000);
// $i is incremented once a minute/10000ms
$i++;
}
//if $i reaches 60 we know that the CRON has run for an hour
if($i == 60){
die();
}
Is this recommended? Will this accurately update my database every 60 seconds?
I don't mind if the script will be a few seconds out.
I understand I will need to set my php.ini to increase the max execution time.
Should work, if the hosting allows overriding of max_execution_time.
You can do it right in the script, by the way: ini_set('max_execution_time', 0). And be careful with memory. It's not C/C++, of course, but still a good idea to watch variables initialization and loops.

PHP sleep delay

In PHP, I want to put a number of second delay on each iteration of the loop.
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
//sleep for 3 seconds
}
How can I do this?
Use PHP sleep() function. http://php.net/manual/en/function.sleep.php
This stops execution of next loop for the given number of seconds. So something like this
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
sleep(3); // this should halt for 3 seconds for every loop
}
I see what you are doing... your delaying a script to constantly check for a file on the filesystem (one that is being uploaded or being written by another script I assume). This is a BAD way to do it.
Your script will run slowly. Choking the server if several users are running that script.
Your server may timeout for some users.
HDD access is a costly resource.
There are better ways to do this.
You could use Ajax. And use a timeout to call your PHP script every few seconds. This will avoid the slow script loading. And also you can keep doing it constantly (the current for loop will only run for 33 seconds and then stop).
You can use a database. In some cases database access is faster than HDD access. Especially with views and caching. The script creating the file/uploading the file can set a flag in a table (i.e. file_exists) and then you can have a script that checks that field in your database.
You can use sleep(3) which sleeps the thread for 3 seconds.
Correction sleep method in php are in seconds.
Hare are two ways to sleep php script for some period of time. When you have your code and want to pause script working for some time use these functions.
In these examples the first part of code will be done on script run and the second part of code will be done but with time delay.
Using sleep() function you can define sleep time in seconds.
Example:
echo "Message 1";
// The first part of code.
$timeInSeconds = 3;
sleep($timeInSeconds);
// The second part of code.
echo "Message 2";
This way it is possible to sleep php script for 3 seconds. Using this function you can sleep script for whole number (integer) of seconds.
Using usleep() function you can define sleep time in microseconds. This sleep time is convenient for intervals that require more precise time than one second.
Example:
echo "Message 1";
// The first part of code.
$timeInMicroSeconds = 2487147;
usleep($timeInMicroSeconds);
// The second part of code.
echo "Message 2";
You can use this function if you want to sleep php for smaller time values than second (float). In this example I have put script to sleep for 2.487147 seconds.
Have you considered using a PHP Daemon script using supervisorD. I use it in multiple tasks that are required to be running all the time.
The catch is making sure that each time you are running your script you check for memory resources. If its too high, stop the process and then let it restart itself up again.
I have successfully used this process to be always checking database records for tasks to process.
It might be overkill but worth considering.

CodeIgniter php script is not running more than 2 minutes??

I have a PHP script in my Code Igniter application,
its run on server and fetch some data but its not running more than 2 minutes approx..
and when I run this without using code igniter its works properly..what may be the reason behind this?
thanks #air4x its works . by setting set_time_limit(300) in the system/core/CodeIgniter.php
if (function_exists("set_time_limit") == TRUE AND #ini_get("safe_mode") == 0)
{
#set_time_limit(300);
}
after setting this code script running well..
Try adding this before you run your code: set_time_limit(0);
More info: http://php.net/manual/en/function.set-time-limit.php
If that doesn't work, you'll need to share what code you are running and what happens when it stops running.
There must be, Set_time_limit - Sets the maximum execution time of a script.
bool set_time_limit ( int $seconds )
When set_time_limit() called. It returns the counter to zero. In other words, if the default limit is 30 seconds, and after 25 seconds of script execution the set_time_limit(20) call is made, then the script will run for a total of 45 seconds before finishing .
Please visit http://php.net/manual/fr/function.set-time-limit.php for more information.

Importing csv timeout issue

I'm trying to modify a csv import function which times out after 60 sec of importing. For every line there are images that are resized and some other code is executed.
I know the vps can handle this but in batches because I have another website on the same server that runs a different csv program but does the same thing. That program can import 8000 lines and resize images as well. The settings there are: process 10 lines and wait 3 sec, repeat.
Settings I raised:
set_time_limit
max_execution_time
Browser http keep alive timeout
I have tried sleep() for every 10th line but this only makes the process import fewer lines
if( (($current_line % 10) == 0) && ($current_line != 0) )
{
sleep(3);
}
This is how the script loops through the file
for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++)
{
//code here
}
Server:
Apache
PHP 5.3.3
MYSQL
Varnish cache
What can I do to make this work?
The first thing to try when your script times out is to run it using the php-cli. There is no execution time limit to scripts that are run through the command line.
If this doesn't solve your problem, then you know it wasn't the execution time limit.
The second thing to try is to print out regular status messages, including from memory_get_usage() so that you can eliminate memory leaks as a cause for your script crash. This may help you identify whether your script was dying on some input.
you can over-write the default timeout time.
set_time_limit (0) ;
using sleep will make it import fewer lines, basically the script is timeing out because it is taking over 60 seconds. by adding sleep it just gets less done in 60 seconds.
If this is a critical script i'd look at moving it to another programing language that can execute this faster. if its just a one off, or not mission critical try set_time_limit(0) which makes it never time out. also try typing php scriptname into the browser to run the script in command line.
Try outputting something to the browser to keep the browser alive. IE times out after 1 minute if inactivity FF is 3 minutes.
<?
if( (($current_line % 10) == 0) && ($current_line != 0) )
{
sleep(3);
echo '. ';
}
?>

Categories