From daeb0c9fc4579d33aa1ef35ad9d92dfbea7c0e71 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 7 Jan 2026 18:09:39 +0100 Subject: [PATCH] Add script 'check_coolwsd_service.sh'. --- check_coolwsd_service.sh | 332 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100755 check_coolwsd_service.sh diff --git a/check_coolwsd_service.sh b/check_coolwsd_service.sh new file mode 100755 index 0000000..259ccc6 --- /dev/null +++ b/check_coolwsd_service.sh @@ -0,0 +1,332 @@ +#!/usr/bin/env bash + +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + +CONFLICTING_SCRIPTS="check_sympa_spool_msg_dir.sh" + + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +usage() { + + + [[ -n "$1" ]] && error "$1" + + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) + +\033[1mDescription\033[m + + Script checks if given service is running and tries to restart service + if it is not running. + +\033[1mOptions\033[m + + No Options available + +\033[1mExample:\033[m + + Check if apache2 service is runnin. Restart service if needed. + + $(basename $0) apache2 + +" + + clean_up 1 +} + + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + clean_up 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" + fi +} + +info (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + +ok (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" + fi +} + +warn_only_terminal () { + if $terminal ; then + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" + fi +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$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 + + warn_only_terminal "\033[1m${_script_name}\033[m is currently running, but it conflicts with this script. + + Exiting now.." + + clean_up 1 + + fi # if $_stop_running ; then + + done # for _val in $CONFLICTING_SCRIPTS ; do + +fi # if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then + + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + + +SYSTEMD_SERVICE="${1:-coolwsd}" + + +# - Systemd supported ? +# - +systemd=$(which systemd) +systemctl=$(which systemctl) + +systemd_supported=false +if [[ -n "$systemd" ]] || [[ -n "$systemctl" ]] ; then + systemd_supported=true +else + fatal "Systemd is not present!" +fi + +if ! systemctl list-unit-files --type=service | grep -q "^${SYSTEMD_SERVICE}\.service"; then + fatal "Service '${SYSTEMD_SERVICE}' not present!" + clean_up 2 +fi + + +#--------------------------------------- +#----------------------------- +# Check if service is running +#----------------------------- +#--------------------------------------- + +if $LOGGING ; then + declare -i _length="${#SYSTEMD_SERVICE}" + echo -e "\n Check if $SYSTEMD_SERVICE service is running.." + echo -en " ===============================" + declare -i i=0 + while [[ $i -lt $_length ]] ; do + echo -n "=" + ((i++)) + done + echo "" +fi + +need_restart=false +if $(systemctl is-active $SYSTEMD_SERVICE > /dev/null 2>&1) ; then + eval $(systemctl show -p MainPID ${SYSTEMD_SERVICE}) + if [[ -n "$MainPID" ]] && [[ $MainPID -gt 0 ]]; then + if $(ps ax | grep -q -E "^\s*${MainPID} " 2> /dev/null) ; then + ok "$SYSTEMD_SERVICE service is up and running." + else + error "$SYSTEMD_SERVICE service seems to be down! Trying to restart service now.." + need_restart=true + fi + else + ok "$SYSTEMD_SERVICE service is up and running." + warn "Cannot determine MainPID for Service '$SYSTEMD_SERVICE', but the status is 'active'." + fi + +else + error "$SYSTEMD_SERVICE service seems to be down! Trying to restart service now.." + need_restart=true +fi + +if $need_restart ; then + + $systemctl daemon-reload > /dev/null 2> $log_file + if [[ $? -ne 0 ]]; then + error "$(cat $log_file)" + fi + sleep 2 + $systemctl stop $SYSTEMD_SERVICE > /dev/null 2> $log_file + if [[ $? -ne 0 ]]; then + error "$(cat $log_file)" + fi + sleep 10 + $systemctl start $SYSTEMD_SERVICE > /dev/null 2> $log_file + if [[ $? -ne 0 ]]; then + error "$(cat $log_file)" + fi + + sleep 5 + if $(systemctl is-active $SYSTEMD_SERVICE > /dev/null 2>&1) ; then + eval $(systemctl show -p MainPID ${SYSTEMD_SERVICE}) + if [[ -n "$MainPID" ]] && [[ $MainPID -gt 0 ]]; then + if $(ps ax | grep -q -E "^\s*${MainPID} " 2> /dev/null) ; then + ok "$SYSTEMD_SERVICE service is now up and running. New PID is '$MainPID'" + if ! $terminal ; then + echo " [ Ok ] service is now up and running. New PID is '$MainPID'" + fi + else + error "Restarting $SYSTEMD_SERVICE service failed!" + fi + else + warn "Cannot determine MainPID for Service '$SYSTEMD_SERVICE', but the status is 'active'." + ok "$SYSTEMD_SERVICE service is up and running." + if ! $terminal ; then + echo " [ Ok ] service is now up and running. New PID is '$MainPID'" + echo " [ Warn ] Cannot determine MainPID for Service '$SYSTEMD_SERVICE', but the status is 'active'." + fi + fi + else + error "Restarting $SYSTEMD_SERVICE service failed!" + fi + +fi + +clean_up 0