shell_exec()超时管理& EXEC()

时间:2010-08-04 17:07:05

标签: php shellexecute

我正在使用我编写的包装类运行第三方脚本,该类调用shell_exec()并将管道导入我稍后使用php代码解析的文件。我应该提一下这是有效的,但我正在尝试增强功能,遇到了一个我没想过的用例。

如何在shell_exec()上管理超时?我想把它包装在try() catch()中,但我不知道如何最好地处理时间组件。

我一直在阅读有关shell_exec()exec()的几个问题,似乎通过将输出参数传递给exec(),您可以获得回报,但这确实依赖在脚本上完成返回状态。在我的迷你测试页面中,我似乎无法让它返回任何输出!

我想到的另一个选项是使用模态对话框,使用ajax样式微调器,同时使用它运行的脚本,并在javascript中设置手动超时。然后,它给用户一个关于它失败/超时和结束的模型对话框消息。

此用例是否有任何可接受的方法?

我的迷你测试,包括以下内容,

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
Array
(
)

0

将此问题用作ref,  Can't execute PHP script using PHP exec

http://www.php.net/manual/en/function.exec.php

4 个答案:

答案 0 :(得分:16)

我为这样的任务写了一些工作代码。函数返回退出代码(0 - 确定,> 0 - 错误)并将stdout,stderr写入引用变量。

/*execute program and write all output to $out
terminate program if it runs more than 30 seconds */
execute("program --option", null, $out, $out, 30);
echo $out;

function execute($cmd, $stdin=null, &$stdout, &$stderr, $timeout=false)
{
    $pipes = array();
    $process = proc_open(
        $cmd,
        array(array('pipe','r'),array('pipe','w'),array('pipe','w')),
        $pipes
    );
    $start = time();
    $stdout = '';
    $stderr = '';

    if(is_resource($process))
    {
        stream_set_blocking($pipes[0], 0);
        stream_set_blocking($pipes[1], 0);
        stream_set_blocking($pipes[2], 0);
        fwrite($pipes[0], $stdin);
        fclose($pipes[0]);
    }

    while(is_resource($process))
    {
        //echo ".";
        $stdout .= stream_get_contents($pipes[1]);
        $stderr .= stream_get_contents($pipes[2]);

        if($timeout !== false && time() - $start > $timeout)
        {
            proc_terminate($process, 9);
            return 1;
        }

        $status = proc_get_status($process);
        if(!$status['running'])
        {
            fclose($pipes[1]);
            fclose($pipes[2]);
            proc_close($process);
            return $status['exitcode'];
        }

        usleep(100000);
    }

    return 1;
}

答案 1 :(得分:7)

我建议您考虑使用proc_open。您可以将其配置为返回流资源,手动保留计时器,如果计时器在进程完成之前到期,则可以使用proc_terminate终止它。如果它在计时器到期之前完成,那么您可以使用proc_close然后使用stream_get_contents来获取本来写入stdout的数据。

请参阅http://www.php.net/manual/en/function.proc-open.php

答案 2 :(得分:0)

我尝试使用popen(),但之后无法终止进程。 此外,即使在Windows上使用stream_set_blocking,stream_get_contents()也会阻止流,因此我不得不使用fread。此外,proc_terminate在Windows上无法正常工作,因此我不得不使用替代的kill函数。

我已经提出了这个问题,它现在应该在Windows和Linux上运行:

function execute($command, $timeout = 5) {
    $handle = proc_open($command, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipe);

    $startTime = microtime(true);

    /* Read the command output and kill it if the proccess surpassed the timeout */
    while(!feof($pipe[1])) {
        $read .= fread($pipe[1], 8192);
        if($startTime + $timeout < microtime(true)) break;
    }

    kill(proc_get_status($handle)['pid']);
    proc_close($handle);

    return $read;
}

/* The proc_terminate() function doesn't end proccess properly on Windows */
function kill($pid) {
    return strstr(PHP_OS, 'WIN') ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid");
}

答案 3 :(得分:0)

如果您使用的是Linux(或Windows 10中的WSL),则使用timeout命令似乎是最简单的方法。 看到这个答案: exec() with timeout