2016-02-19 103 views
1

我的問題可能有點複雜。使用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時,腳本甚至不會啓動該過程。

我感謝任何幫助!提前致謝。

+0

可能與http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php重複 – IeuanG

回答

2

你想嘗試類似:

$pid = exec("sh /var/www/html/shellstart.sh> /dev/null 2>&1 & echo $!; ", $output); 

這將使得腳本在後臺啓動,並返回腳本的PID。

+0

這就像一個魅力!我會研究這是如何工作的。非常感謝你! – kingardox