我想做一个长期的工作,我不能修改最大执行时间,所以我采取了一个解决方案:当脚本要关闭时,脚本会向自己发送一个http请求,所以脚本可以继续运行。 这是我的测试脚本 t.php
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
function post_request_async($url, ARRAY $data) {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
$host = $url['host'];
$path = $url['path'];
$port = isset($url['port']) ? $url['port'] : "80";
$fp = fsockopen($host, $port, $errno, $errstr, 5);
if ($fp){
$req = "";
$req.="POST $path HTTP/1.0\r\n";
$req.="Host: $host\r\n";
$req.="Content-type: application/x-www-form-urlencoded\r\n";
$req.="Content-length: ". strlen($data) ."\r\n";
$req.="Connection: close\r\n\r\n";
$req.= $data;
fputs($fp, $req);
fclose($fp);
}
}
set_time_limit(10);
register_shutdown_function("shutdown_func");
file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." start -->".PHP_EOL, FILE_APPEND);
function shutdown_func(){
file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." shutdown_func -->".PHP_EOL, FILE_APPEND);
post_request_async("http://127.0.0.1/t.php", array());
exit();
}
for(;;) {
sleep(2);
}
?>
但是结果很奇怪:zz.txt
2012-05-16 10:11:42 start -->
2012-05-16 10:11:54 shutdown_func -->
2012-05-16 10:11:54 start -->
2012-05-16 10:12:04 shutdown_func -->
2012-05-16 10:12:04 start -->
2012-05-16 10:12:14 shutdown_func -->
2012-05-16 10:12:14 start -->
2012-05-16 10:12:26 shutdown_func -->
2012-05-16 10:12:26 start -->
2012-05-16 10:12:38 shutdown_func -->
2012-05-16 10:12:38 start -->
2012-05-16 10:12:48 shutdown_func -->
2012-05-16 10:12:48 start -->
2012-05-16 10:12:58 shutdown_func -->
2012-05-16 10:12:58 start -->
2012-05-16 10:13:08 shutdown_func -->
2012-05-16 10:13:08 start -->
2012-05-16 10:13:18 shutdown_func -->
2012-05-16 10:13:18 start -->
2012-05-16 10:13:28 shutdown_func -->
2012-05-16 10:13:28 start -->
2012-05-16 10:13:40 shutdown_func -->
2012-05-16 10:13:40 start -->
2012-05-16 10:13:50 shutdown_func -->
2012-05-16 10:13:50 start -->
2012-05-16 10:14:02 shutdown_func -->
2012-05-16 10:14:02 start -->
2012-05-16 10:14:14 shutdown_func -->
2012-05-16 10:14:14 start -->
2012-05-16 10:14:26 shutdown_func -->
2012-05-16 10:14:26 start -->
2012-05-16 10:14:36 shutdown_func -->
2012-05-16 10:14:36 start -->
2012-05-16 10:14:48 shutdown_func -->
2012-05-16 10:14:48 start -->
2012-05-16 10:14:58 shutdown_func -->
2012-05-16 10:14:58 start -->
2012-05-16 10:15:08 shutdown_func -->
2012-05-16 10:15:08 start -->
2012-05-16 10:15:18 shutdown_func -->
2012-05-16 10:15:18 start -->
php_errors.log
[16-May-2012 02:11:54 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:04 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:14 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:26 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:38 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:48 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:58 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:08 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:18 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:40 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:50 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:02 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:14 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:26 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:36 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:48 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:58 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:08 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:18 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 33
看起来关闭功能被调用了大约 22 次,但突然没有被调用。
我的 php 版本是 5.3.9,服务器是 IIS 7.5 真的需要一些帮助!非常感谢!
------------------------更新1------------- ------------------
经过一番挖掘,我在 php 源代码中找到了一个测试脚本。看起来注册的关闭功能可能会超时。
<?php
set_time_limit(1);
register_shutdown_function("plop");
function plop() {
$ts = time();
while(true) {
if ((time()-$ts) > 2) {
echo "Failed!";
break;
}
}
}
plop();
?>
结果是:
Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9
Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9