Visit Our UNIX and Linux User Community


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?
# 1  
Old 10-14-2014
How to run scripts parallely inside shell script?

Hi ,

I have 4 scripts example script1,script2,script3,script4 .

I have to run script1,script2 and script3 parallely since this 3 scripts dont have dependencies .
Once script1,script2 and script3 got completed successfully , I have to trigger script4.

Can someone help me on this how to right script for this!!!

Thanks,
Vinoth
# 2  
Old 10-14-2014
What have you tried so far?
What scripting langauge?
# 3  
Old 10-14-2014
Quote:
Originally Posted by vinothsekark
Hi ,

I have 4 scripts example script1,script2,script3,script4 .

I have to run script1,script2 and script3 parallely since this 3 scripts dont have dependencies .
Once script1,script2 and script3 got completed successfully , I have to trigger script4.

Can someone help me on this how to right script for this!!!

Thanks,
Vinoth
Hi vinothsekark,

You can try as follows, not tested though.

Code:
#!/bin/bash
./script1 &
./script2 &
./script3 &
Q=1
 
While (Q==1)
do
PID1=`ps -ef | grep -v grep | grep "script1"`
PID2=`ps -ef | grep -v grep | grep "script2"`
PID3=`ps -ef | grep -v grep | grep "script3"`
 
 
sleep 10 ## you can give here number of seconds as per you wish as not sure about how long scripts wil be taking to complete##
 
if [[ $PID1 == "" && $PID2 == "" && $PID3 == "" ]]
then
       ./script 4
       Q=0
else
       Q=1
fi
done

kindly try it in a non live environment as it is not tested and let us know.

EDIT: Sorry sea seems we have updated the post on same time didn't see your question.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-14-2014 at 03:37 AM.. Reason: Adding sorry, as sea asked a question to OP+edited space thanks to sea
# 4  
Old 10-14-2014
@ RavinderSingh: No issues.
There is a space and numbering issue with $PID =="" -> $PID3 == ""
This User Gave Thanks to sea For This Post:
# 5  
Old 10-14-2014
The same could be achieved with
Code:
./script1 &
./script2 &
./script3 &
wait
./script4

However this wil also not test the return codes of the background scripts..
The challenge is to reliably capture those before script 4 can continue...

Something like this perhaps (not tested)
Code:
wrong=0
./script1 & ./script2 & ./script3 &
for job in $(jobs -p)
do
  wait $job || wrong=$((wrong+1))
done
if [ $wrong = 0 ]; then
  ./script4
fi


Last edited by Scrutinizer; 10-15-2014 at 02:24 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-14-2014
The capture whether the script is running or not - is easy, at least when compared to the challenge transporting the exit value out of the subshell to that this handler script knows it, which script has exited with which exit code.

I would have a solution for this at hand, but I'm afraid it works only with 1 background process at a time (that is pseudo-copied to below example).

Either way, it forces you to send a more complex command in the background process, adapt to all 3 files.
Code:
( ./script1 ; printf $? > ./script1_ret  )

Then catch the result of those ./script[1-3]_ret files within a loop, and it should be fine.

Hope this helps

EDIT:
Just for the challenge I've written a script, but not going to post until TO has posted his efforts.

Last edited by rbatte1; 10-14-2014 at 08:15 AM.. Reason: Spelling
# 7  
Old 10-14-2014
Came up with this alternative with bash, ksh93 or zsh, using the named pipes of process substitution.
Code:
{
  read rc1 <&3
  read rc2 <&4
  read rc3 <&5
} 3< <(./script1 >/dev/null 2>&1; echo $?) \
  4< <(./script2 >/dev/null 2>&1; echo $?) \
  5< <(./script3 >/dev/null 2>&1; echo $?) 
if [ $rc1 = 0 ] && [ $rc2 = 0 ] && [ $rc3 = 0 ] ; then
  ./script4
fi


Last edited by Scrutinizer; 10-15-2014 at 02:24 AM..
This User Gave Thanks to Scrutinizer For This Post:

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

Featured Tech Videos