Wait functionality in Solaris

hi all ,

If pid is not an active process ID, the wait utility will return immediately and the return code will be 0.

i just came across this which means that wait will returm 0 even though the script has failed :frowning: so can anyone tell me what is the exact way to get the return status from wait command in bash script ?

If a process no more exists as either active or zombie, its exit status is not recorded anywhere so is unknown. You need to use some other custom mechanism to store its status.

Not exactly. If I run the bash script (from a file named nonzero):

#!/bin/bash
(sleep 5;exit 2)&
pid=$!
(sleep 10;exit 1)
echo "return code from synchronous child $?"
wait $pid
echo "return code from asynchronous child that exited earlier: $?"
(sleep 5;exit 2)&
pid=$!
(sleep 10;exit 1)&
wait	# wait for all children to terminate
echo "wait with no pids returned $?"
wait $pid
echo "return code from asynchronous child that has already been reaped: $?"

I get the output:

return code from synchronous child 1
return code from asynchronous child that exited earlier: 2
wait with no pids returned 0
nonzero: line 13: wait: pid 78859 is not a child of this shell
return code from asynchronous child that has already been reaped: 127

The second line of the above output shows that I do get the exit code from a child that died before I called wait as long as its exit status hadn't already been reaped. The line in blue was a diagnostic written by the shell, not directly produced by the script. The final 127 exit status is an indication that you tried to wait for a child that was not your child or that you had already collected its exit status. You will only get a zero exit status if the child whose status wait collected exited with a zero exit status. (Note that if you kill a process and it is catching the signal you sent; it may still exit with a zero exit status.)

2 Likes

thanks a lot 4 dis piece of explanation :slight_smile: !!