在PHP中执行exec()或system(),并且不等待输出


Executing an exec() or system() in PHP and do not wait for output

我想从PHP脚本在eider exec()或system()中触发一个shell命令,但这是一项需要一段时间才能完成的任务,有没有办法触发它并立即继续运行PHP页面加载?

编辑:我在CentOS 6,PHP 5.3

取决于您使用的操作系统。

对于linux:

pclose(popen("php somefile.php &","r"));

注意末尾的符号(非常重要)。

对于窗口:

pclose(popen("start php.exe somefile.php","r"));

这里start关键字是重要的。

希望这能有所帮助。

这不会直接回答您的问题,但您应该考虑在后台进程中使用cron作业或使用Beanstalkd等队列进行视频转换工作。

通过这种方式,您可以在后台堆叠您的ffmpeg工作,而不会阻塞您的Web服务器。

过去,我在这两种方法(cron/queue)上都取得了很大成功。

其他一些关于后台流程的帖子:

php执行后台进程

在后台中运行ffmpeg进程

使用ffmpeg、PHP和豆茎

你可能会发现一些有用的工具:

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

PEAR系统_Daemon

Pheanstalk,用于PHP 的Beanstalkd库

我的工作:

public function post_create()
{
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(); // optional
    ob_start();
    echo "Tell ajax to gtfo!";
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush(); // Strange behaviour, will not work
    flush();            // Unless both are called !
    // Do processing here
}

这应该有效:

shell_exec("nohup yourcommand > /dev/null 2> /dev/null &");

编辑:对不起,不知道为什么我排除了&把它放到bg2> 将STDOUT和STDERR重定向到/dev/null。

我们使用ajax请求来激活exec部分。。。然后继续执行其他任务