Checking Return Codes of Background Processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking Return Codes of Background Processes
# 1  
Old 11-13-2003
Checking Return Codes of Background Processes

I'm trying to do the following:
1) Run a bunch of jobs in the background
2) Determine if any one of them returns with a non-zero exit status

Here's what I've come up with so far:
##########################################
#!/bin/ksh

while [ ${#pid_array[*]} -lt 1024 ]
do
SLEEP_TIME=`expr 1024 - ${#pid_array[*]}`
nohup exit_script ${SLEEP_TIME} ${#pid_array[*]} > /dev/null &
SAVE_PID=$!
echo "PID:${SAVE_PID} SLOT:${#pid_array[*]} SLEEP:${SLEEP_TIME}"
pid_array[${#pid_array[*]}]=${SAVE_PID}
done

jobs -l

echo "Waiting..."
CNT=0
while [ $CNT -lt ${#pid_array[*]} ]
do
wait ${pid_array[${CNT}]}
echo "PID:${pid_array[${CNT}]} CODE:$?"
CNT=`expr $CNT + 1`
done

exit 0
##########################################

exit_script is just a test program that sleeps for $1 and returns with a code of $2 (mod 256, of course).

The problem I'm having is if I perform the wait on a given pid, and that pid is already done, it doesn't always return with the proper code. Specifically, the first 25 will return with the correct code while the rest return with 127, wait's way of saying it doesn't know the code.

This leaves me with two questions:
1) Is there a way to make it "remember" more than 25 return codes?
2) Is there a better way to do this?

If it matters, this is being ran using ksh on Solaris 8. Thanks for any help you can provide.

Joel
# 2  
Old 11-14-2003
Re: Checking Return Codes of Background Processes

Quote:
Originally posted by bergerj3
Is there a better way to do this?
It would be so much easier if your background processes wrote out their respective exit codes to a /tmp file that your main process could then read and evaluated. Just remember to clean them up.
# 3  
Old 11-14-2003
See this for a better description of the problem. I really would switch to C for this. Looking at the ksh docs, it kinda looks like doing a "set -o monitor" and installing a trap on CHLD which waits on %% or %- or something should work. But I couldn't get it to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Return Codes...

Not sure if this is of any use but...... I was messing around with getting return codes greater than 255 for special usage... Of course the code could be made simple but in this code the new stored return code is generated as exit is progressing... #!/bin/sh # Real and imaginary return... (9 Replies)
Discussion started by: wisecracker
9 Replies

2. UNIX and Linux Applications

Oracle return codes?

Having searched high and low through Oracles documentation, I came to think that they're very scripting-averse, as there's (apparently) no list of possible return/exit codes for their various command line utilities. Is anyone here in possession of such a list, or knows where to find one? It... (16 Replies)
Discussion started by: pludi
16 Replies

3. Shell Programming and Scripting

Different Return Codes

Hi, I wanted to know the significance of different return codes when we do echo $? I know when $? returns 0 the command has worked successfully. but what does $? = 1, 2, 3 etc. signify. Thanks in advance for the help !!! (3 Replies)
Discussion started by: aarti.popi
3 Replies

4. UNIX for Dummies Questions & Answers

Displaying Return Codes

This is a high-level explanation, if more details are needed, please do not hesitate to ask. I have a set of .ctl files which I want to execute: AV1.ctl AV2.ctl AV3.ctl I have a script which has a for loop in it: for filename in AV1 AV2 AV3 do . execute_another_script.sh done ... (2 Replies)
Discussion started by: hern14
2 Replies

5. Shell Programming and Scripting

help with return codes

Hi In an unix script I am using an Perl one liner perl -i -ne '-----' If the perl one liner fails i am not able to catch the return code. It always give 0 as return code. Can you tell me how can i catch the return code perl -i -ne '---' RETCODE=$? echo $RETCODE Thanks and Regards Ammu (2 Replies)
Discussion started by: ammu
2 Replies

6. UNIX for Advanced & Expert Users

Return Codes

I have a simple script which renames a file.How do i capture the return code of the script if the script fails (3 Replies)
Discussion started by: kris01752
3 Replies

7. UNIX for Dummies Questions & Answers

Return codes

Hi, Can anyone tell me if there are return codes for SFTP? If so how would you capture them? I've tried 'man sftp' but its not particularly helpful. Many thanks Helen :confused: (4 Replies)
Discussion started by: Bab00shka
4 Replies

8. UNIX for Dummies Questions & Answers

unix return codes

Suppose I have a script which is monitoring a directory whenever a file drops in that directory,it sends alert say I want to write a return code for the above script which on successful execution of script gives a return value Based on return code , I want to do initiate some jobs in other... (1 Reply)
Discussion started by: abhib45
1 Replies

9. UNIX for Dummies Questions & Answers

Help with Return codes

I have the below script I am running on a Solaris system to check the status of a Tivoli Workload Scheduler job and return the status. We need this script to return a '0' if any of the jobs in the stream are in a "EXEC" state and an "1" if in a "HOLD" state. I am not a programmer so I am not sure... (1 Reply)
Discussion started by: leezer1204
1 Replies

10. Shell Programming and Scripting

Background processes return 127 sporadically

I have created a shell script that spawns multiple background processes (spawns sqlplus application). I use an array to capture the process id of those background processes. I then loop through the array and issue a 'wait' command to wait on the process id that was captured in the array. I am... (2 Replies)
Discussion started by: max_largo
2 Replies
Login or Register to Ask a Question