设置 1 秒的时间限制不会在 1 秒后停止脚本

Setting a 1 second time limit does not stop the script after 1 second

我正在调用 shell 命令,可能需要一段时间,应该在 5 分钟后中断。无论如何,这似乎行不通。

我将其归结为以下测试代码:

<?php

set_time_limit(1);
echo 'TL = ' . set_time_limit(1) . PHP_EOL;
sleep(5);
echo 'finished, not interrupted';

这给了我以下输出:

TL = 1 finished, not interrupted

看起来时间限制设置正确 - 但脚本仍运行 5 秒。

我正在使用 PHP 版本 5.6.11 和 mod_php

PHP 时间限制仅在程序实际执行时强制执行。 sleep 延迟不一定算作延迟,as per the manual:

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.

sleep花费的时间不计入脚本执行时间。