Linux/Mailserver/Fetchmail: Difference between revisions
< Linux | Mailserver
No edit summary |
No edit summary |
||
Line 42: | Line 42: | ||
if [ -r /etc/default/fetchmail ]; then | if [ -r /etc/default/fetchmail ]; then | ||
. /etc/default/fetchmail | . /etc/default/fetchmail | ||
fi | fi | ||
if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then | if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then | ||
echo "Edit /etc/default/fetchmail to start/stop fetchmail" | echo "Edit /etc/default/fetchmail to start/stop fetchmail" | ||
exit 0 | exit 0 | ||
fi | fi | ||
Line 53: | Line 53: | ||
case "$1" in | case "$1" in | ||
start) | start) | ||
for conffile in /etc/fetchmail.conf.d/*.conf; do | for conffile in /etc/fetchmail.conf.d/*.conf; do | ||
bname=$(basename "$conffile") | bname=$(basename "$conffile") | ||
fetchmailddirsub=$fetchmailddir/$bname | fetchmailddirsub=$fetchmailddir/$bname | ||
if [ ! -d "$fetchmailddirsub" ]; then | if [ ! -d "$fetchmailddirsub" ]; then | ||
mkdir -p "$fetchmailddirsub" | mkdir -p "$fetchmailddirsub" | ||
chown $fetchmailuser:$fetchmailgroup "$fetchmailddirsub" | chown $fetchmailuser:$fetchmailgroup "$fetchmailddirsub" | ||
chmod 700 "$fetchmailddirsub" | chmod 700 "$fetchmailddirsub" | ||
fi | fi | ||
PIDFILE=$fetchmailddirsub/fetchmail.pid | PIDFILE=$fetchmailddirsub/fetchmail.pid | ||
OPTIONS2="$OPTIONS --fetchmailrc $conffile" | OPTIONS2="$OPTIONS --fetchmailrc $conffile" | ||
echo "start $conffile" | echo "start $conffile" | ||
FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -S -o -q -p $PIDFILE -x $DAEMON -u $fetchmailuser -c $fetchmailuser -- $OPTIONS2 | FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -S -o -q -p $PIDFILE -x $DAEMON -u $fetchmailuser -c $fetchmailuser -- $OPTIONS2 | ||
done | done | ||
;; | ;; | ||
stop) | |||
for instance in $fetchmailddir/*/*.pid; do | stop) | ||
fetchmailddirsub=$(dirname $instance) | for instance in $fetchmailddir/*/*.pid; do | ||
if [ -f "$instance" ]; then | fetchmailddirsub=$(dirname $instance) | ||
pid=$(head -1 "$instance") | if [ -f "$instance" ]; then | ||
PIDFILE=$fetchmailddirsub/fetchmail.pid | pid=$(head -1 "$instance") | ||
FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -K -o -p $instance -x $DAEMON -u $fetchmailuser | PIDFILE=$fetchmailddirsub/fetchmail.pid | ||
# FETCHMAILHOME=$fetchmailddirsub su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1 | FETCHMAILHOME=$fetchmailddirsub start-stop-daemon -K -o -p $instance -x $DAEMON -u $fetchmailuser | ||
fi | # FETCHMAILHOME=$fetchmailddirsub su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1 | ||
rmdir $fetchmailddirsub | fi | ||
done | rmdir $fetchmailddirsub | ||
for instance in $fetchmailddir/*.pid; do | done | ||
if [ -f "$instance" ]; then | for instance in $fetchmailddir/*.pid; do | ||
su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1 | if [ -f "$instance" ]; then | ||
fi | su $fetchmailuser -c '/usr/bin/fetchmail --pidfile "$instance" --quit' > /dev/null 2>&1 | ||
done | fi | ||
rm -f /var/lock/subsys/fetchmail | done | ||
success "fetchmail stopped" | rm -f /var/lock/subsys/fetchmail | ||
echo | 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 | force-reload|restart) | ||
log_end_msg 1 | log_begin_msg "Restarting mail retriever agent:" "fetchmail" | ||
exit 1 | if ! start-stop-daemon -K -o -q -p $PIDFILE -x $DAEMON -u $USER; then | ||
fi | log_end_msg 1 | ||
sleep 1 | exit 1 | ||
if start-stop-daemon -S -q -p $PIDFILE -x $DAEMON -u $USER -c $USER -- $OPTIONS; then | fi | ||
log_end_msg 0 | sleep 1 | ||
else | if start-stop-daemon -S -q -p $PIDFILE -x $DAEMON -u $USER -c $USER -- $OPTIONS; then | ||
log_end_msg 1 | log_end_msg 0 | ||
exit 1 | else | ||
fi | log_end_msg 1 | ||
;; | exit 1 | ||
try-restart) | fi | ||
if test -e $PIDFILE ; then | ;; | ||
pid=`cat $PIDFILE | sed -e 's/\s.*//'|head -n1` | |||
PIDDIR=/proc/$pid | try-restart) | ||
if [ -d ${PIDDIR} -a "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then | if test -e $PIDFILE ; then | ||
$0 restart | pid=`cat $PIDFILE | sed -e 's/\s.*//'|head -n1` | ||
exit 0 | PIDDIR=/proc/$pid | ||
fi | if [ -d ${PIDDIR} -a "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then | ||
fi | $0 restart | ||
test -f /etc/rc`/sbin/runlevel | cut -d' ' -f2`.d/S*fetchmail* && $0 start | exit 0 | ||
;; | fi | ||
awaken) | fi | ||
log_begin_msg "Awakening mail retriever agent:" "fetchmail" | test -f /etc/rc`/sbin/runlevel | cut -d' ' -f2`.d/S*fetchmail* && $0 start | ||
if [ -s $PIDFILE ]; then | ;; | ||
start-stop-daemon -K -s 10 -q -p $PIDFILE -x $DAEMON | |||
log_end_msg 0 | awaken) | ||
exit 0 | log_begin_msg "Awakening mail retriever agent:" "fetchmail" | ||
else | if [ -s $PIDFILE ]; then | ||
log_end_msg 1 | start-stop-daemon -K -s 10 -q -p $PIDFILE -x $DAEMON | ||
exit 1 | log_end_msg 0 | ||
fi | exit 0 | ||
;; | else | ||
debug-run) | log_end_msg 1 | ||
echo "$0: Initiating debug run of system-wide fetchmail service..." 1>&2 | exit 1 | ||
echo "$0: script will be run in debug mode, all output to forced to" 1>&2 | fi | ||
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 | debug-run) | ||
if [ "$2" = "strace" ]; then | echo "$0: Initiating debug run of system-wide fetchmail service..." 1>&2 | ||
echo "$0: (running debug mode under strace. See strace(1) for options)" 1>&2 | echo "$0: script will be run in debug mode, all output to forced to" 1>&2 | ||
echo "$0: WARNING: strace output may contain security-sensitive info, such as" 1>&2 | echo "$0: stdout. This is not enough to debug failures that only" 1>&2 | ||
echo "$0: passwords; please clobber them before sending the strace file to a" 1>&2 | echo "$0: happen in daemon mode." 1>&2 | ||
echo "$0: public bug tracking system, such as Debian's." 1>&2 | echo "$0: You might want to direct output to a file, and tail -f it." 1>&2 | ||
fi | if [ "$2" = "strace" ]; then | ||
echo "$0: Stopping the service..." 1>&2 | echo "$0: (running debug mode under strace. See strace(1) for options)" 1>&2 | ||
"$0" stop | echo "$0: WARNING: strace output may contain security-sensitive info, such as" 1>&2 | ||
echo "$0: exit status of service stop was: $?" | echo "$0: passwords; please clobber them before sending the strace file to a" 1>&2 | ||
echo "$0: RUNUSER is $USER" | echo "$0: public bug tracking system, such as Debian's." 1>&2 | ||
echo "$0: OPTIONS would be $OPTIONS" | fi | ||
echo "$0: Starting service in nodetach mode, hit ^C (SIGINT/intr) to finish run..." 1>&2 | echo "$0: Stopping the service..." 1>&2 | ||
if [ "$2" = "strace" ] ; then | "$0" stop | ||
shift | echo "$0: exit status of service stop was: $?" | ||
shift | echo "$0: RUNUSER is $USER" | ||
[ $# -ne 0 ] && echo "$0: (strace options are: -tt $@)" 1>&2 | echo "$0: OPTIONS would be $OPTIONS" | ||
su -s /bin/sh -c "/usr/bin/strace -tt $* $DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER &1 | echo "$0: Starting service in nodetach mode, hit ^C (SIGINT/intr) to finish run..." 1>&2 | ||
else | if [ "$2" = "strace" ] ; then | ||
su -s /bin/sh -c "$DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER &1 | shift | ||
fi | shift | ||
echo "$0: End of service run. Exit status was: $?" | [ $# -ne 0 ] && echo "$0: (strace options are: -tt $@)" 1>&2 | ||
exit 0 | 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 | ||
log_warning_msg "Usage: /etc/init.d/fetchmail {start|stop|restart|force-reload|awaken|debug-run}" | fi | ||
log_warning_msg " start - starts system-wide fetchmail service" | echo "$0: End of service run. Exit status was: $?" | ||
log_warning_msg " stop - stops system-wide fetchmail service" | exit 0 | ||
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" | log_warning_msg "Usage: /etc/init.d/fetchmail {start|stop|restart|force-reload|awaken|debug-run}" | ||
exit 1 | log_warning_msg " start - starts system-wide fetchmail service" | ||
;; | log_warning_msg " stop - stops system-wide fetchmail service" | ||
esac | 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 | exit 0 | ||
</pre> | </pre> | ||
</blockquote> | </blockquote> |
Latest revision as of 17:02, 16 March 2013
- source: http://fnxweb.com/blog/2012/07/14/using-multiple-fetchmail-instances-for-instant-gratification/
#!/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