使用 php nohup 执行多个 shell 命令

发布于 2024-09-13 19:53:34 字数 600 浏览 9 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2024-09-20 19:53:35

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

使用 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!

戴着白色围巾的女孩 2024-09-20 19:53:35

多年来我一直问自己这个问题,但我一直认为除了将所有命令放入脚本中并像这样执行它之外没有其他解决方案: 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:如果您需要失败后退出,则使用 && 而不是 ;

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 though

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 wasn't executed, but echo fem was.

The same thing with AND = && instead of ;

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: if you need the quit-after-fail then use && instead of ;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文