check_webservice_load.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:49:08 +02:00
parent 5e312a597d
commit f6d6ee1769
2 changed files with 81 additions and 51 deletions

View File

@ -1,8 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))" working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/check_webservice_load.conf" 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 ommit_curl_check_nginx=false
vserver_guest=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" 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 # --- Read Configurations from $conf_file
@ -179,7 +144,7 @@ trap 'rm -rf "$LOCK_DIR"' 0 2 15
# Some default values # 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 if [[ ! -f "$conf_file" ]]; then
echo "" echo ""
@ -199,6 +164,18 @@ fi
# - # -
if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then 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 _stop_running=false
for _val in $CONFLICTING_SCRIPTS ; do for _val in $CONFLICTING_SCRIPTS ; do
@ -206,15 +183,21 @@ if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then
_script_name="$(basename ${_val_arr[0]})" _script_name="$(basename ${_val_arr[0]})"
if [[ -n "${_val_arr[1]}" ]] && [[ -d "${_val_arr[1]}" ]] ; then if [[ -n "${_val_arr[1]}" ]] ; then
_stop_running=true
else
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 _stop_running=true
fi fi
elif [[ -d "/tmp/${_script_name%%.*}.LOCK" ]]; then
_stop_running=true
fi fi
if $_stop_running ; then if $_stop_running ; then
@ -227,12 +210,51 @@ if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then
clean_up 1 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 done
exit 1
fi fi
## - Remove lockdir when the script finishes, or when it receives a signal
## -
#trap 'rm -rf "$LOCK_DIR"' 0 2 15
#--------------------------------------- #---------------------------------------
#----------------------------- #-----------------------------

View File

@ -22,10 +22,18 @@
# - 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_local_webservice.sh:/tmp/check_local_webservice.LOCK" # - CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh:/tmp/check_local_webservice.LOCK"