#!/usr/bin/env bash 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? # - if [[ "X`which systemd`" = "X" ]]; then systemd_exists=false else systemd_exists=true fi # - Detect linux distribution # - detect_os ## - restart clamv-daemon if process is not present ## - if ! ps -ef | grep -v grep | grep /usr/sbin/clamd > /dev/null ; then error "clamd is down, so i try to restart service .." if $systemd_exists ; then 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 stop clamav-daemon > /dev/null 2> $tmp_err_msg if [[ "$?" -ne 0 ]] ; then 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 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 stop > /dev/null 2> $tmp_err_msg if [[ "$?" -ne 0 ]] ; then 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 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 ## - restart clamv-freshclam if prozess is not present ## - if ! ps -ef | grep -v grep | grep /usr/bin/freshclam > /dev/null ; then error "freshclam is down, so i try to restart the service.." if $systemd_exists ; then 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 stop clamav-freshclam > /dev/null 2> $tmp_err_msg if [[ "$?" -ne 0 ]] ; then 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 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 stop > /dev/null 2> $tmp_err_msg if [[ "$?" -ne 0 ]] ; then 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 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 rm $tmp_err_msg exit 0