check_sympa_service.sh,check_wwsympa_service.sh dont't staert/stop/kill services if packet management ist running.

This commit is contained in:
Christoph 2018-12-28 19:27:56 +01:00
parent 336cdb3a4d
commit 0f8e1e9a60
2 changed files with 226 additions and 71 deletions

View File

@ -140,6 +140,35 @@ echo_skipped() {
fi 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 # - Check some prerequisites
@ -153,6 +182,11 @@ else
terminal=false terminal=false
fi fi
# - Detect linux distribution
# -
detect_os
# - Systemd supported ? # - Systemd supported ?
# - # -
systemd=$(which systemd) systemd=$(which systemd)
@ -195,7 +229,7 @@ fi
# - second we will check '$(realpath "/usr/local/symba/bin/<command>"' # - second we will check '$(realpath "/usr/local/symba/bin/<command>"'
# - i.e /usr/local/sympa-6.2.36/bin/<command> # - i.e /usr/local/sympa-6.2.36/bin/<command>
# - # -
for _command in $wwsympa_commands ; do for _command in $sympa_commands ; do
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')" PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
if [[ -z "$PIDS" ]]; then if [[ -z "$PIDS" ]]; then
_command="$(realpath "${_command}")" _command="$(realpath "${_command}")"
@ -213,32 +247,59 @@ if $restart_needed ;then
echononl " Stop service '${service_name}' first .." echononl " Stop service '${service_name}' first .."
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
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 systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
else
echo_failed
fi
else
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
else
echo_failed
fi
fi
sleep 2 sleep 2
echononl " Start Service '${service_name}' .." echononl " Start Service '${service_name}' .."
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
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 systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
else else
echo_failed echo_failed
fi fi
fi
else else
echo_failed
fi
fi
else
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 /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
@ -247,6 +308,11 @@ if $restart_needed ;then
fi fi
fi fi
else
echo_failed
fi
fi
fi
declare -i count=0 declare -i count=0
@ -255,27 +321,31 @@ if $restart_needed ;then
((count++)) ((count++))
sleep 1 sleep 5
PIDS="" PIDS=""
NEW_PIDS="" _NEW_PIDS=""
declare -i count_2=0 declare -i count_2=0
for _command in $sympa_commands ; do for _command in $sympa_commands ; do
((count_2++)) ((count_2++))
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')" 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 if [[ -z "$PIDS" ]]; then
continue continue
else else
if [[ $count_2 -eq 1 ]] ; then if [[ $count_2 -eq 1 ]] ; then
NEW_PIDS="$PIDS" _NEW_PIDS="$PIDS"
else else
NEW_PIDS="$NEW_PIDS $PIDS" _NEW_PIDS="$_NEW_PIDS $PIDS"
fi fi
restart_sucessfully=true
fi fi
done done
restart_sucessfully=true
break break
done done
@ -284,6 +354,16 @@ if $restart_needed ;then
error "Restarting service '${service_name}' failed." error "Restarting service '${service_name}' failed."
else 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. info "Service '${service_name}' was restarted and is now up and running.
The new PIDs are $NEW_PIDS" The new PIDs are $NEW_PIDS"

View File

@ -134,6 +134,35 @@ echo_skipped() {
fi 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 # - Check some prerequisites
@ -147,6 +176,11 @@ else
terminal=false terminal=false
fi fi
# - Detect linux distribution
# -
detect_os
# - Systemd supported ? # - Systemd supported ?
# - # -
systemd=$(which systemd) systemd=$(which systemd)
@ -208,32 +242,59 @@ if $restart_needed ;then
echononl " Stop service '${service_name}' first .." echononl " Stop service '${service_name}' first .."
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
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 systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
else
echo_failed
fi
else
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
echo_done
else
echo_failed
fi
fi
sleep 2 sleep 2
echononl " Start Service '${service_name}' .." echononl " Start Service '${service_name}' .."
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
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 systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
else else
echo_failed echo_failed
fi fi
fi
else else
echo_failed
fi
fi
else
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 /etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then if [[ $? -eq 0 ]] ; then
echo_done echo_done
@ -242,6 +303,11 @@ if $restart_needed ;then
fi fi
fi fi
else
echo_failed
fi
fi
fi
declare -i count=0 declare -i count=0
@ -253,9 +319,9 @@ if $restart_needed ;then
sleep 1 sleep 1
PIDS="" PIDS=""
NEW_PIDS="" _NEW_PIDS=""
declare -i count_2=0 declare -i count_2=0
for _command in $sympa_commands ; do for _command in $wwsympa_commands ; do
((count_2++)) ((count_2++))
@ -264,13 +330,13 @@ if $restart_needed ;then
continue continue
else else
if [[ $count_2 -eq 1 ]] ; then if [[ $count_2 -eq 1 ]] ; then
NEW_PIDS="$PIDS" _NEW_PIDS="$PIDS"
else else
NEW_PIDS="$NEW_PIDS $PIDS" _NEW_PIDS="$_NEW_PIDS $PIDS"
fi fi
restart_sucessfully=true
fi fi
done done
restart_sucessfully=true
break break
done done
@ -278,6 +344,15 @@ if $restart_needed ;then
if ! $restart_sucessfully ; then if ! $restart_sucessfully ; then
error "Restarting service '${service_name}' failed." error "Restarting service '${service_name}' failed."
else 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. info "Service '${service_name}' was restarted and is now up and running.
The new PIDs are $NEW_PIDS" The new PIDs are $NEW_PIDS"