1842655 Members
2424 Online
110194 Solutions
New Discussion

Scripting - jobs

 
SAM_24
Frequent Advisor

Scripting - jobs

Hi,
My requirement is submitting multiple jobs say 8 jobs at the same time and get the exit status of those jobs and proceed based on exit status. To acheive just I am testing how to get exit status using small script.

#!/bin/ksh
job1() {
/tmp/t1.sh &
PID=`echo $!`
wait $PID
STATUS=`echo $?`
}
job1
sleep 7
echo $STATUS

cat /tmp/t1.sh
#!/bin/ksh
sleep 5
exit 0

This works fine as long as I am calling job1() in the foreground. If I change job1 ==> job1& I am not getting anything from $STATUS variable. Any help is appreciated.

Thanks.
Never quit
3 REPLIES 3
Sridhar Bhaskarla
Honored Contributor

Re: Scripting - jobs

Hi,

Once you put your job1 into background all of it's variables inside that function are unavailable to the current shell. So, you won't get anything to the STATUS variable current shell. Try this way. Exit the job1 code with the return code from t1.sh and capture it from the wait of job1.

#!/bin/ksh
job1() {
t1.sh &
PID=`echo $!`
wait $PID
STATUS=$?
exit $STATUS

}

job1 &
PID=`echo $!`
wait $PID
JOBSTATUS=$?
echo $JOBSTATUS

I hope you got the picture.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Zeev Schultz
Honored Contributor

Re: Scripting - jobs

Or add a line in job1 () like this:
echo "$PID\t$STATUS" >> /tmp/stats.list
and later view all $STATUS for all $PIDS as
you comfortable.

Zeev
So computers don't think yet. At least not chess computers. - Seymour Cray
Caesar_3
Esteemed Contributor

Re: Scripting - jobs

Hello!

You can made the job1 function as difrent
script and run it with & like you do in
your job1 for /tmp/t1.sh

Caesar