使用php启动shell脚本并退出而不停止进程
问题描述:
我的问题可能有点复杂。使用php启动shell脚本并退出而不停止进程
这是我的场景:
我想使用php启动mjpg流光标。我的PHP脚本是:
<?php
$output = shell_exec("sh /var/www/html/shellstart.sh");
echo "<pre>$output</pre>";
header("Location: ../index.php");
?>
它在启动脚本时正常工作。问题是,该脚本
shellstart.sh:
LD_LIBRARY_PATH=/opt/mjpg-streamer/mjpg-streamer-experimental/ /opt/mjpg-streamer/mjpg-streamer-experime $
启动过程和header()函数永远不会被调用,因为它从来没有离开脚本。所以我正在寻找一种方法来启动脚本,让它继续运行,但返回到PHP脚本并调用header()函数。
我已经尝试过
trap "exit" SIGINT
和
nohup LD_LIBRARY_PATH=/opt/mjpg-streamer/mjpg-streamer-experimental/ /opt/mjpg-streamer/mjpg-streamer-experime$
也是我试过$断绝关系。但是当使用nohup或$ disown时,脚本甚至不会启动该过程。
我感谢任何帮助!提前致谢。
答
你想尝试类似:
$pid = exec("sh /var/www/html/shellstart.sh> /dev/null 2>&1 & echo $!; ", $output);
这将使得脚本在后台启动,并返回脚本的PID。
+0
这就像一个魅力!我会研究这是如何工作的。非常感谢你! – kingardox
可能与http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php重复 – IeuanG