Running 3 shell script parallel in another shell script

Hi,

I'm trying to do teh below thing.

I have a single script which uses 3 different parameters to do 3 different work like belwo.

test1.sh par1 -- it shuts down an instance
test1.sh par2 -- it shuts down an instance
test1.sh par3 -- it shuts down an instance

Now I created a script test.sh where I'm calling the above one as below.

test1.sh par1 && test1.sh par2 && test1.sh par3

I think the above code will run the test1.sh with 3 different parameters in sequentially i.e. if first one completes then second in this way.

I want to run above three scripts in parallel not sequentially , so please suggest me how I can do this.

Thanks in advance.

You could do:

script1 arg1 arg2 &
script2 arg1 arg2 arg3 &

Which will send the scripts to a subshell/background job, so you can enter new commands - this is almost 'paralell'.

OR

I'd have a ready solution for you, which got inspired by http://www.unix.com/shell-programming-and-scripting/252023-how-run-scripts-parallely-inside-shell-script-2.html.
A detailed description to be found in: [BASH] Script to manage background scripts (running, finished, exit code)

Which became part of TUI : http://www.unix.com/shell-programming-and-scripting/253496-tui-text-user-interface-framework-scripts.html

Preview: https://raw.githubusercontent.com/sri-arjuna/tui/master/screenshots/tui-psm.jpg

However, it only execute scripts, without arguments.
So you would have to create for each script a script which calls the script with arguments.
Then pass those 'holding-scripts' to tui-psm.

Hope this helps

You could do this in test.sh

test1.sh par1 &
test1.sh par2 &
test1.sh par3 &

I don't know what you have in test1.sh but if it is a simple command to shutdown some process, you could call the command itself in a loop and background each.

Hi,

Thanks for the reply.
So I understand if I run below it will run the 3 scripts in parallel.

test1.sh par1 &
test1.sh par2 &
test1.sh par3 &

Now I have 2 questions.

How UNIX will call test1.sh par2 & after test1.sh par1 &.
test1.sh par1 & we are running in backgroud , so after kicking this script shall it automatically kick test1.sh par2 & without considering the first one to fail or success?

SEA -->

Why we are calling wait call in your example?
wait $p1 && wait $p2 && wait $p3 - Is it when p1 completes it will start p2 ...

Request you to please explain this.

Thnaks in advance.

Please use code tags as required by the forum rules.

We are not calling wait.
It executes the scripts in background, and 'wait' is used as intervall to check wether the script has started yet, is still running, or how it exited.

Change:

test1.sh par2 & after test1.sh par1 &

to:

(test1.sh par2 && test1 par1) &

To run the the par1 only if par2 ended successfully, if you want to run it anyway, use ; instead of && .

hth

EDIT: test1.sh par2 && test1 par1 could be the content of a file passed to tui-psm.

Sorry I don't understand you. If you want to run them in parallel you cannot wait for one to succeed before launching the next...

Parallel

test1.sh par1 &
test1.sh par2 &
test1.sh par3 &

Sequential

test1.sh par1 
test1.sh par2 
test1.sh par3 

Sequential only if previous succeeds

test1.sh par1 && test1.sh par2 && test1.sh par3 

Sure, if you let it only execute (set the limit to) one at a time.... it will do a sequence only / as well.

Just to be complete, if one tells the command the limit is 5, it will start t scripts at once in paralell.
And then check every few secs if one or more has ended.
So if 3 had ended, it can start another 3 at once.
If you set the limit to 1000, it could run a thousand scripst in paralell, but still would need to check every few secs, if some had ended and if there are remainings to start.

Its designed to work with more scripts, not just 3, but for some reason, 3 is always the question on the forum.

Also, its not (only) ment to run scripts in background, but also to let you know how they returned.

But guess/seems its an overkill for your needs.

Hi All,

Thanks for the reply.
I have still below confusion.

ust to be complete, if one tells the command the limit is 5, it will start t scripts at once in paralell.
And then check every few secs if one or more has ended.
So if 3 had ended, it can start another 3 at once.
If you set the limit to 1000, it could run a thousand scripst in paralell, but still would need to check every few secs, if some had ended and if there are remainings to start

When I'm running 3 scripts in parallel with below

test1.sh par2 & after test1.sh par1 &

where I'm defining

if one tells the command the limit is 5, it will start t scripts

at once in paralell.

Thnaks in advance.