Fetchmail自動起動設定

Fetchmail自動起動設定

システム起動時にfetchmail設定ユーザ(ホームディレクトリに.fetchmailrcがあるユーザ)でfetchmailを自動起動できるようにする。

[root@centos ~]# vi /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプト作成※

-----
#!/bin/bash
#
# Fetchmail
#
# chkconfig: - 99 20
# description: Fetchmail auto start script

# Source function library.
. /etc/rc.d/init.d/functions

start() {
    # Start daemons.
    for user in `ls /home/`
    do
        if [ -f /home/$user/.fetchmailrc ]; then
            if [ ! -f /home/$user/.fetchmail.pid ]; then
                echo "fetchmail for $user starting..."
                su $user -s "/bin/bash" -c "/usr/bin/fetchmail"
            else
                PID=`cat /home/$user/.fetchmail.pid|cut -d " " -f 1`
                ps $PID>/dev/null
                if [ $? = 0 ]; then
                    echo "fetchmail for $user is already started..."
                else
                    echo "fetchmail for $user is restartng..."
                    su $user -s "/bin/bash" -c "/usr/bin/fetchmail"
                fi
            fi
        fi
    done

    if [ -f /root/.fetchmailrc ]; then
        if [ ! -f /var/run/fetchmail.pid ]; then
            echo "fetchmail for root starting..."
            /usr/bin/fetchmail
        else
            PID=`cat /var/run/fetchmail.pid|cut -d " " -f 1`
            ps $PID>/dev/null
            if [ $? = 0 ]; then
                echo "fetchmail for root is already started..."
            else
                echo "fetchmail for root is restartng..."
                /usr/bin/fetchmail
            fi
        fi
    fi
}

stop() {
    # Stop daemons.
    if [ -f /var/run/fetchmail.pid ]; then
        echo "fetchmail for root stoping..."
        /usr/bin/fetchmail --quit
    fi

    for user in `ls /home/`
    do
        if [ -f /home/$user/.fetchmail.pid ]; then
            echo "fetchmail for $user stoping..."
            su $user -s "/bin/bash" -c "/usr/bin/fetchmail --quit"
        fi
    done
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        run="0"

        if [ -f /var/run/fetchmail.pid ]; then
            PID=`cat /var/run/fetchmail.pid|cut -d " " -f 1`
            ps $PID>/dev/null
            if [ $? = 0 ]; then
                run="1"
                echo "fetchmail for root is running..."
            fi
        fi

        for user in `ls /home/`
        do
            if [ -f /home/$user/.fetchmail.pid ]; then
                PID=`cat /home/$user/.fetchmail.pid|cut -d " " -f 1`
                ps $PID>/dev/null
                if [ $? = 0 ]; then
                    run="1"
                    echo "fetchmail for $user is running..."
                fi
            fi
        done

        if [ $run == "0" ]; then
            echo "fetchmail is not running"
            exit 1
        fi
        ;;
    *)
        echo "Usage: fetchmail {start|stop|restart|status}"
        exit 1
esac

exit $?

-----

[root@centos ~]# chmod +x /etc/rc.d/init.d/fetchmail ← Fetchmail起動スクリプトへ実行権限付加
[root@centos ~]# /etc/rc.d/init.d/fetchmail start ← Fetchmail起動
fetchmail for centos is already started...
[root@centos ~]# chkconfig --add fetchmail ← Fetchmail起動スクリプトをchkconfigへ追加
[root@centos ~]# chkconfig fetchmail on ← Fetchmail自動起動設定
[root@centos ~]# chkconfig --list fetchmail ← Fetchmail自動起動設定確認
fetchmail       0:off   1:off   2:on    3:on    4:on    5:on    6:off