From f6d6ee1769bf06ee43641f746e54e34494eab81d Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 23 Oct 2021 14:49:08 +0200 Subject: [PATCH] check_webservice_load.sh: add parameter 'CONFLICTING_SCRIPTS' - don't run if a conflicting other script is currently running. --- check_webservice_load.sh | 120 +++++++++++++++---------- conf/check_webservice_load.conf.sample | 12 ++- 2 files changed, 81 insertions(+), 51 deletions(-) diff --git a/check_webservice_load.sh b/check_webservice_load.sh index fd8714c..7baad77 100755 --- a/check_webservice_load.sh +++ b/check_webservice_load.sh @@ -1,8 +1,15 @@ #!/usr/bin/env bash +script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/check_webservice_load.conf" +# - Lock directory exists, until the script ends. So +# - we can check, if a previos instanze is already running. +# - +LOCK_DIR="/tmp/${script_name%%.*}.LOCK" +#LOCK_DIR=`mktemp -d` + #--------------------------------------- #----------------------------- @@ -24,12 +31,6 @@ check_website=false ommit_curl_check_nginx=false vserver_guest=false -# - Lock directory exists, until the script ends. So -# - we can check, if a previos instanze is already running. -# - -#LOCK_DIR=/tmp/check_webservice.lock -LOCK_DIR=`mktemp -d` - #--------------------------------------- #----------------------------- @@ -136,42 +137,6 @@ trim() { echo -n "$var" } -## - #--------------------------------------- -## - #----------------------------- -## - # Job is already running? -## - #----------------------------- -## - #--------------------------------------- -## - -## - ## - If job already runs, stop execution.. -## - ## - -## - if mkdir "$LOCK_DIR" 2> /dev/null ; then -## - -## - ## - Remove lockdir when the script finishes, or when it receives a signal -## - trap 'rm -rf "$LOCK_DIR"' 0 2 15 -## - -## - else -## - -## - datum=`date +"%d.%m.%Y"` -## - -## - msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.." -## - -## - echo "" -## - echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running." -## - echo "" -## - echo -e "\tExiting now.." -## - echo "" -## - -## - for _to_address in $to_addresses ; do -## - echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ -## - | sendmail -F "Error `hostname -f`" -f $from_address $_to_address -## - done -## - -## - exit 1 -## - -## - fi - -## - Remove lockdir when the script finishes, or when it receives a signal -trap 'rm -rf "$LOCK_DIR"' 0 2 15 # ------------- # --- Read Configurations from $conf_file @@ -179,7 +144,7 @@ trap 'rm -rf "$LOCK_DIR"' 0 2 15 # Some default values # -DEFAULT_CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh:/tmp/check_local_webservice.LOCK" +DEFAULT_CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh" if [[ ! -f "$conf_file" ]]; then echo "" @@ -199,6 +164,18 @@ fi # - if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then + # - Try using a random start delay to prevent (or at least have a small chance) that + # - conflicting scripts will both/all abort if they start at the same time. + # - + # - !! Notice !! + # - This only makes sense if a fixed LOCK directory is used, otherwise the process list + # - (and NOT the LOCK-directory) is used to look for scripts running in parallel. + # - + if [[ "$LOCK_DIR" = "/tmp/${script_name%%.*}.LOCK" ]]; then + _shift="$(( $RANDOM % 10 + 1 ))" + sleep $(( $RANDOM % 25 + $_shift )) + fi + _stop_running=false for _val in $CONFLICTING_SCRIPTS ; do @@ -206,15 +183,21 @@ if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then _script_name="$(basename ${_val_arr[0]})" - if [[ -n "${_val_arr[1]}" ]] && [[ -d "${_val_arr[1]}" ]] ; then - _stop_running=true - else + if [[ -n "${_val_arr[1]}" ]] ; then - check_string_ps="${_val_arr[0]}" + if [[ "${_val_arr[1]}" = "CHECK_PROCESS_LIST" ]] ; then - if ps -e f | grep -E "\s+${check_string_ps}" | grep -v grep | grep -v -E "\s+vim\s+" > /dev/null ; then + check_string_ps="${_val_arr[0]}" + if ps -e f | grep -E "\s+${check_string_ps}" | grep -v grep | grep -v -E "\s+vim\s+" > /dev/null ; then + _stop_running=true + fi + + elif [[ -d "${_val_arr[1]}" ]] ; then _stop_running=true fi + + elif [[ -d "/tmp/${_script_name%%.*}.LOCK" ]]; then + _stop_running=true fi if $_stop_running ; then @@ -227,12 +210,51 @@ if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then clean_up 1 - fi + fi # if $_stop_running ; then + done # for _val in $CONFLICTING_SCRIPTS ; do +fi # if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then + + +#--------------------------------------- +#----------------------------- +# Job is already running? +#----------------------------- +#--------------------------------------- + +## - If job already runs, stop execution.. +## - +if mkdir "$LOCK_DIR" 2> /dev/null ; then + + ## - Remove lockdir when the script finishes, or when it receives a signal + trap "clean_up 1" SIGHUP SIGINT SIGTERM + +else + + datum=`date +"%d.%m.%Y"` + + msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.." + + echo "" + echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running." + echo "" + echo -e "\tExiting now.." + echo "" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ + | sendmail -F "Error `hostname -f`" -f $from_address $_to_address done + + exit 1 + fi +## - Remove lockdir when the script finishes, or when it receives a signal +## - +#trap 'rm -rf "$LOCK_DIR"' 0 2 15 + #--------------------------------------- #----------------------------- diff --git a/conf/check_webservice_load.conf.sample b/conf/check_webservice_load.conf.sample index 0adabfc..4d859a1 100644 --- a/conf/check_webservice_load.conf.sample +++ b/conf/check_webservice_load.conf.sample @@ -22,10 +22,18 @@ # - In addition to the script, a LOCK directory can also be specified which is # - connected to it. # - +# - If no fixed LOCK directory is connected to the script, set +# - this value to the constant 'CHECK_PROCESS_LIST'. +# - +# - If no value for the LOCK directory is given, the LOCK directory +# - '/tmp/.LOCK' is assumed. +# - +# - # - Example: # - CONFLICTING_SCRIPTS=" -# - /root/bin/monitoring/check_webservice_load.sh -# - /root/bin/monitoring/check_remote_websites.sh:/tmp/check_remote_websites.LOCK +# - /root/bin/monitoring/check_webservice_load.sh:CHECK_PROCESS_LIST +# - /root/bin/monitoring/check_remote_websites.sh +# - " # - # - Defaults to: # - CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh:/tmp/check_local_webservice.LOCK"