Sponsored Content
Top Forums Shell Programming and Scripting Checking Return Codes of Background Processes Post 43209 by bergerj3 on Thursday 13th of November 2003 08:15:37 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
BG(P)							     POSIX Programmer's Manual							     BG(P)

NAME
bg - run jobs in the background SYNOPSIS
bg [job_id ...] DESCRIPTION
If job control is enabled (see the description of set -m), the bg utility shall resume suspended jobs from the current environment (see Shell Execution Environment ) by running them as background jobs. If the job specified by job_id is already a running background job, the bg utility shall have no effect and shall exit successfully. Using bg to place a job into the background shall cause its process ID to become "known in the current shell execution environment", as if it had been started as an asynchronous list; see Asynchronous Lists . OPTIONS
None. OPERANDS
The following operand shall be supported: job_id Specify the job to be resumed as a background job. If no job_id operand is given, the most recently suspended job shall be used. The format of job_id is described in the Base Definitions volume of IEEE Std 1003.1-2001, Section 3.203, Job Control Job ID. STDIN
Not used. INPUT FILES
None. ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of bg: LANG Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of IEEE Std 1003.1-2001, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments). LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES . ASYNCHRONOUS EVENTS
Default. STDOUT
The output of bg shall consist of a line in the format: "[%d] %s ", <job-number>, <command> where the fields are as follows: <job-number> A number that can be used to identify the job to the wait, fg, and kill utilities. Using these utilities, the job can be identified by prefixing the job number with '%' . <command> The associated command that was given to the shell. STDERR
The standard error shall be used only for diagnostic messages. OUTPUT FILES
None. EXTENDED DESCRIPTION
None. EXIT STATUS
The following exit values shall be returned: 0 Successful completion. >0 An error occurred. CONSEQUENCES OF ERRORS
If job control is disabled, the bg utility shall exit with an error and no job shall be placed in the background. The following sections are informative. APPLICATION USAGE
A job is generally suspended by typing the SUSP character (<control>-Z on most systems); see the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 11, General Terminal Interface. At that point, bg can put the job into the background. This is most effec- tive when the job is expecting no terminal input and its output has been redirected to non-terminal files. A background job can be forced to stop when it has terminal output by issuing the command: stty tostop A background job can be stopped with the command: kill -s stop job ID The bg utility does not work as expected when it is operating in its own utility execution environment because that environment has no sus- pended jobs. In the following examples: ... | xargs bg (bg) each bg operates in a different environment and does not share its parent shell's understanding of jobs. For this reason, bg is generally implemented as a shell regular built-in. EXAMPLES
None. RATIONALE
The extensions to the shell specified in this volume of IEEE Std 1003.1-2001 have mostly been based on features provided by the KornShell. The job control features provided by bg, fg, and jobs are also based on the KornShell. The standard developers examined the characteristics of the C shell versions of these utilities and found that differences exist. Despite widespread use of the C shell, the KornShell versions were selected for this volume of IEEE Std 1003.1-2001 to maintain a degree of uniformity with the rest of the KornShell features selected (such as the very popular command line editing features). The bg utility is expected to wrap its output if the output exceeds the number of display columns. FUTURE DIRECTIONS
None. SEE ALSO
Asynchronous Lists , fg , kill() , jobs , wait() COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 BG(P)
All times are GMT -4. The time now is 04:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy