Propagate exist status from a child shell script to a parent.

Hi All,

I have a parent shell script A and a child shell script B.

1). If a command i.e a mysqdump fails in shell script B fails then I trap the error with this code

if [[ $?>0 ]]
        then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"
else
        func_logwriter "Successfully exported the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"
fi

the func_exit writes various messages to log and calls exit 1

2). The parent script A called script B and also monitors the return status in the same way as above. However, the func_exit possess the following code:

function func_exit()
{
        func_logwritertimestamp
        func_logwriter "##########"
        func_logwriter "# APPLICATION ERROR"
        func_logwriter "##########"
        func_logwriter "$1"
        func_logwriter "Aborting the Alpha process"
        func_logwriter "##########"

        SUBJECT=${ADMINSUBJECT}." Failure - Details Attached"
        TO=${ADMINMAIL}

        /usr/bin/mailx -s "${SUBJECT}" ${TO} < ${LOGSDIR}/$(date +"%Y-%m-%d")_alpha.log
        exit 1
}

For some reason the func_exit in script A is not being called on a failure in script B. Hence I don't get an email.

Please could anyone advise if the exit status propagation between the parent and child script is correct. I am using Debian.
Many thanks for your help.

Where is the wait statement in the parent process? I do not see one.

The parent creates the child, then (immediately or a little later) waits for the child. That is how the parent process knows about the child's exit status.

# parent example
child. sh &
# using your func_exit()
# this line waits for the child pid ( $! variable)  then calls func_exit if error 
wait $!  || func_exit

# child.sh
# child process  
sleep 5
exit 1

Use that to test you exit function.