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

我的梦境 提交于 2019-12-12 08:55:34

问题


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?

Edit: I am on CentOS 6, PHP 5.3


回答1:


Depends on the OS you are using.

For linux:

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

notice the amperstand at the end (very important).

For windows:

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

here the start keyword is important.

Hope this helps.




回答2:


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.

This way you can stack up your ffmpeg work in the background without blocking your webserver.

I've had a lot of success with both methods (cron / queue) in the past.

Some other posts about background processes:

php execute a background process

Run a ffmpeg process in the background

Using ffmpeg, PHP and beanstalk

Some tools you might find useful:

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

PEAR System_Daemon

Pheanstalk, a Beanstalkd library for PHP




回答3:


Well use an ajax request to activate the exec part ...then continue with the other tasks




回答4:


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.




回答5:


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
}


来源:https://stackoverflow.com/questions/8910055/executing-an-exec-or-system-in-php-and-do-not-wait-for-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!