Executing exec () or system () in PHP and not waiting for exit

I want to call a shell command in eider exec () or system () from a PHP script, but it takes some time to complete this task, is there a way to start it and continue to start loading the PHP page without delay?

Edit: I'm on CentOS 6, PHP 5.3

+4
source share
5 answers

Depends on the OS used.

For linux:

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

pay attention to the rack at the end (very important).

For windows:

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

The start keyword is important.

Hope this helps.

+16
source

This does not answer your question directly, but you should consider doing your job of converting the video in the background process using either a cron job or a queue such as Beanstalkd.

This way you can stack your ffmpeg work in the background without blocking the web server.

I have had great success in both methods (cron / queue) in the past.

Some other background process messages:

php execute background process

Run the ffmpeg process in the background

Using ffmpeg, PHP and beanstalk

Some tools that may come in handy:

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

PEAR System_Daemon

Pheanstalk, Beanstalkd library for PHP

+4
source

It's good to use an ajax request to activate the exec part ... then continue with other tasks

+1
source

This should work:

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

Edit: sorry, I don’t know why I excluded and put it in bg 2> redirects STDOUT and STDERR to / dev / null.

+1
source

What am I doing:

 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 } 
+1
source

Source: https://habr.com/ru/post/1391629/


All Articles