Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sequence in running script 1

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
AU
Hi guys,

I have the sequence script to run in my script :
Code:
Script1.ksh
Script2.ksh & Script3.ksh & Script4.ksh

# I need to wait until the whole Script2,3,and 4 to be finish first then run Script5

Script5.ksh

How do I do this guys using wait command ?
 
Unless any of the scripts Script2/3/4 start background processes then what you have will work fine

in ksh
Code:
script2 & script3 & script4
is better written
Code:
script2 && script3 && script4
assuming you want script3 to be dependent on the successful completion of script2 and script4 to be dependent on the successful completion of script3. Note that script5 will run whatever the exit status of script2/3/4. You may want to make sure that these scrips have proper exit statements. i.e
Code:
#!/bin/ksh
#script2
...
...
#All OK
exit 0

Ceci n'est pas une signature
Columb Healy
 
I think hokky actually wants to run Scripts2, 3 and 4 in parallel, then run Script5 when they have completed, so this may be the solution:

Code:
Script1.ksh
Script2.ksh &
Script3.ksh &
Script4.ksh &

# I need to wait until the whole Script2,3,and 4 to be finish
# first then run Script5
wait
Script5.ksh


Annihilannic.
 
Thanks Anni,

You get me.

Actually I sorted out as well, but a bit difference
Code:
LoadData(){
Script2.ksh & Script3.ksh & Script4.ksh &
wait
}

Script1.ksh
LoadData
Script5.ksh

Because I thought wait should be put one in function or one in script

so if there's any Script6.ksh running parallel with Script5.ksh, I can simply put like this :

Code:
Script1.ksh
Script2.ksh &
Script3.ksh &
Script4.ksh &

# I need to wait until the whole Script2,3,and 4 to be finish
# first then run Script5
wait
Script5.ksh &
Script6.ksh &
# I need to wait until Script5 and Script6 to be finish before quit the main script
wait
 
To automate a wait use jobs -p to try something like
Code:
script1 &
script2 &
script3 &

while :
do
  set -A arr $(jobs -p) # arr is now an array of background job process numbers
  [[ ${#arr[*]} -gt 0 ]] || break # break out of the loop if the array is empty i.e. all jobs have finished
  sleep 10 # wait 10 seconds before retrying
done

Ceci n'est pas une signature
Columb Healy
 
Err... hides face in shame, it's not!

Ceci n'est pas une signature
Columb Healy
 
The reason for my mid-week brainstorm was that the script fragment was lifted from a script which waited for a number of background processes to finish or a maximum wait time to expire.
The exact script is
Code:
TOT_WAIT=0
SHORT_WAIT=10
MAX_WAIT=300
while [ : ]
do
  set -A arr $(jobs -p)
  [[ ${#arr[*]} -gt 0 ]] || break;
  [[ $TOT_WAIT -gt $MAX_WAIT ]] && { kill ${arr[*]}; break; }
  sleep $SHORT_WAIT
  (( TOT_WAIT += $SHORT_WAIT ))
done;
which, I hope you agree, makes sense. Without the max wait aspect it becomes totally redundant.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top