SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ###########################################################
  4. ###########################################################
  5. ###########################################################
  6. ###########################################################
  7. ###### Author - Vaibhav Sharma (vaebhav)
  8. ###### Date Created -
  9. ###########################################################
  10. ###########################################################
  11. ###########################################################
  12.  
  13. ##########
  14. #### The Wrapper ensures only 1 instance of the Script is executed , Skips Scheduled runs if an instance is already in execution.
  15. #### Maintains a PIDFILE for each execution schedule , and triggers an email alert first @ 5 skips and finally an 10 skips and kiling any subsequent child processes spawned
  16. #### Intermediate Skips can be customized by changing the value for RUNTHRES
  17. #### Maintains a INFOLOG file for its execution
  18. ##########
  19.  
  20.  
  21. ###Script which you want to schedule.
  22. ### Ex - /home/vaebhav/Test.py
  23.  
  24. SCRIPT="<Script Path>"
  25. PIDFILE=~/work/$0.pid
  26. SKIPCOUNT=~/work/$0.runcount
  27.  
  28. RED="RED"
  29. CRITICAL="CRITICAL"
  30. ERRLOG=~/work/$0.err
  31. INFOLOG=~/work/$0.info
  32.  
  33.  
  34.  
  35. mail_alert(){
  36.  
  37. echo "Sending Mail Alert [$2] for $0 Shell RunCount Threshold - [$$] [$(date +"%T %D")] " >> $INFOLOG
  38.  
  39. subj=" $(hostname) [$1] Execution Threshold Skipped for $0 Process"
  40.  
  41. if [[ "$3" == "FALSE" ]]
  42. then
  43.     eMsg="$0 Process has skipped Threshold count of $2 . PID for long running Process can be found at - $PIDFILE"
  44. elif [[ "$3" == "TRUE" ]]
  45. then
  46.     eMsg="$0 Process has skipped Threshold count of $2 . PID for long running Process can be found at - $PIDFILE. Resetting Runcount to - 0. Killing All subsequent child processes as well."
  47. fi
  48. eTo=" Curate the email list for Notifications"
  49.  
  50.         #echo $eMsg | mail -s "$subj" $eTo
  51.     mail -s "$subj"  $eTo <<- EMB
  52.  
  53.     $eMsg
  54.  
  55.         -----------------------------------------------------------------
  56.         Processed at host - $(hostname) -  $(date +"%m-%d-%Y %T %Z")
  57.         ------------------------------------------------------------------
  58. EMB
  59.  
  60. }
  61.  
  62. remove_pid(){
  63.  
  64.     echo "Removing PID File of $0 Shell due to $1 - [$$] [$(date +"%T %D")] " >> $INFOLOG
  65.  
  66.         if [ -f $PIDFILE ]
  67.         then
  68.                 rm $PIDFILE
  69.  
  70.         fi
  71.  
  72.     if [ -f $SKIPCOUNT ]
  73.         then
  74.                 RUNTHRES=0
  75.                 echo $RUNTHRES > $SKIPCOUNT
  76.         fi
  77.  
  78. }
  79.  
  80. control_c() {
  81.  
  82.     echo "Removing PID File of $0 Shell due to Cntrl - C SIGNAL Trap - [$$] [$(date +"%T %D")] " >> $INFOLOG
  83.     remove_pid
  84.     exit
  85. }
  86.  
  87. shutdown() {
  88.   # Get our process group id
  89.   PGID=$(ps -o pgid= $$ | grep -o [0-9]*)
  90.  
  91.   # Kill it in a new new process group
  92.   setsid kill -- -$PGID
  93.   exit 0
  94. }
  95.  
  96. #trap "shutdown" SIGINT SIGTERM
  97.  
  98. kill_child(){
  99.  
  100. WAV_ARRAY=($(ps -ef | grep "$0" | awk '{print $2}'));
  101. echo "CHILD PROCESS ARRAY - ${WAV_ARRAY[@]} ----- " >> $INFOLOG
  102. for cid in "${WAV_ARRAY[@]}"
  103. do
  104.     killCmd=$(kill -9 $cid)
  105.     if [ $? -ne 0 ]
  106.     then
  107.         echo "Killing the child process $cid did not succeded" >> $INFOLOG
  108.     else
  109.         echo "Killed Child Process - [$cid] " >> $INFOLOG
  110.     fi
  111.  
  112. done
  113. remove_pid "[CRITICAL] LONG RUNNING CHILD PROCESSES"
  114. ParentKill=$(kill -9 $PID)
  115. }
  116.  
  117. trap control_c SIGINT
  118.  
  119.  
  120. echo "--------------------- Starting $0  -----------------------" >> $INFOLOG
  121.  
  122. if [ -f $PIDFILE ]
  123. then
  124.   PID=$(cat $PIDFILE)
  125.   ps -p $PID > /dev/null 2>&1
  126.   if [ $? -eq 0 ]
  127.   then
  128.     echo "$0 Shell Already Running Skipping the Immediate Schedule - [$$] [$(date +"%T %D")] " >> $INFOLOG
  129.     if [ -f $SKIPCOUNT ]
  130.     then
  131.     COUNT=1
  132.     RUNTHRES=$(cat $SKIPCOUNT)
  133.     RUNTHRES=$(($RUNTHRES+$COUNT))
  134.     if [[ $RUNTHRES == 5 ]]
  135.     then
  136.         mail_alert $RED $RUNTHRES "FALSE"
  137.     elif [[ $RUNTHRES == 10 ]]
  138.     then
  139.         mail_alert $CRITICAL $RUNTHRES "TRUE"
  140.         kill_child
  141.         echo "Killing Child process of $0_Shell.sh i.e Win_API_Int_Test.py " >> $INFOLOG
  142.         RUNTHRES=0
  143.         echo $RUNTHRES > $SKIPCOUNT
  144.     else
  145.                 echo $RUNTHRES > $SKIPCOUNT
  146.     fi
  147.     echo "Current $0 Shell RunCount - [$RUNTHRES] [$(date +"%T %D")] " >> $INFOLOG
  148.     else
  149.     RUNTHRES=0
  150.     echo $RUNTHRES > $SKIPCOUNT
  151.     fi
  152.     exit 1
  153.   else
  154.     ## Process not found assume not running
  155.     echo $$ > $PIDFILE
  156.     if [ $? -ne 0 ]
  157.     then
  158.       echo "Could not create PID file - [$$] [$(date +"%T %D")] " >> $ERRLOG
  159.       exit 1
  160.     fi
  161.   fi
  162. else
  163.   echo $$ > $PIDFILE
  164.   if [ $? -ne 0 ]
  165.   then
  166.     echo "Could not create PID file - [$$] [$(date +"%T %D")] " >> $ERRLOG
  167.     exit 1
  168.   fi
  169. fi
  170.  
  171.  
  172. if [ -f $SCRIPT ]
  173. then
  174.     cmd=$(python $SCRIPT)
  175.     if [ $? -eq 0 ]
  176.     then
  177.         remove_pid "SUCCESS"
  178.         echo "$SCRIPT was succesfully executed, Exiting Shell script [$(date +"%T %D")] " >> $INFOLOG
  179.         exit 0
  180.     elif [ $? -eq 1 ]
  181.     then
  182.         remove_pid "EXCEPTION"
  183.         echo "$SCRIPT was unsuccesfully, Exiting Shell script [$(date +"%T %D")] " >> $INFOLOG
  184.         exit 1
  185.     fi
  186. else
  187.     echo "$SCRIPT was not found, Exiting Shell script [$(date +"%T %D")]" >> $INFOLOG
  188.     echo "$SCRIPT was not found, Exiting Shell script [$(date +"%T %D")] " >> $ERRLOG
  189.     exit 1
  190. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top