2

我想用 php 的 exec 命令执行多个 shell 命令。命令必须按顺序执行,我想用 nohup 启动它们,这样它就可以在后台运行,而我的 php-script 不必等待它完成。这是我的脚本:

$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)上工作。

任何帮助是极大的赞赏。谢谢

4

2 回答 2

3

我解决了我的问题——尽管这是一种解决方法。

我将 shell 脚本放入一个批处理脚本中,我使用 php 从 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 &

效果很好!

于 2010-08-13T09:38:41.990 回答
0

多年来我一直在问自己这个问题,但我一直认为除了将所有命令放入脚本并执行它之外没有其他解决方案:nohup . ./multiplecommands.sh &

我不知道我为什么不早点尝试,但至少这个小测试运行良好。并且外壳在错误后不会停止。如果你替换;by&&我认为它会

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:如果您需要失败后退出,请使用&&而不是;

于 2014-01-29T16:09:18.233 回答