Operating System - Linux
1821467 Members
2893 Online
109633 Solutions
New Discussion юеВ

Re: Child passing exit code to parent

 
Brian Pyle
Frequent Advisor

Child passing exit code to parent

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?

Thanx

Brian Pyle
Follow The Path With Heart
7 REPLIES 7
Cristian Draghici_1
Frequent Advisor

Re: Child passing exit code to parent

Hi Brian,

Are you looking for 'wait'?

Or are you using straight 'sh'? (and not Bash).

Cristi
Huc_1
Honored Contributor

Re: Child passing exit code to parent

Hello

Perhaps what you are looking for is semaphore...
It is difficult in a sense not to use a file as everithing in Unix is a file, semaphore is also a file but it lives in memory, I have never used theses in a program myself, I have only managed then, that is added some to kernel parameters ..
for oracle and/or applications

But if you do a man -k semaphore you should be able to work it out from there.

Hope this is of use.

Jean-Pierre
Smile I will feel the difference
Tom Dawson
Regular Advisor

Re: Child passing exit code to parent

Brian,

Maybe I'm misunderstanding what you're trying to do. But if the child process is a shell script, you should be able to execute:

exit n

where 'n' is the return code you want to pass back to the parent. In the parent script, you would do something like this:

child.sh
rc=$?

echo "rc = $rc"

This would display the return code passed back via the 'exit' statement.

HTH,
Tom
Brian Pyle
Frequent Advisor

Re: Child passing exit code to parent

Thanx everyone, but let me clarify what I am trying to do. I want to spawn multiple child processes, and manage those through the parent. These children would be run in the background, and I want to somehow pass an exit code back to the parent, even though the child has been run in the background.

eg.

while [ $count -lt 10 ]
do
run_script=`child_script &`
child_pid_list=$child_pid_list,$!
(($count=$count+1))
done

while [ more_children -gt 0 ]
do
"somehow check child status or exit"
"do something based on that exit"
sleep 10
done

Thanx

Brian
Follow The Path With Heart
Brian Pyle
Frequent Advisor

Re: Child passing exit code to parent

I would like to do this without writing to a file though. If I have to write to a file, that's fine. Just that it would be cleaner if I didn't have to.

Brian
Follow The Path With Heart
Gregory Fruth
Esteemed Contributor

Re: Child passing exit code to parent

The "wait" shell command returns the exit
code of the child process that terminated.
You can use "wait" two ways:

wait
wait job

The first form waits for all child processes
and returns the exit code from the last one
that exited. Probably not useful for you,
because you want the codes from all the
child processes.

The second form waits for a specific job
and returns its exit code. This is closer
to what you want, but requires you do wait
for them in order.

What you really need is something like the
C call wait(), which waits for any child
process to finish and returns its error
code. Unfortunately, I don't think the
sh-posix "wait" has that capability. To
do what you want I think you'll have to
go to C or Perl.

HTH

Cristian Draghici_1
Frequent Advisor

Re: Child passing exit code to parent

Ok, here's half the solution.

parent.sh:
----
count=1
declare -a child_pid_array

while [ $count -lt 10 ]
do
./child.sh $$ &
pid=$!
echo "Spawned $pid"
###result=`wait $pid &`
echo $result
child_pid_array[$count]=$pid
((count=$count+1))

echo "$count: $child_pid_array, ${child_pid_array[$count]}"
done

running=9
while [ $running -gt 0 ]
do
sleep 1
count=1
running=9
echo ----------------------
while [ $count -lt 10 ]
do
if [ -d /proc/${child_pid_array[$count]} ]
then echo "$count still running"
else
echo "${child_pid_array[$count]} dead"
((running=$running-1))
fi
((count=$count+1))
done
echo "$running running"
done
----

and child.sh
----
#!/bin/sh

sleeptime=`echo "$RANDOM/1000" | bc`
sleep $sleeptime

echo "Process $$ Exiting with status $sleeptime" > "/proc/$1/fd/1"
exit $sleeptime
----

Only half solved as it will tell you if the child processes are still alive. Status is sent via a fd so that's bad.

The commented wait call in the parent script will never work because when a job is started in the background it is not child of the shell that started it.

Here's what i'm talking about:
(pstree just after running the parent process)
[diciu@bluefish tmp]$ pstree
init-+-anacron
|-atd
|-bdflush
|-cardmgr
|-8*[child.sh---sleep] <<<<<< CHILD
|-crond
[..]
|-mdrecoveryd
|-6*[mingetty]
|-sshd
|-syslogd
|-xdm-+-X
| `-xdm---.Xclients-defau-+-bbrun
| |-fluxbox-+-mozilla-bin
| | |-sylpheed
| | |-xterm---bash---su---bash
| | |-xterm---bash---man---sh---sh-+-l+
| | | `-n+
| | `-xterm---bash--- <<< PARENT

and the parent call to wait will result in:
Spawned 1678
./parent.sh: line 1: wait: pid 1678 is not a child of this shell


So I guess the only thing that stands is job management but I have no idea if the works programatically (ie without Ctrl+Z to put something in background).

Cheers
Cristi