php中带超时参数的exec()?

php中带超时参数的exec()?,php,linux,timeout,exec,Php,Linux,Timeout,Exec,我有两个带超时参数的exec()函数。 例如: exec-执行外部程序 string exec ( string $command [, array &$output [, int &$return_var ]] ) exec() executes the given command. 改为使用此功能-有关更多信息,请阅读 函数PsExecute($command,$timeout=5,$sleep=2){ //首先,执行流程,获取流程ID $pid=PsExec($comm

我有两个带超时参数的exec()函数。 例如:


exec-执行外部程序

string exec ( string $command [, array &$output [, int &$return_var ]] )

exec() executes the given command.

改为使用此功能-有关更多信息,请阅读

函数PsExecute($command,$timeout=5,$sleep=2){
//首先,执行流程,获取流程ID
$pid=PsExec($command);
如果($pid==false)
返回false;
$cur=0;
//第二,循环$timeout秒,检查进程是否正在运行
而($cur<$timeout){
睡眠($睡眠);
$cur+=$sleep;
//如果进程不再运行,则返回true;
如果(!PsExists($pid))
return true;//进程必须已退出,成功!
}
//如果进程在超时后仍在运行,请终止该进程并返回false
PsKill($pid);
返回false;
}
string exec ( string $command [, array &$output [, int &$return_var ]] )

exec() executes the given command.
 function PsExecute($command, $timeout = 5, $sleep = 2) {
        // First, execute the process, get the process ID
        $pid = PsExec($command);

        if( $pid === false )
            return false;

        $cur = 0;
        // Second, loop for $timeout seconds checking if process is running
        while( $cur < $timeout ) {
            sleep($sleep);
            $cur += $sleep;
            // If process is no longer running, return true;
            if( !PsExists($pid) )
                return true; // Process must have exited, success!
        }

        // If process is still running after timeout, kill the process and return false
        PsKill($pid);
        return false;
    }