shell_exec()超时管理和exec() [英] shell_exec() timeout management & exec()

查看:390
本文介绍了shell_exec()超时管理和exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用编写的包装器类来运行第三方脚本,该包装器类调用shell_exec()并将其输送到稍后使用php代码解析的文件中.我应该提到这是行得通的,但是由于遇到了我从未想到的用例,我正在尝试增强功能.

I'm running a third party script by using a wrapper class I've written which calls shell_exec() 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.

如何最好地管理shell_exec()上的超时?我当时想将其包装在try() catch()中,但不确定如何最好地处理时间分量.

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.

我一直在阅读有关shell_exec()exec()的一些问题,似乎通过将输出参数传递给exec()可以得到回报,但这确实取决于脚本以返回状态.另外,在我的迷你测试页中,我似乎无法让它返回任何输出!

I've been reading a few questions on here relating to shell_exec() and exec() and it seems that by passing output params to exec() 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!

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

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

0

使用此问题作为参考, 无法使用PHP exec执行PHP脚本

Using this question as ref, Can't execute PHP script using PHP exec

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

推荐答案

我建议您考虑使用proc_open.您可以配置它以返回流资源,手动保留一个计时器,如果计时器在处理完成之前到期,则可以使用proc_terminate终止它.如果确实在计时器到期之前完成,则可以使用proc_close然后使用stream_get_contents来抓取本来应该写入stdout的数据.

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.

请参见 http://www.php.net/manual/zh/function.proc-open.php

这篇关于shell_exec()超时管理和exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆