Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
426 views
Welcome To Ask or Share your Answers For Others

1 Answer

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]

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...