aws-smtp-relay
aws-smtp-relay copied to clipboard
Code offer: init scripts
Expected behavior
Starting and stopping the SMTP relay on machine start/stop.
Actual behavior
Steps to reproduce the behavior
/usr/local/sbin/start-aws-smtp-relay.sh:
#!/bin/sh
PIDFILE=/var/run/aws-smtp-relay.pid
if [ -e $PIDFILE ]; then
echo "Found $PIDFILE - relay already running?"
ps -p `head -1 $PIDFILE` > /dev/null && exit 1 || echo "Relay process not found; starting..."
fi
java -jar /usr/share/aws-smtp-relay/aws-smtp-relay-1.0.0-jar-with-dependencies.jar -r us-east-1 &
echo $! > $PIDFILE
/usr/local/sbin/stop-aws-smtp-relay.sh:
#!/bin/sh
PIDFILE=/var/run/aws-smtp-relay.pid
if [ -e $PIDFILE ]; then
head -1 $PIDFILE | xargs kill
rm $PIDFILE
fi
/etc/init.d/aws-smtp-relay:
#!/bin/sh
### BEGIN INIT INFO
# Provides: aws-smtp-relay
# Required-Start: $remote_fs $network $named
# Required-Stop: $remote_fs
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Relay SMTP traffic to AWS SES
# Description: Mail relay to convert SMTP traffic to Amazon Simple Email Service API calls.
### END INIT INFO
PIDFILE=/var/run/aws-smtp-relay.pid
case $1 in
start)
/bin/sh /usr/local/sbin/start-aws-smtp-relay.sh
;;
stop)
/bin/sh /usr/local/sbin/stop-aws-smtp-relay.sh
;;
status)
if [ -e $PIDFILE ]; then
PID=`head -1 $PIDFILE`
fi
if [ "$PID" == "" ]; then
echo "AWS SMTP relay is not running"
else
echo "AWS SMTP relay is running with PID $PID"
fi
;;
restart)
/bin/sh /usr/local/sbin/stop-aws-smtp-relay.sh
/bin/sh /usr/local/sbin/start-aws-smtp-relay.sh
;;
esac
exit 0
This could also be adapted to replace the default mail sender, of course, by setting the port to 25 and disabling the other sender.
initd is getting old, I know, but it's still used by Amazon Linux 1, which appears to be the version you get if you start OpsWorks instances without specifying a custom AMI.
You can also look at the Pull Request #22
Nice to know, thanks. The servers we're working on don't use systemd though.
(I did search for existing issues before posting, just didn't check pull requests.)
I'm surprised that more people aren't taking this approach. Google can't seem to find anyone except you guys. Everyone just generates smtp credentials. But rotating those for CIS compliance is a proper pain when you have lots of applications.