Taking reference from following FetchMail Daemon
Fetchmail init script for RHEL
This post will assume you just have a fully functional working mail server and you want do download all emails from some mailboxes located on another mail server, tipically located on your service provider’s one.
This post guide will guide you to install and configure fetchmail and use it with your RHEL 5 or newer, you should use all the following information also with CentOS without change any line, but you shoud adapt it to every Linux distribution.
Most likely you want to setup fetchmail to download your domain mailbox over pop3 and inject into Postfix (or whatever you use as MTA).
To install fetchmail use the following :
yum install fetchmail -y
You should decide how to use fetchmail :
* as a daemon
* as a cron scheduled task
If you want to run it as a daemon you must :
Edit the fetchmail main configuration file /etc/fetchmail.conf as following :
set postmaster “postmaster”
set bouncemail
set no spambounce
set properties “”
set daemon 300poll yourexternalamailhost1.com with proto POP3 auth password
user ‘ext_username1′ there with password ‘ext_password1′ to ‘internal_mailbox1′ here
user ‘ext_username2′ there with password ‘ext_password2′ to ‘internal_mailbox2′ herepoll yourexternalamailhost12.net with proto POP3 auth password
user ‘ext_username1′ there with password ‘ext_password1′ to ‘internal_mailbox1′ here
user ‘ext_username2′ there with password ‘ext_password2′ to ‘internal_mailbox2′ here
For some strange reason, there is no more init.d script for fetchmail on RHEL 5 (or CentOS), so I wrote a simple one that seems to work good.
I’m using it on some production mail server.
/etc/init.d/fetchmail :
#!/bin/sh
#
# FETCHMAIL Control Script
# chkconfig: 2345 99 01
# Description: Here is a little startup/shutdown script for RedHat systems
#
# processname: fetchmail
# pidfile: /var/run/fetchmail.pid
# config: /etc/fetchmail.conf
#
# Author : Riccardo Riva
#
# Source LSB function library.
[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions# Source networking configuration.
. /etc/sysconfig/network# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0nicename=”Fetchmail”
pidfile=”/var/run/fetchmail.pid”
args=”-f /etc/fetchmail.conf –syslog”
prog=”fetchmail”
RETVAL=0# Defining arguments.
case “$1? in
start)
echo -n $”Starting $prog: ”
daemon $prog $args
echo
RETVAL=$?
;;
stop)
echo -n $”Stopping $prog: ”
killproc $prog
echo
/bin/rm -f $pidfile
RETVAL=$?
;;
status)
if exist $pidfiles >/dev/null 2>&1?
then
exit 0
else
exit 1
fi;;
restart|reload)
$0 stop
$0 start
RETVAL=$?
;;
*)
echo “Usage: $prog {start|stop|restart|reload|status}”
exit 1
esacexit $RETVAL
Assign executable rights to the init script, and assign 710 permission to fetcmail.conf with the followings command :
chmod 755 /etc/init.d/fetchmail
chmod 710 /etc/fetchmail.conf
Once you’ve done you should start fetchmail with the following command :
/etc/init.d/fetchmail start
And you should find all your remote server mail, trasferred via POP3 on your local accounts.
You should check your mail server logs for troubleshooting.
For debugging purpose you should comment the “set daemon” line on fetchmail configuration file, then stop the daemon with the following command :
/etc/init.d/fetchmail stop
and then execute fetchmail by hand, in this case all the output will be redirected to your session, so you should see if something goes wrong, for doing that use the following command :
fetchmail -f /etc/fetchmail.conf
The init script support the following argument :
To start Fetchmail
service fetchmail start
or
/etc/init.d/fetchmail start
To stop Fetchmail
service fetchmail stop
or
/etc/init.d/fetchmail stop
To restart Fetchmail
service fetchmail restart
or
/etc/init.d/fetchmail restart
To check Fetchmail status
service fetchmail status
or
/etc/init.d/fetchmail status
Once all works fine, you should set Fetchmail daemon to start automatically at system startup using the following :
chkconfig fetchmail on
If you want do disable the automatic fetchmail daemon startup you must run the following :
chkconfig fetchmail off
Otherwise, if you want to run it as a cron scheduled task you must :
Edit the fetchmail main configuration file /etc/fetchmail.conf as following :
set postmaster “postmaster”
set bouncemail
set no spambounce
set properties “”poll yourexternalamailhost1.com with proto POP3 auth password
user ‘ext_username1′ there with password ‘ext_password1′ to ‘internal_mailbox1′ here
user ‘ext_username2′ there with password ‘ext_password2′ to ‘internal_mailbox2′ herepoll yourexternalamailhost12.net with proto POP3 auth password
user ‘ext_username1′ there with password ‘ext_password1′ to ‘internal_mailbox1′ here
user ‘ext_username2′ there with password ‘ext_password2′ to ‘internal_mailbox2′ here
And test your configuration file running the following command as root :
fetchmail -f /etc/fetchmail.conf
If you have no error, you should schedule the fetchmail execution at whichever time interval you want, I usually set it up to 15 minutes.
For doing so you must add the following line to your crontab configuration file :
0-59/15 * * * * /usr/bin/fetchmail -f /etc/fetchmail.conf 2>&1;
Remember that for debuggin purpose you should run manually from command line and all the output will be redirected to your local session so you should check what’s wrong.