Linux/Mailserver/Fetchmail

From Wiki
#!/bin/sh
#
# Fetchmail2 init script
# Latest change: Oct 14 2012
#
### BEGIN INIT INFO
# Provides: fetchmail
# Required-Start: $network $local_fs $remote_fs $syslog
# Required-Stop: $remote_fs
# Should-Start: $mail-transport-agent exim4 $named
# Should-Stop: $mail-transport-agent exim4
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: init-Script for system wide fetchmail daemon
### END INIT INFO
#
# A fetchmailrc file containg hosts and passwords for all local users should be
# placed in /etc/fetchmailrc. Remember to make the /etc/fetchmailrc mode 600
# to avoid disclosing the users' passwords.
#
# This is a modified version designed to start multiple instances of fetchmail
# Creates an instance for each conf file in /etc/fetchmail.conf.d
#

set -e

# Defaults
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/fetchmail
fetchmailuser=fetchmail
fetchmailgroup=nogroup
OPTIONS="-v "

fetchmailddir="/var/run/fetchmail"
UIDL="/var/lib/fetchmail/.fetchmail-UIDL-cache"
START_DAEMON="no"

. /lib/lsb/init-functions

if [ -r /etc/default/fetchmail ]; then
	. /etc/default/fetchmail
fi

if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then
	echo "Edit /etc/default/fetchmail to start/stop fetchmail"
	exit 0
fi

test -f $DAEMON || exit 0

case "$1" in
	start)
		for conffile in /etc/fetchmail.conf.d/*.conf; do
			bname=$(basename "$conffile")
			fetchmailddirsub=$fetchmailddir/$bname
			if [ ! -d "$fetchmailddirsub" ]; then
				mkdir -p "$fetchmailddirsub"
				chown $fetchmailuser:$fetchmailgroup "$fetchmailddirsub"
				chmod 700 "$fetchmailddirsub"
			fi
			PIDFILE=$fetchmailddirsub/fetchmail.pid
			OPTIONS2="$OPTIONS --fetchmailrc $conffile"
			echo "start $conffile"
			FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -S -o -q -p $PIDFILE -x $DAEMON -u $fetchmailuser -c $fetchmailuser -- $OPTIONS2
		done
		;;

	stop)
		for instance in $fetchmailddir/*/*.pid; do
			fetchmailddirsub=$(dirname $instance)
			if [ -f "$instance" ]; then
				pid=$(head -1 "$instance")
				PIDFILE=$fetchmailddirsub/fetchmail.pid
				FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -K -o -p $instance -x $DAEMON -u $fetchmailuser
				# FETCHMAILHOME=$fetchmailddirsub su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1
			fi
			rmdir $fetchmailddirsub
		done
		for instance in $fetchmailddir/*.pid; do
			if [ -f "$instance" ]; then
				su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1
			fi
		done
		rm -f /var/lock/subsys/fetchmail
		success "fetchmail stopped"
		echo
		;;

	force-reload|restart)
		log_begin_msg "Restarting mail retriever agent:" "fetchmail"
		if ! start-stop-daemon -K -o -q -p $PIDFILE -x $DAEMON -u $USER; then
			log_end_msg 1
			exit 1
		fi
		sleep 1
		if start-stop-daemon -S -q -p $PIDFILE -x $DAEMON -u $USER -c $USER -- $OPTIONS; then
			log_end_msg 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	
	try-restart)
		if test -e $PIDFILE ; then
			pid=`cat $PIDFILE | sed -e 's/\s.*//'|head -n1`
			PIDDIR=/proc/$pid
			if [ -d ${PIDDIR} -a "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then
				$0 restart
				exit 0
			fi
		fi
		test -f /etc/rc`/sbin/runlevel | cut -d' ' -f2`.d/S*fetchmail* && $0 start
		;;
	
	awaken)
		log_begin_msg "Awakening mail retriever agent:" "fetchmail"
		if [ -s $PIDFILE ]; then
			start-stop-daemon -K -s 10 -q -p $PIDFILE -x $DAEMON
			log_end_msg 0
			exit 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	
	debug-run)
		echo "$0: Initiating debug run of system-wide fetchmail service..." 1>&2
		echo "$0: script will be run in debug mode, all output to forced to" 1>&2
		echo "$0: stdout. This is not enough to debug failures that only" 1>&2
		echo "$0: happen in daemon mode." 1>&2
		echo "$0: You might want to direct output to a file, and tail -f it." 1>&2
		if [ "$2" = "strace" ]; then
			echo "$0: (running debug mode under strace. See strace(1) for options)" 1>&2
			echo "$0: WARNING: strace output may contain security-sensitive info, such as" 1>&2
			echo "$0: passwords; please clobber them before sending the strace file to a" 1>&2
			echo "$0: public bug tracking system, such as Debian's." 1>&2
		fi
		echo "$0: Stopping the service..." 1>&2
		"$0" stop
		echo "$0: exit status of service stop was: $?"
		echo "$0: RUNUSER is $USER"
		echo "$0: OPTIONS would be $OPTIONS"
		echo "$0: Starting service in nodetach mode, hit ^C (SIGINT/intr) to finish run..." 1>&2
		if [ "$2" = "strace" ] ; then
			shift
			shift
			[ $# -ne 0 ] && echo "$0: (strace options are: -tt $@)" 1>&2
			su -s /bin/sh -c "/usr/bin/strace -tt $* $DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER &1
			else
			su -s /bin/sh -c "$DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER &1
		fi
		echo "$0: End of service run. Exit status was: $?"
		exit 0
		;;

	*)
		log_warning_msg "Usage: /etc/init.d/fetchmail {start|stop|restart|force-reload|awaken|debug-run}"
		log_warning_msg " start - starts system-wide fetchmail service"
		log_warning_msg " stop - stops system-wide fetchmail service"
		log_warning_msg " restart, force-reload - starts a new system-wide fetchmail service"
		log_warning_msg " awaken - tell system-wide fetchmail to start a poll cycle immediately"
		log_warning_msg " debug-run [strace [strace options...]] - start a debug run of the"
		log_warning_msg " system-wide fetchmail service, optionally running it under strace"
		exit 1
		;;
		esac

exit 0