Reputation: 69
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
Upvotes: 2
Views: 3800
Reputation: 1
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 ;
Upvotes: 0
Reputation: 69
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!
Upvotes: 3