#!/usr/bin/env bash check_string_ps="" if [[ -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 "" echo -e " [ Fatal ] $*" echo "" echo -e "\tScript terminated.." echo "" rm -rf $LOCK_DIR exit 1 } error (){ echo "" echo -e " [ Error ] $*" echo "" } warn (){ echo "" echo -e " [ Warn ] $*" echo "" } info (){ echo "" echo -e " [ Info ] $*" echo "" } ok (){ echo "" echo -e " [ Ok ] $*" echo "" } 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 #--------------------------------------- #----------------------------- # Check if Postfix Mailservice is running #----------------------------- #--------------------------------------- if $LOGGING ; then echo -e "\n Check if Postfix Mailservice is running.." echo -e " =========================================" fi if ! ps ax | grep -e "\ ${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 stop postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log if [[ $? -ne 0 ]]; then error "$(cat ${LOCK_DIR}/err_msg.log)" fi sleep 10 $systemctl start postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log if [[ $? -ne 0 ]]; then error "$(cat ${LOCK_DIR}/err_msg.log)" fi else $postfix_init_script stop > /dev/null 2>&1 if [[ $? -ne 0 ]]; then error "Stopping Postfix Mailservice failed!" fi sleep 10 $postfix_init_script start > /dev/null 2>&1 if [[ $? -ne 0 ]]; then error "Starting Postfix Mailservice failed!" fi fi if ! ps ax | grep -e "\ ${check_string_ps}" | grep -v grep > /dev/null ; 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