check_local_webservice.sh: add parameter 'CONFLICTING_SCRIPTS' - don't run if a conflicting other script is currently running.

This commit is contained in:
Christoph 2021-10-23 14:48:35 +02:00
parent 404ab8a891
commit 5e312a597d
2 changed files with 70 additions and 43 deletions

View File

@ -5,7 +5,7 @@ script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))" working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf" 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" RESTART_CHECK_FILE="/tmp/${script_name%%.*}.NEED-RESTART"
@ -179,12 +179,70 @@ TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)"
# - Job is already running? # - 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 job already runs, stop execution..
# - # -
if mkdir "$LOCK_DIR" 2> /dev/null ; then if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal ## - 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 else
@ -208,45 +266,6 @@ else
fi 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

View File

@ -52,10 +52,18 @@ LOG_FILES_TO_MONITOR=""
# - In addition to the script, a LOCK directory can also be specified which is # - In addition to the script, a LOCK directory can also be specified which is
# - connected to it. # - 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/<base-script_name>.LOCK' is assumed.
# -
# -
# - Example: # - Example:
# - CONFLICTING_SCRIPTS=" # - CONFLICTING_SCRIPTS="
# - /root/bin/monitoring/check_webservice_load.sh # - /root/bin/monitoring/check_webservice_load.sh:CHECK_PROCESS_LIST
# - /root/bin/monitoring/check_remote_websites.sh:/tmp/check_remote_websites.LOCK # - /root/bin/monitoring/check_remote_websites.sh
# - "
# - # -
# - Defaults to: # - Defaults to:
# - CONFLICTING_SCRIPTS="/root/bin/monitoring/check_webservice_load.sh" # - CONFLICTING_SCRIPTS="/root/bin/monitoring/check_webservice_load.sh"