2010-08-11 73 views
2

我想用php的exec命令執行多個shell命令。這些命令必須按順序進行,我想用nohup來啓動它們,以便它可以在後臺運行,並且我的php腳本不必等待它完成。 這裏是我的腳本:使用php執行多個shell命令nohup

$command1="nohup convert -trim +repage pic1.png pic2.png; convert pic2.png -thumbnail 500x10000 pic3.png; convert pic2.png -resize 115x1000 -gravity Center -crop 115x196+0+0 +repage pic4.png; rm pic1.png; rm pic2.png > /dev/null 2> /dev/null & echo $"; 
$output = exec($command1 . ' 2>&1', $output, $return); 

正如你所看到的,它需要依次是因爲我想編輯之前已經修剪的照片。命令本身和順序部分運行良好,但nohup在整個$ command1中不起作用。我不確定它是否實際上沒有做任何事情,或只是在最後一個命令(rm pic2.png)上工作。

任何幫助,非常感謝。 謝謝

+0

'nohup'能很好地工作分號:請參閱http://people.ucalgary.ca/~wellings/tipspit/nohup.html您可以使用不同的命令創建測試用例(如創建五個後續文件)?另外,我認爲如果其中一個命令返回一個錯誤,其他命令將不會被執行(雖然不知道,但Linux專家需要確認) – 2010-08-11 09:29:18

+0

我認爲它也可以工作(它工作正常用一個命令W/O分號),但它實際上並沒有... 所以我的PHP腳本等待命令完成,而不是像以前繼續... 順便說一句,我認爲你是對的關於以下錯誤時不執行命令。 – Michael27 2010-08-11 09:56:32

回答

2

我解決了我的問題 - 即使它是一種解決方法。

我把shell腳本到批處理腳本,這是我從PHP調用使用exec("nohup batch.sh > /dev/null 2>&1 &");

在批處理腳本我執行這樣的shell命令:nohup convert -trim +repage pic1.png pic2.png && nohup convert pic2.png -thumbnail 500x10000 pic3.png &

效果很好!

+0

如何在不依賴shell腳本的情況下執行此操作? – knocte 2012-09-19 11:51:18

0

我一直在問自己這個多年,但我總覺得有沒有其他解決辦法不是將所有命令到一個腳本並執行它想:nohup . ./multiplecommands.sh &

我不知道爲什麼我沒有早嘗試,但至少這個小小的測試工作得很好。 並且在發生錯誤後shell不會停止。 如果更換;通過&&我認爲它會雖然

blup$ nohup echo ett > ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon ; echo tvaa >> ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon ; echo tre >> ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon ; echo fyra >> ~/tmptypo/nohup_on_multiple_commands_test_sep_thru_semicolon ; echo fem >> ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon ; cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon & 
nohup: ignoring input and redirecting stderr to stdout 
bash: /home/blubber/tmptypo/nohup_on_multiple_commands_test_sep_thru_semicolon: No such file or directory 
[3] 27136 
blub$ ett  #<-- this looks a bit ugly, though, but ignore the additional prompt now. 
tvaa 
tre 
fem 

[3]+ Done     cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon 
blub$ cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolonett 
tvaa 
tre 
fem 
blub$ 

ERGO:echo fyra未執行,但echo fem了。

AND = &&代替;

blub2$ nohup echo one > ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon && echo two >> ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon && echo threefour >> ~/tmptypo/nohup_on_multiple_commands_test_sep_thru_semicolon && echo five >> ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon ; cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon &nohup: ignoring input and redirecting stderr to stdout 
bash: /home/blubber2/tmptypo/nohup_on_multiple_commands_test_sep_thru_semicolon: No such file or directory 
[3] 28744 
blub$ one 
two 

[3]+ Done     cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon 
blub$ cat ~/tmp/nohup_on_multiple_commands_test_sep_thru_semicolon 
one 
two 
blub$ 

ERGO同樣的事情:如果你需要退出,之後失效,然後使用&&代替;