How to run scripts parallely inside shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run scripts parallely inside shell script?
# 8  
Old 10-14-2014
I didn't have any issues with this approach using bash and ksh:

Code:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!

wait $p1 ; rc1=$?
wait $p2 ; rc2=$?
wait $p3 ; rc3=$?

# Some debug output
printf "Job 1 (%d): %d\n" $p1 $rc1
printf "Job 2 (%d): %d\n" $p2 $rc2
printf "Job 3 (%d): %d\n" $p3 $rc3

if [ $rc1 = 0 ] && [ $rc2 = 0 ] && [ $rc1 = 0 ]
then
    ./script4
fi

# 9  
Old 10-14-2014
Quote:
Originally Posted by Chubler_XL
I didn't have any issues with this approach using bash and ksh:

Code:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!

wait $p1 ; rc1=$?
wait $p2 ; rc2=$?
wait $p3 ; rc3=$?

# Some debug output
printf "Job 1 (%d): %d\n" $p1 $rc1
printf "Job 2 (%d): %d\n" $p2 $rc2
printf "Job 3 (%d): %d\n" $p3 $rc3

if [ $rc1 = 0 ] && [ $rc2 = 0 ] && [ $rc1 = 0 ]
then
    ./script4
fi

Easier with bitwise-or:

Code:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!

# once rc goes non-zero, it stays that way
wait $p1 ; rc=$?
wait $p2 ; rc=$(( $rc | $? ))
wait $p3 ; rc=$(( $rc | $? ))


if [ $rc = 0 ]
then
    ./script4
fi

It's not a big deal for 3 scripts. But if you spawned 30 or so...

You do lose the ability to tell which script failed though.
# 10  
Old 10-15-2014
Quote:
Originally Posted by achenle
Easier with bitwise-or:

Code:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!

# once rc goes non-zero, it stays that way
wait $p1 ; rc=$?
wait $p2 ; rc=$(( $rc | $? ))
wait $p3 ; rc=$(( $rc | $? ))


if [ $rc = 0 ]
then
    ./script4
fi

It's not a big deal for 3 scripts. But if you spawned 30 or so...

You do lose the ability to tell which script failed though.
If you don't care which ones fail, there is no need to keep track of the individual return codes at all:
Code:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!

# Execute script4 only if all other scripts complete successfully:
wait $p1 && wait $p2 && wait $p3 && ./script4
rc=$?

# Wait for remaining children if script1 or script2 failed:
wait
exit $rc

Note that the shell is only required to keep track of CHILD_MAX processes and the standards say CHILD_MAX can be as small as 25. So, if you want to keep track of more than 25 background processes, verify that your shell can keep track of all of them by checking the CHILD_MAX limit on your system with the command:
Code:
getconf CHILD_MAX

Also note that if you have CHILD_MAX processes running, attempts to start any more processes (foreground or background) will fail.
# 11  
Old 10-15-2014
@ Scrunitizer: Seriously?
Thats nice and so small.

Guess i like writing 'gui's way too much...
That makes those scripts always so immense :P
How to run scripts parallely inside shell script?-demoscriptjpg

Last edited by sea; 10-15-2014 at 06:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

2. Shell Programming and Scripting

Run multiple procedures from shell script parallely

Hi, I need to write a Shell Script wherein i will connect to a DB2 Database and run multiple DB procedures. I know how to do in a way where procedures will be called one after the other, like when first procedure finishes, second will be executed. But what i want is to run them at the same time... (11 Replies)
Discussion started by: Neelkanth
11 Replies

3. Shell Programming and Scripting

Run 2 shell scripts simultaneously from one script

i Run 2 scripts on all of around 50 nodes every day. 1.Mod_1.sh 2.Mod_2.sh eg.. i run file with specific node no like Mod_1.sh NODE_(node number) Mod_2.sh NODE_(node number) I want to run both file by using single script with unique node number. Eg.. Mod_new.sh NODE_(node... (11 Replies)
Discussion started by: Ganesh Mankar
11 Replies

4. Shell Programming and Scripting

Run .exe inside shell script

I have to run some shell scripts in Windows using Cygwin. I am able to achieve that using %BASH% --login -i "/cygdrive/d/script.sh" , where %BASH% is an environment variable in Windows set to C:\cygwin\bin\bash.exe. I have a created a Cygwin environment variable $EXE_PATH =... (3 Replies)
Discussion started by: HemanthJayasimh
3 Replies

5. Shell Programming and Scripting

Shell script to run all the python scripts from particular directory

I have N number of python scripts which i am receiving from REST web server and i am saving them in /home/mandar/ . Now i want to write a script to run all the python scripts from this directory and monitor them every minute...if any process is dead or hung it should be restarted by this script. ... (0 Replies)
Discussion started by: Mandar Nandale
0 Replies

6. Shell Programming and Scripting

Multiple Threads/Tasks to run parallely using the shell script

Scenario: I have two PCs (named as A & B) which would send some traps to my third PC (named as C). In PC C, I have to write a shell script such that it should accept the datas from both the PC-A & B parallely. So my question is, is it possible to have two different child threads/tasks... (2 Replies)
Discussion started by: nthiruvenkatam
2 Replies

7. Shell Programming and Scripting

Run a script parallely with different arguments

Hi! I want to run a script in parallel with different arguments. eg. start script.sh argA script.sh argB script.sh argC end Can someone please tell how to achieve this. Thanks in advance. (4 Replies)
Discussion started by: dummyix
4 Replies

8. Shell Programming and Scripting

how to run shell script inside expect script?

I have the code like this : shell script continues ... .... expect -c" spawn telnet $ip expect "login:" send \"$usrname\r\" expect "Password:" send \"$passwd\r\" expect "*\>" send \"$cmdstr\r\" ... (1 Reply)
Discussion started by: robbiezr
1 Replies

9. Shell Programming and Scripting

How to run an SQL script inside a shell

How do I create a K Shell which would silently (without user input) logon to Oracle and run an SQL script? Any help will be greatly appreciated. Steve (1 Reply)
Discussion started by: stevefox
1 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question