Operating System - HP-UX
1844219 Members
2803 Online
110230 Solutions
New Discussion

Child process passing parent a status code

 
Brian Pyle
Frequent Advisor

Child process passing parent a status code

I'm using /usr/bin/sh, and am trying to figure out a way to manage a number of child procceses. I need to pass exit statuses back to the parent (managing) from the children. I have looked at trap, but it appears that is a one way from parent to child, not the other way. I was trying to avoid a file or fifo if possible. Anybody know of a way to pass info back to the parent?
Follow The Path With Heart
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Child process passing parent a status code

return 0
exit 0

for good status codes

return 1
exit 1

for bad.

There is a system for the various codes if you want to be all official.

The methodology above passed the code back from the child to the parent, if the parent is waiting. Whether or not this works depends on how the child was launched.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: Child process passing parent a status code

A child can certainly send a signal to a parent process that the parent can trap BUT the only thing that will be known is the signal number itself not an exit status.

One option is to use the wait command.
child.sh &
CHILDPID=${!}
...
...
...
wait ${CHILDPID}

The wait will block until the child finished and report its status.

Trying to export a variable from the child to the parent absolutely will not work.

If it ain't broke, I can fix that.
Brian Pyle
Frequent Advisor

Re: Child process passing parent a status code

My apologies, I left out a very crucial piece of info on this. I want the child to run in the background :)

Thanx
Follow The Path With Heart
A. Clay Stephenson
Acclaimed Contributor

Re: Child process passing parent a status code

In that case, my cut at this would be:

1) Capture the child pid using ${!}.

child.sh &
CHILDPID=${!}

The child will write its status to a temp file
based upon it's process ID ${$}.
e.g. /var/tmp/X${$}.txt
and just before exiting send a signal to its parent process using ${PPID}.
e.g. kill -s 16 ${PPID}

The parent would have a trap on signal 16 (or the agreed upon signal) to then read /var/tmp/X${CHILDPID}.txt for the status information and then remove the temp file.

This will give you a mechanism for asynchronously handling multiple child processes while continuing to do useful task in the parent. All the child processes could share a command signal but the operation should be quick so that no signals are ignored while processing an existing one.

If it ain't broke, I can fix that.
Brian Pyle
Frequent Advisor

Re: Child process passing parent a status code

Thanx for the help. Even though Clay's last example is probably what I'm going to use, I was really hoping somebody had a way to do this without writing a file. I've contemplated using some socket communica via perl, but would also like to avoid that if possible. Thought maybe somebody had a silver bullet :)

Thanx Again

Brian Pyle
Follow The Path With Heart
Peter Nikitka
Honored Contributor

Re: Child process passing parent a status code

Hi,

the exit-Status of the 'wait'-command is that of the child, so:
child &
wait $!
child_exit=$?

Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"