使用 php nohup 执行多个 shell 命令
我想使用 php 的 exec 命令执行多个 shell 命令。这些命令必须按顺序执行,我想用 nohup 启动它们,这样它就可以在后台运行,并且我的 php 脚本不必等待它完成。 这是我的脚本:
$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)。
非常感谢任何帮助。 谢谢
I want to execute multiple shell-commands with php's exec command. The commands have to be made sequentially and I want to start them with nohup so it can run in the background and my php-script doesn't have to wait for it to finish.
Here's my 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);
As you can see, it needs to be sequentially because I want to edit a picture that has been trimmed before. The command in itself and sequential part work well, but the nohup doesn't work on the whole $command1. I am not sure if it actually doesn't do anything or just works on the last command (rm pic2.png).
Any help is greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了我的问题 - 尽管这是一种解决方法。
使用
exec("nohup batch.sh > /dev/null 2>&1 &");
从 php 调用该脚本我将 shell 脚本放入批处理脚本中,在批处理脚本中 我执行这样的 shell 命令: nohup Convert -trim +repage pic1.png pic2.png && nohup Convert pic2.png -thumbnail 500x10000 pic3.png &
效果很好!
I solved my problem - even though it's kind of a workaround.
I put the shell script into a batch script, which I call from php using
exec("nohup batch.sh > /dev/null 2>&1 &");
In the batch script I execute the shell commands like this:
nohup convert -trim +repage pic1.png pic2.png && nohup convert pic2.png -thumbnail 500x10000 pic3.png &
Works well!
多年来我一直问自己这个问题,但我一直认为除了将所有命令放入脚本中并像这样执行它之外没有其他解决方案:
nohup 。 ./multiplecommands.sh &
我不知道为什么我没有早点尝试一下,但至少这个小测试工作得很好。
并且 shell 在发生错误后不会停止。
如果您将
;
替换为&&
我认为它会ERGO:
echo fyra
未执行,但echo fem 是。
与
AND
=&&
相同,而不是;
ERGO:如果您需要失败后退出,则使用
&&
而不是;
I've asking myself this for years, but I always thought there's no other solution than putting all commands into a script and executing it like:
nohup . ./multiplecommands.sh &
I do not know why I didn't try it earlier, but at least this tiny test worked fine.
And the shell does not stop after an error.
If you replace the
;
by&&
I think it will thoughERGO:
echo fyra
wasn't executed, butecho fem
was.The same thing with
AND
=&&
instead of;
ERGO: if you need the quit-after-fail then use
&&
instead of;