Skip to content

Instantly share code, notes, and snippets.

@chrisnew
Created January 21, 2015 14:18
Show Gist options
  • Save chrisnew/b0c1b8d310fc5ceaeac4 to your computer and use it in GitHub Desktop.
Save chrisnew/b0c1b8d310fc5ceaeac4 to your computer and use it in GitHub Desktop.
postfix-wrapper.sh for supervisord in docker. Controls postfix as it gets controlled by supervisord.
#!/usr/bin/env bash
# postfix-wrapper.sh, version 0.1.0
#
# You cannot start postfix in some foreground mode and
# it's more or less important that docker doesn't kill
# postfix and its chilren if you stop the container.
#
# Use this script with supervisord and it will take
# care about starting and stopping postfix correctly.
#
# supervisord config snippet for postfix-wrapper:
#
# [program:postfix]
# process_name = postfix
# command = /path/to/postfix-wrapper.sh
# startsecs = 0
# autorestart = false
#
trap "postfix stop" SIGINT
trap "postfix stop" SIGTERM
trap "postfix reload" SIGHUP
# force new copy of hosts there (otherwise links could be outdated)
cp /etc/hosts /var/spool/postfix/etc/hosts
# start postfix
postfix start
# lets give postfix some time to start
sleep 3
# wait until postfix is dead (triggered by trap)
while kill -0 "`cat /var/spool/postfix/pid/master.pid`"; do
sleep 5
done
@gudata
Copy link

gudata commented Feb 25, 2016

Why not just do

sleep infinity

instead of checking if the process is alive?

@chrisnew
Copy link
Author

chrisnew commented Jun 3, 2016

@gudata, good point. I’m currently looking for a more elegant solution. I once had the problem, that postfix can be shutdown externally and the script doesn’t notice it causing supervisord to think, that postfix is still alive. I’ll try to find something and update it soon.

(thanks to GitHub not sending me a notification about your comment… )

@Sispheor
Copy link

Hi! Here the Dockerfile use syslog and print in the stdout the logs.
Maybe an elegant solution?

From my side the line syslog-ng --no-caps does nothing on the centos 7 image.

@Sispheor
Copy link

So, it works with rsyslog.

This is the run script I call

#!/bin/bash

# generate aliases database
newaliases

# Change syslog to local logging and start it
rm -f /etc/rsyslog.d/listen.conf
echo "Starting rsyslogd..."
rsyslogd
sleep 1
echo "rsyslog started"

echo "Starting postfix..."
postfix start
# lets give postfix some time to start
sleep 3
echo "OK running"

tail -F /var/log/maillog

@matheusb-comp
Copy link

Thanks! I just had to change the test when running on Docker using the alpine postfix, since the PID file have spaces before the actual number.

# Check every 5 seconds if postfix is still alive
PID_FILE="/var/spool/postfix/pid/master.pid"
while true; do
  sleep 5
  # Stop this script if no PID file is available
  if [ ! -f "$PID_FILE" ]; then
    break;
  else
    # Or if the numeric PID in the file can't receive a signal
    PID="$(cat $PID_FILE | sed 's/[^0-9]*//g')"
    if ! kill -0 "$PID" 2>/dev/null; then
      break;
    fi
  fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment