[lug] checking bg processes' exit status

Jeffrey Haemer jeffrey.haemer at gmail.com
Tue Mar 4 11:29:36 MST 2008


Nick,

The biggest problem is saving $? for each command, especially when you're
throwing some or all of them into the background, so they run in parallel.

An easy solution, if practical, is to wrap each process in a shell script
that saves its exit status.
Thus

*$ cat foo*
> #!/bin/bash
> ls bogus-filename
> echo $? > $0.status
> *$ foo &> /dev/null &*
> *$ cat foo.status*
> 2


As a variant on that, I sometimes use a logging module, which redirects all
output to a logfile and renames the logfile on completion.  Some variant on
this will do the trick:

*$ cat foo*
> #!/bin/bash
> source ./logging.sh
> ls $*       # your command goes here
> *$ cat logging.sh*
> # save a log, with the error status
>
> rm -f $0.[0-9]* $0.out
> LOG=$0.out
> exec &>$LOG
> trap 'mv $LOG ${LOG/out/$?}' EXIT
> *$ foo; ls*
> foo
> foo.0
> *$ foo bogusfile; ls*
> foo
> foo.2


Hope this helps.

On Tue, Mar 4, 2008 at 10:26 AM, Nick Golder <nrg at nirgo.net> wrote:

> I have a bash script that is setting a series of processes in the
> background and I want to see what their exit codes are.  I have played
> around trying to find a way to do it and I haven't found a reliable way,
> yet.  Any suggestions?
>
>
> --
> Nick Golder
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
>



-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell]
http://goyishekop.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20080304/cdc6fcf5/attachment.html>


More information about the LUG mailing list