<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Child passing exit code to parent in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035245#M5901</link>
    <description>Hi Brian,&lt;BR /&gt;&lt;BR /&gt;Are you looking for 'wait'?&lt;BR /&gt;&lt;BR /&gt;Or are you using straight 'sh'? (and not Bash).&lt;BR /&gt;&lt;BR /&gt;Cristi</description>
    <pubDate>Tue, 29 Jul 2003 05:26:58 GMT</pubDate>
    <dc:creator>Cristian Draghici_1</dc:creator>
    <dc:date>2003-07-29T05:26:58Z</dc:date>
    <item>
      <title>Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035244#M5900</link>
      <description>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?&lt;BR /&gt;&lt;BR /&gt;Thanx&lt;BR /&gt;&lt;BR /&gt;Brian Pyle</description>
      <pubDate>Mon, 28 Jul 2003 17:10:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035244#M5900</guid>
      <dc:creator>Brian Pyle</dc:creator>
      <dc:date>2003-07-28T17:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035245#M5901</link>
      <description>Hi Brian,&lt;BR /&gt;&lt;BR /&gt;Are you looking for 'wait'?&lt;BR /&gt;&lt;BR /&gt;Or are you using straight 'sh'? (and not Bash).&lt;BR /&gt;&lt;BR /&gt;Cristi</description>
      <pubDate>Tue, 29 Jul 2003 05:26:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035245#M5901</guid>
      <dc:creator>Cristian Draghici_1</dc:creator>
      <dc:date>2003-07-29T05:26:58Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035246#M5902</link>
      <description>Hello&lt;BR /&gt;&lt;BR /&gt;Perhaps what you are looking for is semaphore...&lt;BR /&gt;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 ..     &lt;BR /&gt; for oracle and/or applications&lt;BR /&gt;&lt;BR /&gt;But if you do a man -k semaphore you should be able to work it out from there.&lt;BR /&gt;&lt;BR /&gt;Hope this is of use.&lt;BR /&gt;&lt;BR /&gt;Jean-Pierre</description>
      <pubDate>Tue, 29 Jul 2003 07:34:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035246#M5902</guid>
      <dc:creator>Huc_1</dc:creator>
      <dc:date>2003-07-29T07:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035247#M5903</link>
      <description>Brian,&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;exit n&lt;BR /&gt;&lt;BR /&gt;where 'n' is the return code you want to pass back to the parent.  In the parent script, you would do something like this:&lt;BR /&gt;&lt;BR /&gt;child.sh&lt;BR /&gt;rc=$?&lt;BR /&gt;&lt;BR /&gt;echo "rc = $rc"&lt;BR /&gt;&lt;BR /&gt;This would display the return code passed back via the 'exit' statement.&lt;BR /&gt;&lt;BR /&gt;HTH,&lt;BR /&gt;Tom&lt;BR /&gt;</description>
      <pubDate>Tue, 29 Jul 2003 10:57:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035247#M5903</guid>
      <dc:creator>Tom Dawson</dc:creator>
      <dc:date>2003-07-29T10:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035248#M5904</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;eg.&lt;BR /&gt;&lt;BR /&gt;while [ $count -lt 10 ]&lt;BR /&gt;  do&lt;BR /&gt;    run_script=`child_script &amp;amp;`&lt;BR /&gt;    child_pid_list=$child_pid_list,$!&lt;BR /&gt;    (($count=$count+1))&lt;BR /&gt;  done&lt;BR /&gt;&lt;BR /&gt;while [ more_children -gt 0 ]&lt;BR /&gt;  do&lt;BR /&gt;    "somehow check child status or exit"&lt;BR /&gt;    "do something based on that exit"&lt;BR /&gt;    sleep 10&lt;BR /&gt;  done&lt;BR /&gt;&lt;BR /&gt;Thanx&lt;BR /&gt;&lt;BR /&gt;Brian</description>
      <pubDate>Tue, 29 Jul 2003 12:37:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035248#M5904</guid>
      <dc:creator>Brian Pyle</dc:creator>
      <dc:date>2003-07-29T12:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035249#M5905</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;Brian</description>
      <pubDate>Tue, 29 Jul 2003 12:40:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035249#M5905</guid>
      <dc:creator>Brian Pyle</dc:creator>
      <dc:date>2003-07-29T12:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035250#M5906</link>
      <description>The "wait" shell command returns the exit&lt;BR /&gt;code of the child process that terminated.&lt;BR /&gt;You can use "wait" two ways:&lt;BR /&gt;&lt;BR /&gt;wait&lt;BR /&gt;wait job&lt;BR /&gt;&lt;BR /&gt;The first form waits for all child processes&lt;BR /&gt;and returns the exit code from the last one&lt;BR /&gt;that exited.  Probably not useful for you,&lt;BR /&gt;because you want the codes from all the&lt;BR /&gt;child processes.&lt;BR /&gt;&lt;BR /&gt;The second form waits for a specific job&lt;BR /&gt;and returns its exit code.  This is closer&lt;BR /&gt;to what you want, but requires you do wait&lt;BR /&gt;for them in order.&lt;BR /&gt;&lt;BR /&gt;What you really need is something like the&lt;BR /&gt;C call wait(), which waits for any child&lt;BR /&gt;process to finish and returns its error&lt;BR /&gt;code.  Unfortunately, I don't think the&lt;BR /&gt;sh-posix "wait" has that capability.  To&lt;BR /&gt;do what you want I think you'll have to&lt;BR /&gt;go to C or Perl.&lt;BR /&gt;&lt;BR /&gt;HTH&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 29 Jul 2003 16:41:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035250#M5906</guid>
      <dc:creator>Gregory Fruth</dc:creator>
      <dc:date>2003-07-29T16:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: Child passing exit code to parent</title>
      <link>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035251#M5907</link>
      <description>Ok, here's half the solution.&lt;BR /&gt;&lt;BR /&gt;parent.sh:&lt;BR /&gt;----&lt;BR /&gt;count=1&lt;BR /&gt;declare -a child_pid_array&lt;BR /&gt;&lt;BR /&gt;while [ $count -lt 10 ]&lt;BR /&gt;do&lt;BR /&gt;        ./child.sh $$ &amp;amp;&lt;BR /&gt;        pid=$!&lt;BR /&gt;        echo "Spawned $pid"&lt;BR /&gt;        ###result=`wait $pid &amp;amp;`&lt;BR /&gt;        echo $result&lt;BR /&gt;        child_pid_array[$count]=$pid&lt;BR /&gt;        ((count=$count+1))&lt;BR /&gt;&lt;BR /&gt;        echo "$count:  $child_pid_array, ${child_pid_array[$count]}"&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;running=9&lt;BR /&gt;while [ $running -gt 0 ]&lt;BR /&gt;do&lt;BR /&gt;        sleep 1&lt;BR /&gt;        count=1&lt;BR /&gt;        running=9&lt;BR /&gt;        echo ----------------------&lt;BR /&gt;        while [ $count -lt 10 ]&lt;BR /&gt;        do&lt;BR /&gt;                if [ -d /proc/${child_pid_array[$count]} ]&lt;BR /&gt;                        then echo "$count still running"&lt;BR /&gt;                else&lt;BR /&gt;                        echo "${child_pid_array[$count]} dead"&lt;BR /&gt;                        ((running=$running-1))&lt;BR /&gt;                fi&lt;BR /&gt;                ((count=$count+1))&lt;BR /&gt;        done&lt;BR /&gt;        echo "$running running"&lt;BR /&gt;done&lt;BR /&gt;----&lt;BR /&gt;&lt;BR /&gt;and child.sh&lt;BR /&gt;----&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;sleeptime=`echo "$RANDOM/1000" | bc`&lt;BR /&gt;sleep $sleeptime&lt;BR /&gt;&lt;BR /&gt;echo "Process $$ Exiting with status $sleeptime"  &amp;gt; "/proc/$1/fd/1"&lt;BR /&gt;exit $sleeptime&lt;BR /&gt;----&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Here's what i'm talking about:&lt;BR /&gt;(pstree just after running the parent process)&lt;BR /&gt;[diciu@bluefish tmp]$ pstree&lt;BR /&gt;init-+-anacron&lt;BR /&gt;     |-atd&lt;BR /&gt;     |-bdflush&lt;BR /&gt;     |-cardmgr&lt;BR /&gt;     |-8*[child.sh---sleep] &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; CHILD&lt;BR /&gt;     |-crond&lt;BR /&gt;[..]&lt;BR /&gt;     |-mdrecoveryd&lt;BR /&gt;     |-6*[mingetty]&lt;BR /&gt;     |-sshd&lt;BR /&gt;     |-syslogd&lt;BR /&gt;     |-xdm-+-X&lt;BR /&gt;     |     `-xdm---.Xclients-defau-+-bbrun&lt;BR /&gt;     |                             |-fluxbox-+-mozilla-bin&lt;BR /&gt;     |                             |         |-sylpheed&lt;BR /&gt;     |                             |         |-xterm---bash---su---bash&lt;BR /&gt;     |                             |         |-xterm---bash---man---sh---sh-+-l+&lt;BR /&gt;     |                             |         |                              `-n+&lt;BR /&gt;     |                             |         `-xterm---bash--- &amp;lt;&amp;lt;&amp;lt; PARENT&lt;BR /&gt;&lt;BR /&gt;and the parent call to wait will result in:&lt;BR /&gt;Spawned 1678&lt;BR /&gt;./parent.sh: line 1: wait: pid 1678 is not a child of this shell&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;Cheers&lt;BR /&gt;Cristi</description>
      <pubDate>Wed, 30 Jul 2003 05:44:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/child-passing-exit-code-to-parent/m-p/3035251#M5907</guid>
      <dc:creator>Cristian Draghici_1</dc:creator>
      <dc:date>2003-07-30T05:44:27Z</dc:date>
    </item>
  </channel>
</rss>

