cancel
Showing results for 
Search instead for 
Did you mean: 

Parallel jobs

SOLVED
Go to solution
Prabhu_7
Frequent Advisor

Parallel jobs

Hi,

I have 4 shell scripts which has to start parallely (at same time).

I can put all the scripts in
cron and set same time to start running.

Is there any other method to do this? Which is the best method to do the same.

Raj

7 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Parallel jobs

Hi Raj:

How about launching the scirpts in the background from one common script (cron'ed or otherwise).

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: Parallel jobs

try this

#! /usr/bin/ksh
script1 & ; script2 & ; script3 & ; script4 &

All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Parallel jobs

you just beat me James :)
All paths lead to destiny
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Parallel jobs

I will add one thing to this:

The typical approach is to have the parent process wat until all the child processes are finished and then let the parent exit. You then have an easy way of knowing that the jobs have finished without having to run ps'es.

#!/usr/bin/sh

myscript1.sh &
myscript2.sh &
myscript3.sh &
myscript4.sh &
wait
STAT=${?}
exit ${STAT}

The wait without arguments will pause until all the background tasks have finished.
If it ain't broke, I can fix that.
Prabhu_7
Frequent Advisor

Re: Parallel jobs

Yes ,

I want the process to wait until all (4) my process gets over and then send mail for me.

But just a "wait" will hold process until all background jobs to get over right.
There might be many more background process running on same server at same time.
So i guess my process will wait for all background process. AM i right ?

Then should i get PID for all jobs seperately and write sepearte wait also for each PID ?

Anybody got piece of code?
Requirement is

main.sh
scr1.sh &
scr2.sh &
scr3.sh &
scr4.sh &
wait untill above 4 process get over then
sendmail.sh
James R. Ferguson
Acclaimed Contributor

Re: Parallel jobs

Hi (again) Raj:

The sample code Clay provided will accomodate your 'wait' until all background processes are done. If you want to get a bit more sophisticated consider this:

#!/usr/bin/sh
typeset LOG=/tmp/logger.$$
trap 'rm ${LOG}' EXIT
exec 1> ${LOG} 2>&1
{ sleep 3; cp; } &
PID1=$!
{ sleep 7; date; } &
PID2=$!
{ sleep 1; mv; } &
PID3=$!
wait ${PID1}; R1=$?; echo "PID1 was ${PID1} with return code=${R1}"
wait ${PID2}; R2=$?; echo "PID2 was ${PID2} with return code=${R2}"
wait ${PID3}; R3=$?; echo "PID3 was ${PID3} with return code=${R3}"
mailx -s "Test Results" root < ${LOG}
exit 0

As written the script runs three tasks in the background. Process-1 and process-3 will exit with a return code of <1> since they are written to fail. Process-2 will exit with a successful value of <0>.

The script itself can be executed in the background. All stdout and stderr from the processes are written to a file named uniquely with the parent pid ($$).

Upon completion, the script mails the output to 'root' and removes the temporary file of its results.

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: Parallel jobs

the wait command - as Clay suggested - will work. the & will wait until all commands "in the current shell" have completed. When you start a script with
#! /usr/bin/ksh
it runs in it's own shell. hence the wait command will wait for only the things that your script started to end.
All paths lead to destiny