简体   繁体   English

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

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

I want to trigger a shell command in eider exec() or system() from PHP script but it is a task that take a while to complete, is there a way to trigger it and continue running the PHP page load without delay? 我想从PHP脚本中触发eider exec()或system()中的shell命令,但这是一个需要一段时间才能完成的任务,有没有办法触发它并继续运行PHP页面加载而没有延迟?

Edit: I am on CentOS 6, PHP 5.3 编辑:我在CentOS 6,PHP 5.3上

Depends on the OS you are using. 取决于您使用的操作系统。

For linux: 对于linux:

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

notice the amperstand at the end (very important). 注意最后的放大器(非常重要)。

For windows: 对于Windows:

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

here the start keyword is important. 这里start关键字很重要。

Hope this helps. 希望这可以帮助。

This doesn't answer your question directly, but you should consider doing your video conversion work in a background process with either a cron job or using a queue such as Beanstalkd. 这不能直接回答您的问题,但您应该考虑使用cron作业或使用Beanstalkd等队列在后台进程中进行视频转换。

This way you can stack up your ffmpeg work in the background without blocking your webserver. 这样,您可以在后台堆叠ffmpeg工作,而不会阻止您的Web服务器。

I've had a lot of success with both methods (cron / queue) in the past. 我过去在这两种方法(cron / queue)上取得了很大的成功。

Some other posts about background processes: 关于后台流程的其他一些帖子:

php execute a background process php执行后台进程

Run a ffmpeg process in the background 在后台运行ffmpeg进程

Using ffmpeg, PHP and beanstalk 使用ffmpeg,PHP和beanstalk

Some tools you might find useful: 您可能会发现一些有用的工具:

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

PEAR System_Daemon PEAR System_Daemon

Pheanstalk, a Beanstalkd library for PHP Pheanstalk,一个用于PHP的Beanstalkd库

What I do: 我所做的:

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
}

好吧,使用ajax请求激活exec部分......然后继续执行其他任务

This should work: 这应该工作:

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

Edit: sorry, dunno why I excluded the & to put it to bg 2> redirects STDOUT and STDERR to /dev/null. 编辑:对不起,不知道我为什么要排除&把它放到bg 2>将STDOUT和STDERR重定向到/ dev / null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM