0

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?

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

The above should do this. If you check with 'ps' after, you will see 3 instances of php running (assuming they are long enough to take a few seconds at least). Processor handles that for you.
0

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...

5 Comments

it's work fine but one after one not parallel, how to run all files in parallel?
when I run the php files and get the uptput I see that the files run one after one and not in parallel...
@user3083317, read what i've wrote : you probably have to install it
and it's possible to prevent print of the output?
This should work, the outpout won't be printed on the screen and the process will run in background : sh -c "cat cmd.txt | parallel --max-procs=4 --group 'nohup php {}'&>/dev/null"& Any idea how to close the putty session after. maybe with exit command at the end, I cannot test right now. Anyway, if my answer helped you, mark as "resolved".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.