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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Propagate exist status from a child shell script to a parent.
# 1  
Old 03-16-2012
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

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:

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.

Last edited by daveu7; 03-16-2012 at 08:53 PM..
# 2  
Old 03-16-2012
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.

Code:
# 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

Code:
# child.sh
# child process  
sleep 5
exit 1

Use that to test you exit function.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fail Parent Script if any of the child fails

I have requirement where I need to fail parent if any one of the child process fails. Here is the code snippet for i in 1 2 3 4 5 6 7 8 9 10 do child_script $i & done wait I need to fail my main script if any one of my child process fails (8 Replies)
Discussion started by: gvkumar25
8 Replies

2. Linux

Monitoring shell script if object is down and it should update the child objects status as well

Viewers Please help me to get out of the below issue.. Thanks in advance Required shell script for Monitoring status of the objects and it should update the child objects status as well. Requirements:- 1. We are working on IIB (IBM Integration Bus v10) and trying to implement the broker ('... (0 Replies)
Discussion started by: dsreddy447
0 Replies

3. Shell Programming and Scripting

How to exit from the parent script while the child is running?

hi, i want to call a child shell script from a parent shell script. the child will be running for 5 mins. normally when the child is running, parent will wait till the child completes. so in the above case parent will be paused for 5 mins. is there a way so that the parents does not wait for the... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

5. Shell Programming and Scripting

Script, child kills parent

Hello everyone, I'm trying to write a script, i would like to say the child kills the parent, how would i do that? (2 Replies)
Discussion started by: jessy21
2 Replies

6. Shell Programming and Scripting

How to return control from the child script to the parent one?

I have two shell scripts : A.sh and B.sh A.sh echo "In A" exec B.sh echo "After B" B.sh echo "In B" The output is : In A In B I want the output : In A In B After B (4 Replies)
Discussion started by: suchismitasuchi
4 Replies

7. Shell Programming and Scripting

Parent/child Korn shell script help

I have the following two Korn shell scripts: SHELL1.ksh #!/usr/bin/ksh nohup sas /abc/123/sasprogram1.sas & SHELL2.ksh #!/usr/bin/ksh ./SHELL1.ksh wait nohup sas /abc/123/sasprogram2.sas & My goal is to run SHELL1.ksh within SHELL2.ksh. SHELL1.ksh runs sasprogram1.sas. I would like... (1 Reply)
Discussion started by: sasaliasim
1 Replies

8. Shell Programming and Scripting

parent shell is waiting upon the child shell

Hi, I haev to devlop a script which when executed will take in a sudo privelege and run a set of commands then will go back to parent shell and execute the rest of the command But the problem I am facing is that when the script is executed it takes the sudo privelege but it waits for the... (0 Replies)
Discussion started by: ruchirmayank
0 Replies

9. Programming

Status of child job after parent is killed

Hi, I have a requirement. Scenario: A parent job invokes a child job and gets killed. The child becomes orphan and gets attached to init. Child job is removed from the pid table as soon as it gets completed. Requirement is i need the status of the child job even after the parent job is... (7 Replies)
Discussion started by: anjul_thegreat
7 Replies

10. Shell Programming and Scripting

Returning values from child to parent shell

I need to send the status from child shell failure to parent shell. I would like to know how could we accomplish this. My parent.sh is as below: #!/bin/ksh set -x echo "I am in parent shell now..." child.sh ret_stat=$? echo "rest_stat=$ret_stat" echo "I am below parent shell end..." ... (4 Replies)
Discussion started by: acheepi
4 Replies
Login or Register to Ask a Question