Back to home page

Enduro/X

 
 

    


0001 Alarm handling:
0002 
0003 
0004 #!/bin/bash                                                                     
0005  
0006 #Time to wait for stuck processes before killing them                           
0007 export ALARMTIME=30
0008  
0009 PARENTPID=$$
0010  
0011 exit_timeout() {
0012     echo "Alarm signal received : killing all children"
0013     for pid in ${CHILDPIDS[@]}; do
0014             kill $pid >/dev/null 2>&1
0015     done
0016     exit
0017 }
0018  
0019 CHILDCOUNT=0
0020  
0021 for server in alice bob charlie; do
0022 CHILDCOUNT=$CHILDCOUNT+1
0023     echo "Running command on $server:"
0024     ssh $server "uname -a" &
0025     CHILDPIDS[$CHILDCOUNT]=$!
0026 done
0027  
0028 #Prepare to catch SIGALRM, call exit_timeout
0029 trap exit_timeout SIGALRM
0030  
0031 #Sleep in a subprocess, then signal parent with ALRM
0032 (sleep $ALARMTIME; kill -ALRM $PARENTPID) &
0033 #Record PID of subprocess                                                       
0034 ALARMPID=$!
0035  
0036 #Wait for child processes to complete normally
0037 wait ${CHILDPIDS[*]}
0038  
0039 echo "Alarm never reached, children exited cleanly."
0040 #Tidy up the Alarm subprocess                    
0041 kill $ALARMPID
0042 
0043