Return value of background process Return value of background process shell shell

Return value of background process


You can use shell traps to invoke a function when a child exits by trapping SIGCHLD. If there is only one background process running, then you can wait for it in the sigchld handler and get the status there. If there are multiple background children running it gets a little more complex; here is a code sample (only tested with bash):

set -m # enable job controlprtchld() {    joblist=$(jobs -l | tr "\n" "^")  while read -a jl -d "^"; do    if [ ${jl[2]} == "Exit" ] ; then       job=${jl[1]}       status=${jl[3]}        task=${jl[*]:4}      break      fi    done <<< $joblist  wait $job   echo job $task exited: $status}trap prtchld SIGCHLD(sleep 5 ; exit 5) &(sleep 1 ; exit 7) &echo stuff is runningwait


I like the first one better for my purpose, I presume in the "do something here if process failed" I can kill the script that called this wrapper script for foo by using it's name.

I think the first solution works well for multiple children. Anyway, I had to get this done quickly, so I used a hack which works for my application:

I start the process in background as usual within the main script, then use $! to get it's pid ( since $! returns last bg pid), sleep for 2 seconds and do a ps -e | grep pid to check if the process is still around based on the return value of (ps -e | grep pid). This works well for me because if my background process aborts it does so immediately ( because the address is in use).


You could nest the background process inside a script. For example, if the process you wish to send to the background is called foo:

#!/bin/shfooif [ $? ]then     # do something here if process failedfi

Then just run the above script in the background instead of foo. you can kill it if you need to shut it down, but otherwise it will never terminate as long as foo continues to run, and if foo dies, you can do whatever you want to based on its error code.