David Yell David Yell - 1 year ago 243
PHP Question

shell_exec() timeout management & exec()

I'm running a third party script by using a wrapper class I've written which calls

and pipes into a file I parse later using php code. I should mention that this is working, but I am trying to enhance the functionality, having encountered a use case I hadn't thought of.

How is it best to manage timeout on shell_exec()? I was thinking of wrapping it in a
try() catch()
but I'm not sure how to best handle the time component.

I've been reading a few questions on here relating to
and it seems that by passing output params to
you can get a return, but that does rely on the script finishing with a return status. Plus in my mini test page, I can't seem to get it to return any output!

The other option I thought about was using a modal dialog, with an ajax style spinner whilst the script it working, and setting a manual timeout in javascript. Which then gave the user a model dialog message about it failing/timeout and ending.

Are there any accepted methods for this use case?

My mini test, consisted of the following,

public $e_return = array();
public $e_status = '';
// Paths are absolute from /
public function execCheck($domain){
exec($this->ssl_check_path." -s ".$domain." -p 443 > ".$this->folder.$this->filename." 2>&1 &", &$this->e_return, &$this->e_status);

// Returns


Using this question as ref,


Answer Source

I would suggest you look into using proc_open. You can configure it to return a stream resource, manually keep a timer, and if the timer expires before the process completes, you can terminate it with proc_terminate. If it does complete before the timer expires, then you can use proc_close then stream_get_contents to grab the data that would have otherwise been written to stdout.

See http://www.php.net/manual/en/function.proc-open.php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download