#!/usr/bin/env bash check_string_ps="" if [[ -x /usr/lib/postfix/sbin/master ]]; then check_string_ps="/usr/lib/postfix/sbin/master" elif [[ -x "/usr/lib/postfix/master" ]] ; then check_string_ps="/usr/lib/postfix/master" elif [[ -x "/usr/libexec/postfix/master" ]] ; then check_string_ps="/usr/libexec/postfix/master" fi postfix_init_script="" if [[ -x "/etc/init.d/postfix" ]] ; then postfix_init_script="/etc/init.d/postfix" fi LOCK_DIR=`mktemp -d` #--------------------------------------- #----------------------------- # Base Function(s) #----------------------------- #--------------------------------------- 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 echo "" rm -rf $LOCK_DIR exit 1 } 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// /}" } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } #--------------------------------------- #----------------------------- # Check some prerequisites #----------------------------- #--------------------------------------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true LOGGING=true else terminal=false LOGGING=false fi if [[ -z $check_string_ps ]]; then fatal "$(basename $0): Postfix seems NOT to be installed" fi # - Systemd supported ? # - systemd=$(which systemd) systemctl=$(which systemctl) systemd_supported=false if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then systemd_supported=true else if [[ ! -x $postfix_init_script ]]; then fatal "$(basename $0): Missing Postfix Init-Script!" fi fi # - Detect linux distribution # - detect_os #--------------------------------------- #----------------------------- # Check if Postfix Mailservice is running #----------------------------- #--------------------------------------- if $LOGGING ; then echo -e "\n Check if Postfix Mailservice is running.." echo -e " =========================================" fi if ! ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep -v grep > /dev/null ; then error "Postfix Mailservice seems to be down! Trying to restart service now.." if $systemd_supported ; then $systemctl daemon-reload > /dev/null 2> ${LOCK_DIR}/err_msg.log if [[ $? -ne 0 ]]; then error "$(cat ${LOCK_DIR}/err_msg.log)" fi sleep 2 # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) # - 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. Exiting restarting the service .." else $systemctl stop postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log if [[ $? -ne 0 ]]; then error "$(cat ${LOCK_DIR}/err_msg.log)" fi sleep 10 # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) # - 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 postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log if [[ $? -ne 0 ]]; then error "$(cat ${LOCK_DIR}/err_msg.log)" fi fi fi else # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) # - 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 - Exiting restarting the service .." else $postfix_init_script stop > /dev/null 2>&1 if [[ $? -ne 0 ]]; then error "Stopping Postfix Mailservice failed!" fi sleep 10 # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) # - 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 $postfix_init_script start > /dev/null 2>&1 if [[ $? -ne 0 ]]; then error "Starting Postfix Mailservice failed!" fi fi fi fi declare -i counter=0 PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') while [[ "X${PID}" = "X" ]]; do sleep 1 PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') if [[ $counter -gt 10 ]]; then break else ((counter++)) fi done if [[ "X${PID}" = "X" ]] ; then error "Restarting Postfix Mailservice failed!" else ok "Postfix Mailservice is up and running." fi else if $LOGGING ; then ok "Postfix Mailservice is up and running." fi fi rm -rf $LOCK_DIR exit 0