Running shell script in background using php

I need to execute a shell script. The trick I want to do is

$Command = "nohup cvlc input --sout '#transcode {vcodec=h264,acodec=mp3,samplerate=44100}:std{access=http,mux=ffmpeg{mux=flv},dst=0.0.0.0:8083/".output"}' &"; $str = shell_exec($Command); 

I do not want him to wait for the completion of the command, I want it to run in the background. I do not want another php stream, since it delays the command, it can take up to 3 hours.

+4
source share
2 answers
 $str = shell_exec($Command.' 2>&1 > out.log'); 

You need to redirect the output of the command.

If the program starts with this function so that it continues to work in the background, the output of the program should be redirected to a file or other output stream. Otherwise, PHP will hang until the program terminates.

http://php.net/manual/en/function.exec.php

+7
source

You can try running your command in the background using the following function:

 function exec_bg($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } 

This causes the shell command to start, but the php thread continues.

+7
source

All Articles