To execute scripts parallelly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To execute scripts parallelly
# 1  
Old 05-17-2014
To execute scripts parallelly

Hi I have set two set of scripts sets in a file which perform similar operations but with different script names for e.g.:
Code:
1st set of script1.txt:
1.sh
2.sh
3.sh
4.sh

2nd set of script2.txt:

1_1.sh
2_1.sh
3_3.sh
4_4.sh

I want to execute these set of scripts parallelly in such a way that it should wait for the dependent scripts to complete before the next set of scripts run

for eg:
1.sh and 1_1.sh should run parallelly and i have maintained a completion flag in each scripts which will result like 1.completed and 1_1.completed once each scripts completes
Once both of these flags are completed next set of scripts should run parallelly
2.sh and 2_2.sh.So like this subsequent scripts should run until unless both previous scripts completes alone.
i.e 2.sh and 2_2.sh should run only once 1.completed and 1_1.completed flag is created.

Last edited by rohit_shinez; 05-17-2014 at 09:18 AM..
# 2  
Old 05-17-2014
Here's a command line I ran that demonstrates one way to do what you're talking about.

Code:
for fn in c11 c12 c13 c14; do ${fn} & done; wait; echo "First set finished"; for fn in x11 x12 x13 x14; do ${fn} & done; wait; echo "Second set finished"

The idea is to spawn background processes in batches, with "&", then use the "wait" command to pause until they are all done.

If you want to try it exactly as I ran it, the scripts are all just symlink'd to this code.

Code:
#!/bin/bash

WAIT=$(( 2 + ( ${RANDOM} % 5 ) ))

echo "Run $0 - wait ${WAIT}"

sleep ${WAIT}

echo "Done $0"

I just wanted them to finish in random order to show that "wait" really does block until all the child processes are done.
# 3  
Old 05-17-2014
How about something like:
Code:
while read script1 && read script2 <&3
do
  "$script1" &
  "$script2" &
  wait
done < script1.txt 3<script2.txt

Variable script1 is read from file descriptor 0 (stdin), which is assigned to script1.txt, as defined at the bottom of the loop.
Variable script2 is read from file descriptor 3, which is assigned to script2.txt (also defined at the bottom of the loop).

Last edited by Scrutinizer; 05-18-2014 at 04:56 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-18-2014
Hi scrut,

Can it possible to excute the scripts from particular directory path becoz each script suits belong to different path for eg 1.sh is present in $direct1 and 1_1.sh in $direct2

Code:
 
$direct1/script1 &
$direct2/script2 &

# 5  
Old 05-18-2014
Sure you can, this is just the principle. You can adjust it anyway you like, for example
Code:
Direct1=/path/to/foo
Direct2=/path/to/bar

while read script1 && read script2 <&3
do
  "$Direct1/$script1" &
  "$Direct2/$script2" &
  wait
done < script1.txt 3<script2.txt

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-18-2014
Hi scrut,

In your code if any of two parallel script fails due to any reason it will not wait. i tested something like this making 1.sh executable without errors but 1_1.sh with errors. So in this case next set of scripts i.e 2.sh and 2_1.sh should not run. For this reason i maintained a flag in each scrips like touching 1.completed and 1_1.completed files until this is found you shouldn't start next set of scripts

Last edited by rohit_shinez; 05-18-2014 at 06:19 AM..
# 7  
Old 05-18-2014
Of course, it is just the principle. You can easily add code after the wait statement to test for those flags (or return codes if the scripts allow) and break the loop (with a break statement, for example) if there isn't a positive result..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Need ./ to execute scripts

Hi, I am really sorry for this question but still i am confused. I have shell script called sample.sh I can execute only with the combination of ./sample.sh Is ./ really necessary ? Is it something related with $HOME or $PATH variable. Why and How can i resolve this case ? ... (2 Replies)
Discussion started by: Nandy
2 Replies

2. Shell Programming and Scripting

Executes scripts parallelly based on their success

Hi Team , I have one Master.sh file which call X,Y,Z scripts , but here X may call again some sub scripts X_sub1.sh , X_sub2.sh Y calls Y_sub1.sh,Y_sub2.sh and similarly Z script also . Now requirement is Both X and Y should execute parallel bcz X and Y are independent... (9 Replies)
Discussion started by: chandini
9 Replies

3. Shell Programming and Scripting

Execute scripts in Parallel

Hi I want to execute few scripts in Parallel. There is a Master Script (MS.ksh) which will call internally all the scripts we need to run in Parallel. Say there are three set of scripts : ABC_1.ksh --> ABC_2.ksh --> ABC_3.ksh (execute ABC_2 when ABC_1 is successful ; Execute ABC_3 only when... (6 Replies)
Discussion started by: dashing201
6 Replies

4. Shell Programming and Scripting

Need to execute different scripts with shell script

Hi All, Am in need of some inputs to overcome the following problem, A tar file(/var/execute/scripts/ that contains different types of scripts(may be perl,shell, python etc...). I have written a shell script which is located @ /var/execute.sh to untar the file in some location say... (1 Reply)
Discussion started by: SMNK
1 Replies

5. Shell Programming and Scripting

need to have a cronjob which will execute certain scripts every hr

Hi My question needs two answers how to write scripts to update a table in oracle db based on the result of the number of record counts for example i need to execute the following script every hour awk '{sum++;}END{for(i in sum) {print i, sum}}' filename here everyhour the... (3 Replies)
Discussion started by: aemunathan
3 Replies

6. Shell Programming and Scripting

Execute scripts from remote using telnet

Hi having the below code (sleep 1 echo "username" sleep 2 echo "password" sleep 2 echo "cd /home/test1/path" sleep 10 echo "ls -l | grep filename" if exists echo "script1.sh filename" echo "mailx -s "Task Executed" user@domain.com" else echo "mailx -s "File not Exist"... (1 Reply)
Discussion started by: karthikn7974
1 Replies

7. Shell Programming and Scripting

Need to execute 2 scripts, wait, execute 2 more wait, till end of file

:cool: I need to execute a shell script to do the following: cat a file run two back ground processes using the first two values from the file wait till those background processes finish run two more background processes using the next two values from the file wait till those background... (1 Reply)
Discussion started by: halo98
1 Replies

8. Shell Programming and Scripting

Running three scripts parallelly

Hi All, We have three shell script batch, which extract data from three different systems(oracle, db2, db2/400). By running each shell script batch, the data is extracted from respective systems. while the batch is running, job date, system_name, start_date and end_date will be inserted into... (1 Reply)
Discussion started by: anwarsait
1 Replies

9. Shell Programming and Scripting

How to execute scripts at logout?

Hi, We can execute scripts at X login time in Linux by placing scripts in /etc/X11/xinit/xinitrc.d/ directory. Can you suggest a method to execute scripts at X logout time? rgds, pcsaji (1 Reply)
Discussion started by: pcsaji
1 Replies

10. UNIX for Dummies Questions & Answers

How can I execute TCL scripts in HP-UX

Hi All, Since I want to execute TCL scripts in HP-UX, I cannot find where to start. I try to execute the following scripts. It gets "tclsh: not found." errors. Where / how to execute tcl scripts? Thanks a lots. #!/bin/sh # the next line restarts using wish \ exec tclsh "$0" "$@" (3 Replies)
Discussion started by: wilsonchan1000
3 Replies
Login or Register to Ask a Question