strange! php script suspended, not exit and not continue, like dead

I have a simple test php script:


[php]

eShop Tools <?php ini_set('display_errors',0); error_reporting(E_ERROR | E_WARNING | E_PARSE);

$max_execution_time = ini_get(‘max_execution_time’);
ini_set(“max_execution_time”, 300);

file_put_contents(“log111.txt”,date(“Y-m-d H:i:s”)."=>begin while"."\r\n",FILE_APPEND);

$StartTime = date(“y-m-d h:i:s”);
while(true){
$EndTime = date(“y-m-d h:i:s”);
$TimeSpent = strtotime($EndTime)-strtotime($StartTime);

$i=0;
do{
	$i++;
}while($i<100000);

if( $TimeSpent >120){
	break;
}

}

file_put_contents(“log111.txt”, date(“Y-m-d H:i:s”)."=>before echo"."\r\n" ,FILE_APPEND);

ini_set(“max_execution_time”, $max_execution_time);

echo “Success”;

file_put_contents(“log111.txt”, date(“Y-m-d H:i:s”)."=>after echo"."\r\n" ,FILE_APPEND);

?>

Processing...
[/php] ************************************ when running this script, I got the following logs in log111.txt:

2015-12-26 10:46:31=>begin while
2015-12-26 10:48:32=>before echo
2015-12-26 10:48:32=>after echo

bug the browser display nothing. I can see the php script is still loading in browser.

when I change " $TimeSpent >20" , it works well and display "Success " and the button html.

and when " $TimeSpent >120" , this script also can run well in my local server and some hosting web site, but can’t run in SiteGround web site, it just display nothing and show running there. SiteGround support didn’t know the reason.

Does anyone know what is the reason? I will very appreciate.

What are you trying to accomplish with this?

I don’t understand why you want to loop for 2 minutes without doing anything. When other functions can do the same for less.

just for testing! Actually, my script will update database, it also need about 3-4 minutes. I don’t know why the php script suspend there.

You have me even more perplexed. I have dealt with databases that have tables with 10’s of millions of records in them and don’t take 4 minutes to run updates on.

I know. I am doing not only database operation, but also doing some other operation. It is not the question.

even I don’t use “while” clause, but use “sleep”, the result is same, no output on browser.

Honestly, if you have a script that has an extended execution time, use a cron job or run it from command line.

It sounds like max_execution_time is not high enough.
The default setting in php.ini is 30 seconds. If you run the script from command line like astonecipher suggested in a cron job then the execution time is set to 0 (no limit).

If you have access to the php.ini file in siteground then view this doc for more info.
http://php.net/manual/en/info.configuration.php#ini.max-execution-time

If it is not a test you need to run from a browser then just run the script from command line :
~$ php yourscript.php [$vars]

thanks for astonecipher’s advice. But if I use cron job to do such job, how do I know this job execute successfully? The logs can’t tell me anything about executiong status, because the logs show the script already executed, but in face, it is still running, not exit, like suspending there.

the websit max_execution_time default is 600. this is enough.

Depending on the host, they may have an email parameter that will notify you when the script has run successfully/failed.

If not, you can roll your own in bash or php to email you when complete.

thanks, I’ll try this.

Have you tried ini_set(‘display_errors’,1);

yes, but nothing displayed.

Here is an example along the lines of your test file and how to call it.

cronTest.php
[php]

<?php $startTime = date( "Y-m-d h:i:s" ); $verbose = false; if( in_array( "-v", $argv ) ) { $verbose = true; } function printContent( $verbose = false, $str ) { $time = date( "Y-m-d h:i:s" ); if ( $verbose ) echo "$time:: $str\n\r"; } printContent($verbose, "Script finished"); for( $i = 0; $i < 1000000; $i ++ ) { if( $i % 1000 ) { printContent( $verbose, "counter at $i" ); } } printContent( $verbose, "Script finished" ); $endTime = date("y-m-d h:i:s"); $execTime = strtotime($endTime)-strtotime($startTime); printContent( $verbose, "Script took $execTime seconds to run.\n\r"); [/php] to use, go to your command line where the file is and enter: php cronTest.php -v

Hi, astonecipher, thanks for your help. can you give some advise on my application?

My application is used to revise ebay product quantity and price by using ebay api. I got new quantity and price from our databse and call ebay api to revise one by one(actually can revise 4 items per call), each call needs 1-2 seconds, so total time will exceed 20 minutes. I used ajax call to do this, but I found if the execution time exceed about 85 seconds, ajax can’t receive the return. So I have the above test script.

when I run same script on my local machine and IX Web Hosting, it works well. But on SiteGround host, it has the above problem. SiteGround can’t find the reason. Their apache timeout setting is:

The default Apache timeout value is 300 seconds, there is also a timeout of 120 seconds imposed by the Chroot environment. I have increased that value to 300 as well.
The monitoring system is killing php processes that are running for more than 250 seconds.

I don’t know why I can only run about 85 seconds.

Because our database procuct is varying with time, it’s not better to revise by using cron job.

can you give me some advice on how to implement revising? thanks very much!

Well, you can set a cron to run every minute, so it is not only still viable, it should be what you use to handle it. That way you are not relying on anything but the server to institute it.

I am at work now, so give me a bit to think of something that may help.

Which API are you currently using?

ReviseInventoryStatus api, by using curl to send xml.

even by using cron job, how can I revise all items in such short time in one process?

First: SOAP via cmdline.

Run this script on the problem server and see what it does.
[php]

<?php $startTime = date( "Y-m-d h:i:s" ); $verbose = false; if( in_array( "-v", $argv ) ) { $verbose = true; } function printContent( $verbose = false, $str ) { $time = date( "Y-m-d h:i:s" ); if( $verbose ) echo "$time:: $str\n\r"; } printContent( $verbose, "Script finished" ); for( $i = 0; $i < 5000000; $i ++ ) { if( $i % 1000 ) { printContent( $verbose, "counter at $i" ); } } printContent( $verbose, "Script finished" ); $endTime = date( "y-m-d h:i:s" ); $execTime = strtotime( $endTime ) - strtotime( $startTime ); printContent( $verbose, "Script took $execTime seconds to run.\n\r" ); if( mail( "[email protected]", 'Cron finished', __FILE__ . " ran for $execTime seconds and closed properly." ) ) { printContent( $verbose, "Mail sent" ); } else { printContent( $verbose, "Mail failed to send." ); }[/php] I am not seeing anything that says it takes X seconds for a return. If that is when you run it through ajax, discount it. Ajax has more connections to make than a cron or straight script would have. If you are not on a sandbox account, set on up and run some tests against it.

Another thought, is to use this API instead, BulkRevise and update in batches.

I can’t access command line on web host, I will think about using BulkRevise and update in batches.

thank you very much!

Sponsor our Newsletter | Privacy Policy | Terms of Service