Fail Parent Script if any of the child fails


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fail Parent Script if any of the child fails
# 1  
Old 06-10-2017
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


Code:
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
# 2  
Old 06-10-2017
wait allows to specify a process ID or a job specification to wait for. man bash:
Quote:
wait [-n] [n ...]
Wait for each specified child process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If the -n option is supplied, wait waits for any job to terminate and returns its exit status. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-10-2017
one Idea came into my mind is

pass the parent process id to child and kill parent in child process if child process fails.
correct me if I am doing it in wrong way.

Code:
#!/bin/ksh
P_PID=$$
for i in 1 2 3 4 5 6 7 8 9 10
do
child_script $i $P_PID &
done
wait

Code:
#!/bin/ksh
some code of child
ret_stat=$?
if [ "$ret_stat" -ne "0" ]
then
kill $2
fi

---------- Post updated at 01:28 AM ---------- Previous update was at 01:27 AM ----------

thanks rudic for quick reply

can you please explain me with example.

Last edited by gvkumar25; 06-10-2017 at 04:04 AM..
# 4  
Old 06-10-2017
With file1 .. file4 existing in PWD, I ran
Code:
for i in 1 2 3 4 5; do { sleep $i; ls file$i; } & done; jobs; for i in 1 2 3 4 5; do wait %$i; echo $?; done
[1] 7865
[2] 7866
[3] 7867
[4] 7869
[5] 7870
[1]   Running                 { sleep $i; ls file$i; } &
[2]   Running                 { sleep $i; ls file$i; } &
[3]   Running                 { sleep $i; ls file$i; } &
[4]-  Running                 { sleep $i; ls file$i; } &
[5]+  Running                 { sleep $i; ls file$i; } &
file1
[1]   Done                    { sleep $i; ls file$i; }
0
file2
[2]   Done                    { sleep $i; ls file$i; }
0
file3
[3]   Done                    { sleep $i; ls file$i; }
0
file4
[4]-  Done                    { sleep $i; ls file$i; }
0
ls: cannot access 'file5': No such file or directory
[5]+  Exit 2                  { sleep $i; ls file$i; }
2

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-10-2017
Thanks for your reply.
I don't want to exit the script if none of child process fails. I want to exit only if my child process fails.
I tried below code snippet more like your solution only.


Code:
files="test test2 "
pids=""
exit_counter=0
for i in $files; do
    ./test2 $i &
    p_pid="$!"
    echo "$i  process id is : $p_pid"
    pids+="$p_pid "
done
jobs -p
for pid in $pids; do
    wait $pid
    if [ $? -eq 0 ]; then
        echo "SUCCESS - Job $pid exited with a status of $?"
    else
        echo "FAILED - Job $pid exited with a status of $?"
        exit_counter=`expr "$exit_counter" + 1 `
        echo "EXIT COUNTER : $exit_counter"
    fi
done
if [ $exit_counter -ne 0 ]
then
  exit "$exit_counter"
fi

# 6  
Old 06-10-2017
I see a weakness if the child process fails without terminating.
Examples:
user input required
runaway process.
I would consider replacing the wait process with a loop that executes every 30 (?) seconds to see if all processes have finished, and if not within a reasonable time frame, perform some notification.
# 7  
Old 06-10-2017
@jgt: How then would you retrieve the children's exit stati?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

3. Shell Programming and Scripting

Child exiting and not returning to parent script

I am having a parent scripts which reads a file with child scripts name. I need to read one by one child script , execute it and 1. If child script fails send mail to the team with the log file 2. If the child script executes fine then proceed with the next child script execution. #!... (3 Replies)
Discussion started by: nw2unx123
3 Replies

4. Shell Programming and Scripting

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 ] then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"... (1 Reply)
Discussion started by: daveu7
1 Replies

5. 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

6. AIX

Parent directory seems to disappear if child fails rename

If I have a file/folder that a user does not have permission to and I try to rename it, it removes the entire parent folder. At that point it is only visible in a ls. (Not a ls -l, file, more, cd). It happens on every filesystem. This is Aix 5.3 $ cd test $ ls -la total 0 drwxr-xr-x 2 root ... (4 Replies)
Discussion started by: scriptr2be
4 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Passing a variable from a child script back to the parent

Hi I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script... (3 Replies)
Discussion started by: Andy82
3 Replies

10. 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
Login or Register to Ask a Question