ejecutar múltiples comandos shell con php nohup
Pregunta
Quiero ejecutar múltiples comandos shell-con comando exec de php. Los comandos tienen que hacerse de forma secuencial y quiero empezar con nohup por lo que puede ejecutarse en segundo plano y mi php-script no tenga que esperar a que termine. Aquí está mi 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);
Como se puede ver, tiene que ser de forma secuencial porque quiero editar una imagen que ha sido recortado antes. El comando en sí mismo y el trabajo secuencial parte también, pero el nohup no funciona en todo el comando1 $. No estoy seguro de si en realidad no hace nada o solo funciona en el último comando (rm pic2.png).
Cualquier ayuda es muy apreciada. Gracias
Solución
He resuelto mi problema - a pesar de que es una especie de solución
. puse la secuencia de comandos shell en una secuencia de comandos por lotes, que llamo de php usando exec("nohup batch.sh > /dev/null 2>&1 &");
En la escritura de la hornada I ejecutar los comandos shell como esto: nohup convert -trim +repage pic1.png pic2.png && nohup convert pic2.png -thumbnail 500x10000 pic3.png &
funciona bien!
Otros consejos
He preguntándome esto durante años, pero siempre he pensado que no hay otra solución que poner todos los comandos en un script y ejecutarlo como: nohup . ./multiplecommands.sh &
No sé por qué no lo probamos antes, pero al menos esta pequeña prueba funcionó bien.
Y la cáscara no se detiene después de un error.
Si reemplaza el ;
por &&
Creo que va a pesar
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
no se ha ejecutado, pero era echo fem
Lo mismo con AND
= &&
en lugar de ;
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: si usted necesita el dejar de fumar, después de quebrar a continuación, utilizar &&
en lugar de ;