LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-24-2006, 11:59 AM   #1
gururaj
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Rep: Reputation: 0
getting the exit status of a background processes


Hi,
I want to get the exit code for all the background processes after the wait

#!/bin/sh
echo "Parallel Processes Start"
sleep a.sh &
sleep b.sh &
sleep 100 &
echo "Parallel Processes Created"
wait

can you please advise on this

Thanks in advance.
 
Old 04-24-2006, 05:47 PM   #2
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
I took the "sleep" from in front of a.sh and b.sh - I don't know what you meant by it.

Code:
#!/bin/sh
echo "Parallel Processes Start"
a.sh &
apid=$!
b.sh &
bpid=$!
sleep 100 &
slpid=$!
echo "Parallel Processes Created"
wait apid
acode=$?
wait bpid
bcode=$?
wait slpid
slcode=$?
echo "a.sh code is $acode"
echo "b.sh code is $bcode"
echo "sleep code is $slcode"
 
Old 04-25-2006, 12:39 AM   #3
gururaj
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
i am sorry, i typed wrongly the script

it should be as

#!/bin/sh
echo "Parallel Processes Start"
a.sh &
b.sh &
sleep 100 &
echo "Parallel Processes Created"
wait

here a.sh and b.sh are another shell scripts which has exit status as 0 when the scripts executed successfully and 1 for any abnormal termination.

now after wait i have to run c.sh only when both a.sh and b.sh should have returned 0.

sorry for the inconvineance
 
Old 04-25-2006, 01:15 PM   #4
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Ok, a few minor changes from my last post. You don't need the sleep at all, since the waits will cover it for you.
Code:
#!/bin/sh
echo "Parallel Processes Start"
a.sh &
apid=$!
b.sh &
bpid=$!
echo "Parallel Processes Created"
wait apid
acode=$?
wait bpid
bcode=$?
[ $acode -eq 0 ] && [ $bcode -eq 0 ] && c.sh
 
Old 04-26-2006, 01:29 AM   #5
gururaj
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks ataraxia,

What if the b.sh finishes its execution well before a.sh,
can is still get the return code of b.sh ?
 
Old 04-26-2006, 06:21 PM   #6
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Quote:
Originally Posted by gururaj
Thanks ataraxia,

What if the b.sh finishes its execution well before a.sh,
can is still get the return code of b.sh ?
Yes. Since the waits are for specific PIDs, you will always get the right return code in the right variable. If b.sh finishes first, it will just make the second wait finish instantly.
 
Old 04-27-2006, 06:59 AM   #7
gururaj
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
I think
acode=$? will get the return code of wait and not the return code of a.sh right. Is there any other way ??
 
Old 04-27-2006, 05:56 PM   #8
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Quote:
Originally Posted by gururaj
I think
acode=$? will get the return code of wait and not the return code of a.sh right. Is there any other way ??
The return code of wait is the return code of a.sh, according to the bash manpage.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
> Ftp With Exit Status, Ftp with exit status vwvr9 Linux - General 4 02-23-2005 02:53 AM
exit status naflan Programming 2 10-22-2004 12:22 PM
error exit status 30 debian_dummy Debian 2 09-21-2004 10:26 PM
gcc exit status 1 Daddio Linux - Software 4 10-25-2003 05:37 PM
Trapping an exit status chrisk5527 Linux - General 3 09-12-2003 09:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration