Question

I am trying to merge multiple linux commands in one line to perform deployment operation. For example

nohup php 1.php
nohup php 2.php
nohup php 3.php
nohup php 4.php

I want perform all of them in parallel, it is possible in a .sh file?

Was it helpful?

Solution 3

you can also do-it like that :

nohup sh -c 'php 1.php; php 2.php; php 3.php' &

edit : to answer your question, the process are parallel. you can verify this by writing the ps command. eg : with the sleep command :

nohup sh -c 'sleep 30 && sleep 30' &

output :

 ....
 6920    7340    7340       6868  pty2    17763 16:33:27 /usr/bin/sleep
 6404    4792    4792       7004  pty2    17763 16:33:26 /usr/bin/sleep
 ....

edit 2 : Ok then try with parallel command. (you probably have to install it)

Create a file cmd.txt :

1.php
2.php
3.php

Then execute this command (haven't tried yet, but it should work). you can change the --max-procs numbers if you have more/less than 4 core :

cat cmd.txt | parallel --max-procs=4 --group 'php {}'

hope it works...

OTHER TIPS

In linux you can use && to execute commands sequentially, and a command will only execute if the previous one succeeded.

nohup php 1.php && nohup php 2.php && nohup php 3.php

Edit: in case you do not want the error-checking provided by the && operator, use the semicolon (;) to chain the commands, like this:

nohup php 1.php ; nohup php 2.php ; nohup php 3.php

If you don't mind putting them into the background, you can do that by putting

  & ;     

between the 1st/2nd, and 2nd/3rd. This will execute the 3 essentially in parallel.

&: execute in bg
; do 2nd command irrespective of success of 1st

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top