For various reasons I have 10 email accounts spread across various servers for the family and me, and have always used an instance of fetchmail to round-robin connect to these and download all emails locally (see my previous post about cloud email). But this means I might have to wait 5-10 minutes to get new emails! But no longer…
As it happens, all the mail servers I use support IMAP (as they should), and fetchmail supports the IMAP IDLE command, which lets you connect and then get instant notifications to changes to the mail folders (in this case, fetchmail instantly slurps new emails down to your box).
However, it can only do this if only one email account is configured, and it is not trivial to run multiple fetchmail instances (each with a different config. file pointing to a separate email account).
But, it’s also not that tricky, and essentially boils down to running each one with a unique value for the environment variable FETCHMAILHOME so that it has somewhere unique to put its PID file.
So, I wrote the following /usr/sbin/fetchmail-service script (a modified version of the one-shot /etc/init.d/ version I’ve been running for years). It creates a separate fetchmail instance for each /etc/fetchmail.conf and /etc/fetchmail.d/*.conf it finds, so each of these can have a single account with IMAP IDLE set.
For now, it still uses /etc/init.d functions; I’ll tidy that up at some point.
#!/bin/sh # # fetchmail This shell script takes care of starting and stopping # the fetchmail DAEMON. # # # chkconfig: 2345 95 5 # description: fetchmail fetchs mail from pop3/imap mail servers fetchmailuser=mail fetchmailgroup=mail fetchmailddir=/var/lib/fetchmail # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -f /usr/bin/fetchmail ] || exit 0 # See how we were called. case "$1" in start) # Start daemons. echo -n "Starting fetchmail: " update_boot_stage 'Starting email fetching service' conffile=/etc/fetchmail.conf [ -f "$conffile" ] && FETCHMAILHOME=$fetchmailddir daemon --user=$fetchmailuser /usr/bin/fetchmail --daemon 60 --syslog --fetchmailrc "$conffile" if [ ! -d "$fetchmailddir" ]; then mkdir "$fetchmailddir" chown $fetchmailuser:$fetchmailgroup "$fetchmailddir" chmod 700 "$fetchmailddir" fi for conffile in /etc/fetchmail.conf.d/*.conf; do bname=$(basename "$conffile") fetchmailddirsub=$fetchmailddir/$bname if [ ! -d "$fetchmailddirsub" ]; then mkdir "$fetchmailddirsub" chown $fetchmailuser:$fetchmailgroup "$fetchmailddirsub" chmod 700 "$fetchmailddirsub" fi [ -f "$conffile" ] && FETCHMAILHOME=$fetchmailddirsub daemon --user=$fetchmailuser /usr/bin/fetchmail --daemon 60 --syslog --fetchmailrc "$conffile" done touch /var/lock/subsys/fetchmail success "fetchmail started" echo ;; stop) # Stop daemons. echo -n "Shutting down fetchmail: " for instance in $fetchmailddir/*/*.pid; do fetchmailddirsub=$(dirname $instance) if [ -f "$instance" ]; then pid=$(head -1 "$instance") 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 ;; restart) $0 stop $0 start ;; status) status fetchmail ;; *) echo "Usage: fetchmail {start|stop|restart|status}" exit 1 esac exit 0
Cross fingers, it’s only been running with the lot for quarter of an hour, but it seems to be doing the job!
Finally, for some reason the Fedora fetchmail RPM has never come with service control scripts (hence my writing the above one). I’ve now moved the init.d version to /usr/sbin as mentioned, and written a functional systemd config. file for it that will cleanly manage all instances:
[Unit] Description=A remote mail retrieval and forwarding utility After=sendmail.service [Service] ExecStart=/usr/sbin/fetchmail-service start ExecReload=/usr/sbin/fetchmail-service restart ExecStop=/usr/sbin/fetchmail-service stop Type=forking GuessMainPID=no [Install] WantedBy=multi-user.target
Leave a Reply to fnx Cancel reply