Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###########################################################
- ###########################################################
- ###########################################################
- ###########################################################
- ###### Author - Vaibhav Sharma (vaebhav)
- ###### Date Created -
- ###########################################################
- ###########################################################
- ###########################################################
- ##########
- #### The Wrapper ensures only 1 instance of the Script is executed , Skips Scheduled runs if an instance is already in execution.
- #### 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
- #### Intermediate Skips can be customized by changing the value for RUNTHRES
- #### Maintains a INFOLOG file for its execution
- ##########
- ###Script which you want to schedule.
- ### Ex - /home/vaebhav/Test.py
- SCRIPT="<Script Path>"
- PIDFILE=~/work/$0.pid
- SKIPCOUNT=~/work/$0.runcount
- RED="RED"
- CRITICAL="CRITICAL"
- ERRLOG=~/work/$0.err
- INFOLOG=~/work/$0.info
- mail_alert(){
- echo "Sending Mail Alert [$2] for $0 Shell RunCount Threshold - [$$] [$(date +"%T %D")] " >> $INFOLOG
- subj=" $(hostname) [$1] Execution Threshold Skipped for $0 Process"
- if [[ "$3" == "FALSE" ]]
- then
- eMsg="$0 Process has skipped Threshold count of $2 . PID for long running Process can be found at - $PIDFILE"
- elif [[ "$3" == "TRUE" ]]
- then
- 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."
- fi
- eTo=" Curate the email list for Notifications"
- #echo $eMsg | mail -s "$subj" $eTo
- mail -s "$subj" $eTo <<- EMB
- $eMsg
- -----------------------------------------------------------------
- Processed at host - $(hostname) - $(date +"%m-%d-%Y %T %Z")
- ------------------------------------------------------------------
- EMB
- }
- remove_pid(){
- echo "Removing PID File of $0 Shell due to $1 - [$$] [$(date +"%T %D")] " >> $INFOLOG
- if [ -f $PIDFILE ]
- then
- rm $PIDFILE
- fi
- if [ -f $SKIPCOUNT ]
- then
- RUNTHRES=0
- echo $RUNTHRES > $SKIPCOUNT
- fi
- }
- control_c() {
- echo "Removing PID File of $0 Shell due to Cntrl - C SIGNAL Trap - [$$] [$(date +"%T %D")] " >> $INFOLOG
- remove_pid
- exit
- }
- shutdown() {
- # Get our process group id
- PGID=$(ps -o pgid= $$ | grep -o [0-9]*)
- # Kill it in a new new process group
- setsid kill -- -$PGID
- exit 0
- }
- #trap "shutdown" SIGINT SIGTERM
- kill_child(){
- WAV_ARRAY=($(ps -ef | grep "$0" | awk '{print $2}'));
- echo "CHILD PROCESS ARRAY - ${WAV_ARRAY[@]} ----- " >> $INFOLOG
- for cid in "${WAV_ARRAY[@]}"
- do
- killCmd=$(kill -9 $cid)
- if [ $? -ne 0 ]
- then
- echo "Killing the child process $cid did not succeded" >> $INFOLOG
- else
- echo "Killed Child Process - [$cid] " >> $INFOLOG
- fi
- done
- remove_pid "[CRITICAL] LONG RUNNING CHILD PROCESSES"
- ParentKill=$(kill -9 $PID)
- }
- trap control_c SIGINT
- echo "--------------------- Starting $0 -----------------------" >> $INFOLOG
- if [ -f $PIDFILE ]
- then
- PID=$(cat $PIDFILE)
- ps -p $PID > /dev/null 2>&1
- if [ $? -eq 0 ]
- then
- echo "$0 Shell Already Running Skipping the Immediate Schedule - [$$] [$(date +"%T %D")] " >> $INFOLOG
- if [ -f $SKIPCOUNT ]
- then
- COUNT=1
- RUNTHRES=$(cat $SKIPCOUNT)
- RUNTHRES=$(($RUNTHRES+$COUNT))
- if [[ $RUNTHRES == 5 ]]
- then
- mail_alert $RED $RUNTHRES "FALSE"
- elif [[ $RUNTHRES == 10 ]]
- then
- mail_alert $CRITICAL $RUNTHRES "TRUE"
- kill_child
- echo "Killing Child process of $0_Shell.sh i.e Win_API_Int_Test.py " >> $INFOLOG
- RUNTHRES=0
- echo $RUNTHRES > $SKIPCOUNT
- else
- echo $RUNTHRES > $SKIPCOUNT
- fi
- echo "Current $0 Shell RunCount - [$RUNTHRES] [$(date +"%T %D")] " >> $INFOLOG
- else
- RUNTHRES=0
- echo $RUNTHRES > $SKIPCOUNT
- fi
- exit 1
- else
- ## Process not found assume not running
- echo $$ > $PIDFILE
- if [ $? -ne 0 ]
- then
- echo "Could not create PID file - [$$] [$(date +"%T %D")] " >> $ERRLOG
- exit 1
- fi
- fi
- else
- echo $$ > $PIDFILE
- if [ $? -ne 0 ]
- then
- echo "Could not create PID file - [$$] [$(date +"%T %D")] " >> $ERRLOG
- exit 1
- fi
- fi
- if [ -f $SCRIPT ]
- then
- cmd=$(python $SCRIPT)
- if [ $? -eq 0 ]
- then
- remove_pid "SUCCESS"
- echo "$SCRIPT was succesfully executed, Exiting Shell script [$(date +"%T %D")] " >> $INFOLOG
- exit 0
- elif [ $? -eq 1 ]
- then
- remove_pid "EXCEPTION"
- echo "$SCRIPT was unsuccesfully, Exiting Shell script [$(date +"%T %D")] " >> $INFOLOG
- exit 1
- fi
- else
- echo "$SCRIPT was not found, Exiting Shell script [$(date +"%T %D")]" >> $INFOLOG
- echo "$SCRIPT was not found, Exiting Shell script [$(date +"%T %D")] " >> $ERRLOG
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement