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!

Checking multiple processes has ended

Status
Not open for further replies.

Dudek

Technical User
Jan 14, 2002
27
MY
Hi there again..

Another question

I have a script that runs 3 seperate scripts like below:

script1.sh &
script2.sh &
script3.sh &

it pushes it all to the background. I need to run a script script4.sh that would start after ALL THREE scripts script1.sh script2.sh and script3.sh has been completed. Is that possible? And how do i do that?

TIA

JD
 
One method would be to have scripts 1, 2 and 3 create a temporary file once they have completed. You would run them from your main script in the background as normal, and then have some form of loop which checks for the presence of the 3 temporary files. If they exist, then run script 4. You would also need some form of "get-out" in case the 3 scripts don't finish properly.

This is a pseudo-ksh example ...

script1.sh &
script2.sh &
script3.sh &

SLEEPCOUNT=0
SCRIPT4_RUN=0
while [ $SLEEPCOUNT -le 50 ]
do
if [ -s script1.tmp ] && [ -s script2.tmp] && [ -s script3.tmp ]
then
script4.sh
SCRIPT4_RUN=1
break
fi
sleep 60
SLEEPCOUNT=$((SLEEPCOUNT + 1))
done

if [ $SCRIPT4_RUN = 1 ]
then
echo "script4 ran successfully"
else
echo "script4 did not run"
fi

Greg.
 
If you don't need the exit statuses from your background processes you could do this

script1.sh &
script2.sh &
script3.sh &

wait # wait for all background processes to finish

script4.sh
Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Just use "wait" in your ORIGINAL script

script1.sh &
script2.sh &
script3.sh &
wait

Or, if that doesn't work for you, pickup the pid of each
background process with $! and wait individually:

script1.sh &
FIRST=$!
script2.sh &
SECOND=$!
script3.sh &
THIRD=$!
# do more stuff
script4 &
wait $SECOND
script5 &
wait $THIRD
wait $FIRST

etc. Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top