Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Fail Parent Script if any of the child fails

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-10-2017
gvkumar25 gvkumar25 is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 10 June 2017, 4:12 AM EDT
Posts: 43
Thanks: 13
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 06-10-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2017, 3:32 PM EDT
Location: Aachen, Germany
Posts: 11,177
Thanks: 287
Thanked 3,444 Times in 3,173 Posts
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.
The Following User Says Thank You to RudiC For This Useful Post:
gvkumar25 (06-10-2017)
Sponsored Links
    #3  
Old Unix and Linux 06-10-2017
gvkumar25 gvkumar25 is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 10 June 2017, 4:12 AM EDT
Posts: 43
Thanks: 13
Thanked 0 Times in 0 Posts
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 03:04 AM..
    #4  
Old Unix and Linux 06-10-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2017, 3:32 PM EDT
Location: Aachen, Germany
Posts: 11,177
Thanks: 287
Thanked 3,444 Times in 3,173 Posts
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

The Following User Says Thank You to RudiC For This Useful Post:
gvkumar25 (06-10-2017)
Sponsored Links
    #5  
Old Unix and Linux 06-10-2017
gvkumar25 gvkumar25 is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 10 June 2017, 4:12 AM EDT
Posts: 43
Thanks: 13
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #6  
Old Unix and Linux 06-10-2017
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 August 2017, 9:47 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,960
Thanks: 2
Thanked 260 Times in 243 Posts
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.
Sponsored Links
    #7  
Old Unix and Linux 06-10-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2017, 3:32 PM EDT
Location: Aachen, Germany
Posts: 11,177
Thanks: 287
Thanked 3,444 Times in 3,173 Posts
@jgt: How then would you retrieve the children's exit stati?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to exit from the parent script while the child is running? Little Shell Programming and Scripting 3 10-04-2013 11:33 AM
forking a child process and kill its parent to show that child process has init() as its parent vizz_k Shell Programming and Scripting 2 11-17-2011 12:03 AM
Parent directory seems to disappear if child fails rename scriptr2be AIX 4 05-16-2011 01:34 PM
Script, child kills parent jessy21 Shell Programming and Scripting 2 05-07-2010 02:12 AM
Parent/child Korn shell script help sasaliasim Shell Programming and Scripting 1 05-06-2008 05:09 PM



All times are GMT -4. The time now is 05:13 PM.