check_systemd_service.sh: add support of 'conflicting scripts'.

This commit is contained in:
Christoph 2022-10-17 00:39:12 +02:00
parent 4dffba46b9
commit c3b5200e83

View File

@ -3,6 +3,9 @@
LOCK_DIR="/tmp/$(basename $0).$$.LOCK" LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log" log_file="${LOCK_DIR}/${script_name%%.*}.log"
CONFLICTING_SCRIPTS="check_sympa_spool_msg_dir.sh"
#--------------------------------------- #---------------------------------------
#----------------------------- #-----------------------------
# Base Function(s) # Base Function(s)
@ -125,6 +128,16 @@ trim() {
echo -n "$var" echo -n "$var"
} }
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
# ---------- # ----------
# - Jobhandling # - Jobhandling
@ -139,6 +152,67 @@ trap clean_up SIGHUP SIGINT SIGTERM
mkdir "$LOCK_DIR" mkdir "$LOCK_DIR"
# - 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.
# -
# - Skip delay if running in an terminal (from copnsole)
# -
if ! $terminal ; then
if [[ "$LOCK_DIR" = "/tmp/${script_name%%.*}.LOCK" ]]; then
_shift="$(( $RANDOM % 10 + 1 ))"
sleep $(( $RANDOM % 25 + $_shift ))
fi
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
#--------------------------------------- #---------------------------------------
#----------------------------- #-----------------------------
@ -146,16 +220,6 @@ mkdir "$LOCK_DIR"
#----------------------------- #-----------------------------
#--------------------------------------- #---------------------------------------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
if [[ -n "$1" ]] ; then if [[ -n "$1" ]] ; then
service_name=$1 service_name=$1
else else