check_clamav-daemon.sh: don't start/stop services if package management (apt,dpkg,..) ist running.

This commit is contained in:
Christoph 2018-12-27 19:23:45 +01:00
parent 66ad72af4c
commit 3036be6061

View File

@ -3,6 +3,90 @@
tmp_err_msg=$(mktemp) tmp_err_msg=$(mktemp)
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
else
echo " [ Error ] $*"
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
else
echo " [ Warn ] $*"
fi
echo ""
}
info (){
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo " [ Info ] $*"
fi
echo ""
}
ok (){
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
else
echo " [ Ok ] $*"
fi
echo ""
}
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// /}"
}
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# - Is this a systemd system? # - Is this a systemd system?
# - # -
if [[ "X`which systemd`" = "X" ]]; then if [[ "X`which systemd`" = "X" ]]; then
@ -12,62 +96,154 @@ else
fi fi
# - Detect linux distribution
# -
detect_os
## - restart clamv-daemon if process is not present ## - restart clamv-daemon if process is not present
## - ## -
if ! ps -ef | grep -v grep | grep /usr/sbin/clamd > /dev/null ; then if ! ps -ef | grep -v grep | grep /usr/sbin/clamd > /dev/null ; then
echo "clamd is down, so i try to start it.."
error "clamd is down, so i try to restart service .."
if $systemd_exists ; then if $systemd_exists ; then
systemctl stop clamav-daemon > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
cat $tmp_err_msg && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
fi warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
sleep 2 else
systemctl start clamav-daemon > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then systemctl stop clamav-daemon > /dev/null 2> $tmp_err_msg
cat $tmp_err_msg if [[ "$?" -ne 0 ]] ; then
fi cat $tmp_err_msg
fi
sleep 2
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
systemctl start clamav-daemon > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then
cat $tmp_err_msg
fi
fi
fi
else else
/etc/init.d/clamav-daemon stop > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
cat $tmp_err_msg && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
fi warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
sleep 2 else
/etc/init.d/clamav-daemon start > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then /etc/init.d/clamav-daemon stop > /dev/null 2> $tmp_err_msg
cat $tmp_err_msg if [[ "$?" -ne 0 ]] ; then
fi cat $tmp_err_msg
fi
sleep 2
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
/etc/init.d/clamav-daemon start > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then
cat $tmp_err_msg
fi
fi
fi
fi fi
#else
# echo "clamd is up. noting to do" sleep 10
if ! ps -ef | grep -v grep | grep /usr/sbin/clamd > /dev/null ; then
error "Restart of clamd failed, sservice is down..."
else
ok "Service 'clamd' is now up and running"
fi
else
if $terminal ; then
ok "clamd is up. noting to do"
fi
fi fi
## - restart clamv-freshclam if prozess is not present ## - restart clamv-freshclam if prozess is not present
## - ## -
if ! ps -ef | grep -v grep | grep /usr/bin/freshclam > /dev/null ; then if ! ps -ef | grep -v grep | grep /usr/bin/freshclam > /dev/null ; then
echo "freshclam is down, so i try to start it.."
error "freshclam is down, so i try to restart the service.."
if $systemd_exists ; then if $systemd_exists ; then
systemctl stop clamav-freshclam > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
cat $tmp_err_msg && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
fi warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
sleep 2 else
systemctl start clamav-freshclam > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then systemctl stop clamav-freshclam > /dev/null 2> $tmp_err_msg
cat $tmp_err_msg if [[ "$?" -ne 0 ]] ; then
fi cat $tmp_err_msg
fi
sleep 2
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
systemctl start clamav-freshclam > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then
cat $tmp_err_msg
fi
fi
fi
else else
/etc/init.d/clamav-freshclam stop > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
cat $tmp_err_msg && $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
fi warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
sleep 2 else
/etc/init.d/clamav-freshclam start > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then /etc/init.d/clamav-freshclam stop > /dev/null 2> $tmp_err_msg
cat $tmp_err_msg if [[ "$?" -ne 0 ]] ; then
fi cat $tmp_err_msg
fi
sleep 2
if ( [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ) \
&& $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then
warn "It seems that package management (apt,dpkg) is running. Omit starting the service .."
else
/etc/init.d/clamav-freshclam start > /dev/null 2> $tmp_err_msg
if [[ "$?" -ne 0 ]] ; then
cat $tmp_err_msg
fi
fi
fi
fi fi
#else
# echo "freshclam is up. noting to do" sleep 10
if ! ps -ef | grep -v grep | grep /usr/bin/freshclam > /dev/null ; then
error "Restart of freshclam failed, service is down..."
else
ok "Service 'freshclam' is now up and running"
fi
else
if $terminal ; then
ok "freshclam is up. noting to do"
fi
fi fi
rm $tmp_err_msg rm $tmp_err_msg