用php在后台执行shell脚本

Jed*_*Jed 4 php linux shell

我需要执行一个shell脚本.抓住我想做到这一点

$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);
Run Code Online (Sandbox Code Playgroud)

我不希望它等到命令完成,我希望它在后台进程中运行.我不想要另一个php线程因为它会超时命令最多可能需要3个小时才能完成.

Edu*_*sso 8

您可以尝试使用如下函数在后台运行命令:

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

这使你的shell命令运行,但php流继续.


Mat*_*oli 7

$str = shell_exec($Command.' 2>&1 > out.log');
Run Code Online (Sandbox Code Playgroud)

您需要重定向命令的输出.

如果使用此函数启动程序,为了使其在后台继续运行,必须将程序的输出重定向到文件或其他输出流.如果不这样做将导致PHP挂起,直到程序执行结束.

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