From 0f8e1e9a60c825c3630751dff81c1d7191d1696f Mon Sep 17 00:00:00 2001 From: Christoph Kuchenbuch Date: Fri, 28 Dec 2018 19:27:56 +0100 Subject: [PATCH] check_sympa_service.sh,check_wwsympa_service.sh dont't staert/stop/kill services if packet management ist running. --- check_sympa_service.sh | 152 +++++++++++++++++++++++++++++---------- check_wwsympa_service.sh | 145 ++++++++++++++++++++++++++++--------- 2 files changed, 226 insertions(+), 71 deletions(-) diff --git a/check_sympa_service.sh b/check_sympa_service.sh index 9c8980c..5f18db5 100755 --- a/check_sympa_service.sh +++ b/check_sympa_service.sh @@ -140,6 +140,35 @@ echo_skipped() { fi } +detect_os () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + # ------------- # - Check some prerequisites @@ -153,6 +182,11 @@ else terminal=false fi + +# - Detect linux distribution +# - +detect_os + # - Systemd supported ? # - systemd=$(which systemd) @@ -195,7 +229,7 @@ fi # - second we will check '$(realpath "/usr/local/symba/bin/"' # - i.e /usr/local/sympa-6.2.36/bin/ # - -for _command in $wwsympa_commands ; do +for _command in $sympa_commands ; do PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')" if [[ -z "$PIDS" ]]; then _command="$(realpath "${_command}")" @@ -213,41 +247,73 @@ if $restart_needed ;then echononl " Stop service '${service_name}' first .." if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then - systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + + sleep 2 + echononl " Start Service '${service_name}' .." + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + else + echo_failed + fi + fi + + else + echo_failed + fi fi else - /etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + /etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + + sleep 2 + echononl " Start Service '${service_name}' .." + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + else + echo_failed + fi + fi + + else + echo_failed + fi fi fi - sleep 2 - - echononl " Start Service '${service_name}' .." - if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then - systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed - fi - else - /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed - fi - fi - - declare -i count=0 restart_sucessfully=false @@ -255,27 +321,31 @@ if $restart_needed ;then ((count++)) - sleep 1 + sleep 5 PIDS="" - NEW_PIDS="" + _NEW_PIDS="" declare -i count_2=0 for _command in $sympa_commands ; do ((count_2++)) PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')" + if [[ -z "$PIDS" ]]; then + _command="$(realpath "${_command}")" + PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')" + fi if [[ -z "$PIDS" ]]; then continue else if [[ $count_2 -eq 1 ]] ; then - NEW_PIDS="$PIDS" + _NEW_PIDS="$PIDS" else - NEW_PIDS="$NEW_PIDS $PIDS" + _NEW_PIDS="$_NEW_PIDS $PIDS" fi + restart_sucessfully=true fi done - restart_sucessfully=true break done @@ -284,6 +354,16 @@ if $restart_needed ;then error "Restarting service '${service_name}' failed." else + count=0 + for _pid in $_NEW_PIDS ; do + ((count++)) + if [[ $count -eq 1 ]] ; then + NEW_PIDS="$_pid" + else + NEW_PIDS="$NEW_PIDS $_pid" + fi + done + info "Service '${service_name}' was restarted and is now up and running. The new PIDs are $NEW_PIDS" diff --git a/check_wwsympa_service.sh b/check_wwsympa_service.sh index a41cc56..2fa6c49 100755 --- a/check_wwsympa_service.sh +++ b/check_wwsympa_service.sh @@ -134,6 +134,35 @@ echo_skipped() { fi } +detect_os () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + # ------------- # - Check some prerequisites @@ -147,6 +176,11 @@ else terminal=false fi + +# - Detect linux distribution +# - +detect_os + # - Systemd supported ? # - systemd=$(which systemd) @@ -208,41 +242,73 @@ if $restart_needed ;then echononl " Stop service '${service_name}' first .." if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then - systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + + sleep 2 + echononl " Start Service '${service_name}' .." + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + else + echo_failed + fi + fi + + else + echo_failed + fi fi else - /etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + /etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + + sleep 2 + echononl " Start Service '${service_name}' .." + + if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \ + && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + echo_skipped + warn "It seems that package management (apt,dpkg) is running. Omit starting the service .." + else + + /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1 + if [[ $? -eq 0 ]] ; then + echo_done + else + echo_failed + fi + fi + + else + echo_failed + fi fi fi - sleep 2 - - echononl " Start Service '${service_name}' .." - if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then - systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed - fi - else - /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1 - if [[ $? -eq 0 ]] ; then - echo_done - else - echo_failed - fi - fi - - declare -i count=0 restart_sucessfully=false @@ -253,9 +319,9 @@ if $restart_needed ;then sleep 1 PIDS="" - NEW_PIDS="" + _NEW_PIDS="" declare -i count_2=0 - for _command in $sympa_commands ; do + for _command in $wwsympa_commands ; do ((count_2++)) @@ -264,13 +330,13 @@ if $restart_needed ;then continue else if [[ $count_2 -eq 1 ]] ; then - NEW_PIDS="$PIDS" + _NEW_PIDS="$PIDS" else - NEW_PIDS="$NEW_PIDS $PIDS" + _NEW_PIDS="$_NEW_PIDS $PIDS" fi + restart_sucessfully=true fi done - restart_sucessfully=true break done @@ -278,6 +344,15 @@ if $restart_needed ;then if ! $restart_sucessfully ; then error "Restarting service '${service_name}' failed." else + count=0 + for _pid in $_NEW_PIDS ; do + ((count++)) + if [[ $count -eq 1 ]] ; then + NEW_PIDS="$_pid" + else + NEW_PIDS="$NEW_PIDS $_pid" + fi + done info "Service '${service_name}' was restarted and is now up and running. The new PIDs are $NEW_PIDS"