使用php在后台执行shell脚本 [英] Executing a shell script in background with php

查看:80
本文介绍了使用php在后台执行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);

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

解决方案

$str = shell_exec($Command.' 2>&1 > out.log');

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

如果使用此功能启动程序,则要使其在后台继续运行,必须将程序的输出重定向到文件或其他输出流.否则,将导致PHP挂起,直到程序执行结束.

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

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

$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 dont want it to wait till the command is finished, i want it to run in a background process. I do not want another php thread as it will timeout the command can take up to 3 hours to finish.

解决方案

$str = shell_exec($Command.' 2>&1 > out.log');

You need to redirect the output of the command.

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

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

这篇关于使用php在后台执行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆