8

I'm searching for a reliable way to test if postfix is running from inside a bash script.

My first attempt was simply trying pidof postfix, which doesn't work.

Then I tried to get the postfix status:

POSTFIX_LOCATION=/var/packages/MailServer/target/sbin/postfix # location of postfix
result=`$POSTFIX_LOCATION status`
if [ -z $result ]; then
    echo "Error: No status output from postfix"
elif [[ "$result" == *"is running"* ]]; then
    echo "postfix is running!"
else echo "postfix is not running!"
fi

But even though the status is reported to the console, the result variable stays empty. This is the console output:

postfix/postfix-script: the Postfix mail system is running: PID: 11996
Error: No status output from postfix

I finally found a way to test if postfix is running by getting the process name of PID: 11996, which is "master". So the following does work:

pidof master

But this is not very verbose and I'm not sure if this is a reliable way to test if postfix is running.

So my questions are:

  • How can I get the output of postfix status from inside a bash script? - Anything I'm doing wrong there?
  • Is there a better reliable way to test if postfix is running from inside a bash script?
  • 1
    Possible status outputs to stderr? Try to add 2>&1 to status-command. Also you can use pgrep postfix – Costas Feb 28 '15 at 7:52
  • I tried 2>&1 but the result stays empty. Sadly pgrep is no option because it is not available on the system (a Synology disk station with DSM 5.1). – Balder Feb 28 '15 at 8:05
  • 1
    Does stackoverflow.com/questions/226699/… help? – garethTheRed Feb 28 '15 at 8:33
  • 1
    ps aux | grep [p]ostfix ? – Costas Feb 28 '15 at 8:41
  • Thanks @Costas and @garethTheRed - using ps seems to work. On the DSM system ps doesn't accept any parameters other then w. But simply calling ps | grep [p]ostfix does work. – Balder Feb 28 '15 at 8:49
8

Debian or Ubuntu

sudo service postfix status
  • Sorry I though it was fro terminal – Javo Troya Sep 9 '15 at 23:14
3

To check whether Postfix is running or not:

sudo /etc/init.d/postfix status

To start the Postfix:

sudo /etc/init.d/postfix start

To stop the postfix:

sudo /etc/init.d/postfix stop
  • 1
    I can type postfix start without an error message, but when I type postfix status it says "the Postfix mail system is not running." I suppose it's silently failing. – PJ Brunet Feb 11 '17 at 19:14
1

A reliable way to test if postfix is running:

if /var/packages/MailServer/target/sbin/postfix status; then
    echo "postfix is running!"
else echo "postfix is not running!"
fi

An alternative that essentially does the same:

if /var/packages/MailServer/target/libexec/master -t 2>/dev/null; then
    echo "postfix is not running!"
else echo "postfix is running!"
fi

Reasoning

After doing some more research, it turned out that testing for ps | grep [p]ostfix is not reliable at all to verify if postfix is running.

It seems the reason why /var/packages/MailServer/target/sbin/postfix status does not output anything useful is, because postfix uses the internal postlog binary for the output. The relevant code can be found in the bash script /var/packages/MailServer/target/libexec/postfix-script:

LOGGER="$command_directory/postlog -t $MAIL_LOGTAG/postfix-script"
INFO="$LOGGER -p info"

Checking for the exit code of postfix status does work fine on the other hand. The postfix-script is actually called behind the scenes by the postfix binary and cannot be called directly. But luckily the script also includes the code to test if postfix is running:

status)
    $daemon_directory/master -t 2>/dev/null && {
        $INFO the Postfix mail system is not running
        exit 1
    }
    $INFO the Postfix mail system is running: PID: `sed 1q pid/master.pid`
    exit 0
    ;;

The variable $daemon_directory gets set by the calling postfix binary. On my system it resolves to /var/packages/MailServer/target/libexec/.

  • After doing some more research Could you support your claims by providing evidence? – toogley May 7 '16 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.