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!"

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? 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

Debian or Ubuntu

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

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!"

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!"


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:

    $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

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

I'm running postfix inside a very lean docker container and has very few normal commands installed. Yet it does have netstat. So use netstat to list ports that a process is listening on. Look for a process listening on the smtp port (e.g. postfix).

if ! netstat -l | grep smtp; then
    echo "postfix is not running!"
else echo "postfix is running!"
  • 2
    Can't you just do if postfix status; then echo running; fi? You shouldn't need any other command than postfix here as postfix itself can tell its own service status. Oct 31 '20 at 17:27
  • Stéphane Chazelas is correct and all the other dances are not needed. Thanks Stéphane
    – Bryan
    Nov 25 '20 at 14:42

I believe the suggested solutions with "master -t" and "postfix status" may incorrectly report that postfix is running if it was previously killed forcibly leaving its PID files behind.

"pidof master" and similar ps/grep manipulations are fine, except there might be other processes called "master".

So "sudo service postfix status" might be the best (except OP is running DSM where this is not applicable).


You can use the is-active command:

if (systemctl is-active -q postfix); then
  echo "Success: postfix is running."
  echo "Error: postfix is not running!"

Here's the documentation for 'is-active'.

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.