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
    #8  
Old Unix and Linux 2 Weeks Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Note also that your FAILED notice in:

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

is attributing the exit status of the test command to the job on which you're reporting instead of the exit status of the job. You probably want something more like:

Code:
for pid in $pids; do
    wait $pid
    status=$?
    if [ $status -eq 0 ]; then
        echo "SUCCESS - Job $pid exited with a status of 0"
    else
        echo "FAILED - Job $pid exited with a status of $status"
        exit_counter=`expr "$exit_counter" + 1 `
        echo "EXIT COUNTER : $exit_counter"
    fi
done

but, of course, that doesn't get around the problem of wait $pid hanging forever if the job it is waiting for is hung waiting for input or has entered an infinite loop.
Sponsored Links
    #9  
Old Unix and Linux 2 Weeks Ago
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 23 June 2017, 8:14 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,939
Thanks: 2
Thanked 252 Times in 238 Posts
Quote:
Originally Posted by RudiC View Post
@jgt: How then would you retrieve the children's exit stati?

Code:
echo "begin" >test1                                                      
elapsed_time=0                                                           
while [ -s test1 ]                                                       
do                                                                       
        cat /dev/null >test1                                             
        ps -ef |grep $pid1 >test1                                        
        ps -ef |grep $pid2 >>test1                                       
        #repeat for each process                                         
        if [ -s test1 ]                                                  
        then                                                             
                sleep 30                                                 
                let elapsed_time=elapsed_time+30                         
                if [ elapsed_time -gt 300 ]                              
                then                                                     
                        echo "all processes should have finished by now" 
                        cat test1
                        exit 1                                           
                fi                                                       
        fi  
done

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 04:52 PM.