简体   繁体   English

在 PHP CLI 中设置 max_execution_time

[英]Set max_execution_time in PHP CLI

I know that PHP CLI is usually used because of none time limits and primary because it is not using Apache threads/processes.我知道通常使用 PHP CLI 是因为没有时间限制,主要是因为它不使用 Apache 线程/进程。

But is there any way how to explicitly set the max_execution_time for some scripts which i don't want to have the freedom of "unlimited time" and just want to keep those script under control?但是有什么方法可以为一些我不想拥有“无限时间”的自由而只想控制这些脚本的脚本显式设置 max_execution_time 吗?

If you think this question may be better answered on superuser.com and have permission to move it, do it.如果您认为在 superuser.com 上可能会更好地回答这个问题并有权移动它,那就去做吧。 :) :)

Edit : I've been Googling a bit and found the right parameter:编辑:我一直在谷歌搜索,找到了正确的参数:

php -d max_execution_time=5 script.php

The documentation says that when running in command line mode, the default is 0 (unlimited). 文档说在命令行模式下运行时,默认值为 0(无限制)。 It doesn't say that you cannot override it:它并没有说你不能覆盖它:

set_time_limit(10); // this way
ini_set('max_execution_time', 10); // or this way

Edit: I just tried it myself, and it works as expected.编辑:我自己试过了,它按预期工作。

F:\dev\www>c:php -f index.php
PHP Fatal error:  Maximum execution time of 2 seconds exceeded in F:\dev\www\index.php on line 3

Fatal error: Maximum execution time of 2 seconds exceeded in F:\dev\www\index.php on line 3

set_time_limit() works in CLI scripts. set_time_limit() 在 CLI 脚本中工作。

<?php 
set_time_limit(1); //in seconds
for (;;);
?>

After one second returns with the following error message:一秒钟后返回以下错误消息:

PHP Fatal error:  Maximum execution time of 1 second exceeded in \
/home/stivlo/test.php on line 4

Initially I tried with sleep() and the time limit doesn't get applied.最初我尝试使用 sleep() 并且没有应用时间限制。 As @Jon suggested, using a real computation, like an infinite loop works.正如@Jon 建议的那样,使用真正的计算,就像无限循环一样有效。

I found this interesting comment in the sleep() PHP page:我在sleep() PHP 页面中发现了这个有趣的评论:

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself.注意: set_time_limit() function 和配置指令 max_execution_time 只影响脚本本身的执行时间。 Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running在确定脚本运行的最长时间时,不包括在脚本执行之外发生的活动所花费的任何时间,例如使用 system() 的系统调用、sleep() function、数据库查询等

I have a similar problem.我有一个类似的问题。 But in my case, I had remote database calls taking significant time and I wanted to terminate the script after a certain amount of time.但就我而言,我的远程数据库调用需要花费大量时间,我想在一定时间后终止脚本。 As noted in the other answers, external calls (or sleep() ) doesn't count against the configured timeout setting.如其他答案所述,外部调用(或sleep() )不计入配置的超时设置。

So my approach was to figure out the scripts pid, and then background a kill -9 command after a sleep.所以我的方法是找出脚本 pid,然后在睡眠后执行kill -9命令。 Here's a basic example:这是一个基本示例:

$cmd = "(sleep 10  && kill -9 ".getmypid().") > /dev/null &";
exec($cmd); //issue a command to force kill this process in 10 seconds

$i=0;
while(1){
    echo $i++."\n";
    sleep(1); //simulating a query
}

Note that it's possible that your script ends nicely, and a new, different one starts up with that same pid which this kill command would terminate.请注意,您的脚本可能会很好地结束,并且一个新的、不同的脚本以该 kill 命令将终止的相同 pid 启动。 That is unlikely, but you should consider that as a possibility if you use this approach.这不太可能,但如果您使用这种方法,您应该将其视为一种可能性。

Use set_time_limit function.使用set_time_limit function。

You can also use „timeout” linux command:您还可以使用“超时” linux 命令:

timeout 5 cliprocess.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM