From 5e312a597d7046d8e75d2f0323f7acff2ce06243 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 23 Oct 2021 14:48:35 +0200 Subject: [PATCH] check_local_webservice.sh: add parameter 'CONFLICTING_SCRIPTS' - don't run if a conflicting other script is currently running. --- check_local_webservice.sh | 101 ++++++++++++++---------- conf/check_local_webservice.conf.sample | 12 ++- 2 files changed, 70 insertions(+), 43 deletions(-) diff --git a/check_local_webservice.sh b/check_local_webservice.sh index 019cec5..7916343 100755 --- a/check_local_webservice.sh +++ b/check_local_webservice.sh @@ -5,7 +5,7 @@ script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" -LOCK_DIR="/tmp/check_local_webservice.LOCK" +LOCK_DIR="/tmp/${script_name%%.*}.LOCK" RESTART_CHECK_FILE="/tmp/${script_name%%.*}.NEED-RESTART" @@ -179,12 +179,70 @@ TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)" # - Job is already running? # ------------- + +# - Stop here, if these give scripts are running +# - +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 + + IFS=':' read -a _val_arr <<< "${_val}" + + _script_name="$(basename ${_val_arr[0]})" + + if [[ -n "${_val_arr[1]}" ]] ; then + + if [[ "${_val_arr[1]}" = "CHECK_PROCESS_LIST" ]] ; 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 + + echo "" + echo "[ Error ]: The \"${_script_name}\" script is currently running, but it conflicts with this script." + echo "" + echo " Exiting now.." + echo "" + + clean_up 1 + + fi # if $_stop_running ; then + + done # for _val in $CONFLICTING_SCRIPTS ; do + +fi # if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then + + # - 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 SIGHUP SIGINT SIGTERM + trap "clean_up 1" SIGHUP SIGINT SIGTERM else @@ -208,45 +266,6 @@ else fi -# - Stop here, if these give scripts are running -# - -if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then - - _stop_running=false - for _val in $CONFLICTING_SCRIPTS ; do - - IFS=':' read -a _val_arr <<< "${_val}" - - _script_name="$(basename ${_val_arr[0]})" - - if [[ -n "${_val_arr[1]}" ]] && [[ -d "${_val_arr[1]}" ]] ; then - _stop_running=true - else - - 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 - fi - - if $_stop_running ; then - - echo "" - echo "[ Error ]: The \"${_script_name}\" script is currently running, but it conflicts with this script." - echo "" - echo " Exiting now.." - echo "" - - clean_up 1 - - fi - - - done -fi - - diff --git a/conf/check_local_webservice.conf.sample b/conf/check_local_webservice.conf.sample index d900d7f..6c2e99c 100644 --- a/conf/check_local_webservice.conf.sample +++ b/conf/check_local_webservice.conf.sample @@ -52,10 +52,18 @@ LOG_FILES_TO_MONITOR="" # - 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_webservice_load.sh"