exec()与超时 [英] exec() with timeout
问题描述
我正在寻找一种可以超时运行PHP进程的方法.目前,我只是在使用 exec()
,但它不提供超时选项.
I'm looking for a way to run a PHP process with a timeout. Currently I'm simply using exec()
, but it does not provide a timeout option.
我还尝试使用 proc_open()
并使用
What I also tried is opening the process using proc_open()
and using stream_set_timeout()
on the resulting pipe, but that didn't work either.
那么,有什么方法可以在超时的情况下运行命令(准确地说是PHP命令)? (PS:这是针对max_execution_time
限制失败的情况,因此无需建议.)
So, is there any way to run a command (a PHP command to be precise) with a timeout? (PS: This is for cases where the max_execution_time
limit fails, so no need to suggest that.)
(顺便说一句,我还需要检索该过程的返回码.)
(By the way, I also need to retrieve the return code of the process.)
推荐答案
我在这个主题上进行了一些搜索,得出的结论是,在某些情况下(如果使用的是Linux),可以使用超时"命令.相当灵活
I've searched a bit on this topic and came to conclusion that in some case (if you are using linux) you can use 'timeout' command. It's pretty flexible
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
or: timeout [OPTION]
在我的特殊情况下,我试图从PHP运行sphinx索引器,有点像迁移数据脚本,所以我需要为sphinx文档重新索引
in my particular case I'm trying to run sphinx indexer from PHP, kinda migration data script so I need to reindex my sphinx documents
exec("timeout {$time} indexer --rotate --all", $output);
然后,我将分析输出并决定再尝试一次,或者抛出异常并退出脚本.
Then I'm going to analyze output and decide to give it one more try, or throw an exception and quit my script.
这篇关于exec()与超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!