How to add a customized daemon

本文介绍如何在RHEL及CentOS上安装与配置Fetchmail服务,实现从远程邮件服务器下载邮件到本地邮件服务器,并提供了一种启动脚本示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 300

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′ here

poll 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 0

nicename=”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
esac

exit $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′ here

poll 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值