exit codes of background jobs?
exit codes of background jobs?
am 05.12.2007 02:19:57 von Mikhail Teterin
Hello!
I have a script, which spawns a lot of background jobs and then waits for
them to finish.
The exit code must be 0 iff all background-jobs succeeded. How can I do
this?
If I simply use `wait', the exit-codes of indidividual jobs are lost.
I don't want to wait for individual ones separately, because there is no way
to know, which exits first...
I'm using ksh on Solaris-10, but am open to switching to some other shell. I
know, how to do this in Tcl, but nobody else here knows the language and
I'd like the script to be maintainable by more than one person :(
Thanks!
-mi
Re: exit codes of background jobs?
am 05.12.2007 03:00:32 von Bill Marcum
On 2007-12-05, Mikhail Teterin wrote:
>
>
> Hello!
>
> I have a script, which spawns a lot of background jobs and then waits for
> them to finish.
>
> The exit code must be 0 iff all background-jobs succeeded. How can I do
> this?
>
> If I simply use `wait', the exit-codes of indidividual jobs are lost.
>
> I don't want to wait for individual ones separately, because there is no way
> to know, which exits first...
>
> I'm using ksh on Solaris-10, but am open to switching to some other shell. I
> know, how to do this in Tcl, but nobody else here knows the language and
> I'd like the script to be maintainable by more than one person :(
>
You can have each background job write its exit code to a log file.
( foo; echo "foo:$?" >> $LOG ) &
Re: exit codes of background jobs?
am 05.12.2007 08:49:46 von Stephane CHAZELAS
On Tue, 04 Dec 2007 20:19:57 -0500, Mikhail Teterin wrote:
> Hello!
>
> I have a script, which spawns a lot of background jobs and then waits for
> them to finish.
>
> The exit code must be 0 iff all background-jobs succeeded. How can I do
> this?
>
> If I simply use `wait', the exit-codes of indidividual jobs are lost.
>
> I don't want to wait for individual ones separately, because there is no way
> to know, which exits first...
>
> I'm using ksh on Solaris-10, but am open to switching to some other shell. I
> know, how to do this in Tcl, but nobody else here knows the language and
> I'd like the script to be maintainable by more than one person :(
[...]
You should be able to wait for an already finished job and get
its exit status this way. Some shells don't support that (even
though it's required by POSIX) but I don't think it would be the
case of Solaris 10 ksh.
So you can do:
cmd1 & p1=$!
cmd2 & p2=$!
cmd3 & p3=$!
r=0
wait "$p1" || r=$?
wait "$p2" || r=$?
wait "$p3" || r=$?
exit "$r"
--
Stephane
Re: exit codes of background jobs?
am 05.12.2007 22:51:13 von spcecdt
In article <4756581a$0$27410$ba4acef3@news.orange.fr>,
Stephane Chazelas wrote:
>You should be able to wait for an already finished job and get
>its exit status this way. Some shells don't support that (even
>though it's required by POSIX) but I don't think it would be the
>case of Solaris 10 ksh.
Does POSIX specify how many jobs the shell should retain exit status for?
I tried this for various shells:
shell -c 'n=2000; i=1; while [ $i -lt $n ]; do sh -c "exit 99"& eval j$i=$!;
i=`expr $i + 1`; done; sleep 2; i=1; while [ $i -lt $n ]; do eval wait \$j$i;
[[ $? -ne 99 ]] && echo $i; i=`expr $i + 1`; done'
Results, where #saved is the number of backgrounded & exited processes for
which the shell gives the correct exit status:
shell #saved wait value if no status available
---------------------------------------------------------
Bourne shell 0 0
bash 2.03.0 1 127 also issues complaint
bash 3.0.15 1 127 also issues complaint
bash 3.2.17 266 127 also issues complaint
ksh88 1 0
ksh93f 500 127
ksh93o 500 127
ksh93r 1000 127
pdksh v5.2.14 1000 127
zsh 3.1.4 0 0
zsh 4.3.4 1 1 also issues complaint
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Re: exit codes of background jobs?
am 06.12.2007 09:03:07 von Stephane CHAZELAS
On Wed, 05 Dec 2007 21:51:13 -0000, John DuBois wrote:
> In article <4756581a$0$27410$ba4acef3@news.orange.fr>,
> Stephane Chazelas wrote:
>>You should be able to wait for an already finished job and get
>>its exit status this way. Some shells don't support that (even
>>though it's required by POSIX) but I don't think it would be the
>>case of Solaris 10 ksh.
>
> Does POSIX specify how many jobs the shell should retain exit status for?
CHILD_MAX whose value must be at least 25, and a shell is
allowed to forget the values for those process for which $! was
not expanded before it was reassigned.
That is I guess
sh -c '...; echo "$$" > fifo; ...' &
pid=$(cat fifo)
....
wait $pid
is not guaranteed to work.
See
http://www.opengroup.org/onlinepubs/009695399/utilities/wait .html
--
Stephane