Re: Mutiple fork return status
- From: bsh <brian_hiles@xxxxxxxxxxxxxx>
- Date: Mon, 11 Jun 2007 16:13:08 -0700
"Prateek" <pratee...@xxxxxxxxx> wrote:
I am starting mutiple fork processes. Each forked process can have a
success or failure result status. How can these be communicated to the
original parent process?
...
Now each back_job can have a return status of 0 or 7. And depending on
the success of all jobs, we can say "all back_jobs ran successfully".
Please advise how to go about this.
Your query is difficult to answer, insofar as the
technique applicable to a specific problem is either
not portable or insufficiently general or robust for
common use.
Unfortunately, this is a shell feature that has been
largely implemented ad hoc -- see below. If you had
been using bash(1), I would have said to use the following
shell special array PIPESTATUS:
#!bash
# Url: http://mirror.hamakor.org.il/archives/linux-il/10-2003/index.html#5984
$ true | false | false | true
$ echo ${PIPESTATUS[@]}
0 1 1 0
BTW, The ksh93g+ shell-option "pipefail" allows the
return code to be set as the first command in pipeline
that returned non-zero exit status.
Is the solution as simple as:
#!ksh
# original example: not tested by me
# Traps are not processed while a job is waiting for a
# foreground process. Thus, a trap on CHLD won't be executed
# until the foreground job terminates.
export COPID COPIDERR
process |& COPID=$! # capture PID of coprocess
trap 'wait $COPID; COPIDERR=$?' CHLD # capture rc
#!ksh
# revision: not tested (probably error involving "wait" arglist)
set -o monitor # this should have been in the original code
typeset -x COPID COPIDERR
typeset -i i=0
process1 & COPID[i+=1]=$! # capture PID of bg process
# process2 & COPID[i+=1]=$! # capture PID of bg process ...
trap 'wait ${COPID[@]}; COPIDERR=$?' CHLD # capture rc
I have not yet been able to commit my time and energy
to implementing a PIPESTATUS-like facility to ksh88/
ksh93. The technique that I will use is based upon a
generalisation of the above code using the builtin "jobs"
and the following code fragment by Tom Christiansen:
"Consider the pipeline:
A | B | C
You want to know the status of C, well, that's easy: it's
in $?, or $status in csh. But if you want it from A, you're
out of luck -- if you're in the csh, that is. In the Bourne
shell, you can get it, although doing so is a bit tricky.
Here's something I had to do where I ran dd's stderr into a
grep -v pipe to get rid of the records in/out noise, but had
to return the dd's exit status, not the grep's:
device=/dev/rmt8
dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
exec 3>&1
status=`( (dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4)
|
egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1
`
exit $status
"
And here is a simpler version:
"Run pipe with return code of first command:
exec 3>&1
rc=`( (cm1 2>&1 1>&3 3>&- 4>&-; echo $? >&4) | cmd2 1>&2 3>&- 4>&-)
4>&1 `
exit $rc
"
I can merely point you to articles in hope that you
will find one which is pertainent:
"Can A Pipe Have More Than One Exit?":
http://www.networkcomputing.com/unixworld/answers/003.html,$0,"webpage","Ray
Swartz" <ray@xxxxxxxxxxxx>
"Managing Background Commands in Shell Scripts":
http://www.unixreview.com/documents/s=9340/ur0409a/ur0409b.htm
I wrote a job monitor function suite that manages
processes, including return codes, priorities, and
foreground/background status. Alas, I cannot locate it,
but there are other scripts that you may wish to check
out. I case the above documents do not provide a solution,
here are some scripts, listed in order of complexity:
"jobmon.sh":
http://www.shelldorado.com/scripts/cmds/example/jobmon.txt
"monproc.bash": (looks like it can be rewritten in ksh)
http://www.comp.eonworks.com/scripts/monproc
"ctl.ksh":
https://sourceforge.net/projects/appctl/.ksh
http://appctl.sourceforge.net/
"onite.sh":
http://www.samag.com/documents/s=1229/sam9201d/9201d.htm
"A Community-Style Overnight Job Spooler": http://www.samag.com/documents/s=1229/sam9201d/9201d.htm
"qjob.ksh":
http://www.unixreview.com/documents/s=9823/sam0508h/0508h.htm
http://www.samag.com/documents/s=9820/sam0508h/0508h_l1.htm
TRACT: Schaefer, Ed & John Spurgeon. "Implementing Semaphores in the
Shell". 2004. Sys Admin 13(8):41-47. <http://www.samag.com/documents/
s=9238/sam0408f/0408f.htm>. <http://critical-error.com/
PrintArticle1634.phtml>.
=Brian
.
- References:
- Mutiple fork return status
- From: Prateek
- Mutiple fork return status
- Prev by Date: Re: Setting variables in a remote SSH shell.
- Next by Date: Re: Mutiple fork return status
- Previous by thread: Re: Mutiple fork return status
- Next by thread: Re: Mutiple fork return status
- Index(es):
Relevant Pages
|