[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Fwd: script to start fetchmail daemon for users on startup



----- Forwarded message from Clive Menzies <clive@clivemenzies.co.uk> -----

> To: debian-user@lists.debian.org
> From: Clive Menzies <clive@clivemenzies.co.uk>
> Date: Fri, 2 Sep 2005 16:18:47 +0100
> Subject: script to start fetchmail daemon for users on startup
> 
> Hi
> 
> Googling around for ways to start the fetchmail daemon on behalf of the
> various users, I came across what looks like a good script on the
> following link:
> http://forums.fedoraforum.org/showthread.php?t=30838
> 
> The script, written by Vince Parsons, is for Fedora although a debian
> version is mentioned; there do not however, appear to be any links to a
> debianised version.
> 
> I've emailed Vince but his email (yahoo) address appears to have expired
> and the message was returned.  His site is https://www.vparsons.net/
> but hasn't been updated since March 2005.
> 
> The script is attached.
> 
> From my limited understanding of these things, I assume that I need to
> amend the file and command locations to reflect the debian file structure.
> 
> I'd appreciate some guidance.
> 
> Regards
> 
> Clive

----- End forwarded message -----

-- 
www.clivemenzies.co.uk ...
...strategies for business


#!/bin/bash
## BEGIN chkconfig header
# chkconfig: 345 80 30
# processname:	/usr/bin/fetchmail
# description:	sysv init script to manage user fetchmail processes
## END chkconfig header
##
## AUTHOR:
##		Vince Parsons 
## Description:
##		fetchmail-init is a sysv init script that manages fetchmail for local users \
##		by searching through users home directories for .fetchmailrc and \
##		starting daemons on the users behalf. fetchmail-init should work \
##		on any platform based on Red Hat Linux 7 and above.
## LICENSE:
##		GNU GPL 
## COPYRIGHT:
##		none this work is in the public domain.
## VERSION INFO:
##		1.0.12 (2005.02.19) - Development
## REQUIREMENTS:
##		bash, fetchmail
## INSTALLATION:
##		Make this script executable and place it somewhere in your path.
## USAGE:
##		fetchmail {start|stop|restart|cleanup|status}

## Only the superuser can run this script.
[ ${UID} -eq 0 ] || exit 1

## Source function library and network configuration.
[ -r /etc/init.d/functions ] && . /etc/init.d/functions
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

## Set defaults.
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PROGRAM="/usr/bin/fetchmail"
export OPTIONS="-d 300 --nobounce"
prog="fetchmail"
LOCKFILE="/var/lock/subsys/fetchmail"
PIDFILE="/var/run/fetchmail.pid"
USERS=`ls -1 /home | grep -v lost+found`
RETVAL=0
ERRNO=0
ERRNO1=0
ERRNO2=0
ERRNO3=0
ERRNO4=0
ErrorControl="yes"	# Extended error control on startup.
## Use /etc/sysconfig/fetchmail to overide any of the defaults set above.
[ -r /etc/sysconfig/fetchmail ] && . /etc/sysconfig/fetchmail

function depend() {
    ## Make sure everything is happy before we continue.
    [ ${NETWORKING} = "no" ] && exit 2
    [ -x ${PROGRAM} ] || exit 3
    [ -x /bin/su ] || exit 4
    [ -x /sbin/pidof ] || exit 5
    ## You can disable all of the extended error checking if you wish.
    ## Set variable ErrorControl to no. Good luck if something breaks...
    if [ ${ErrorControl} = "yes" ]; then
        ## For the sake of our sanity, lets do some error checking, make sure all is well.
        ## FETCHMAILRC is set here to speed up the script, we only need it for start/restart.
        FETCHMAILRC=`find /home -name ".fetchmailrc"`
        ## See if ~/.fetchmailrc exists.
        [[ ${FETCHMAILRC} = "" ]] && ERRNO=9
        ## See if /var/lock/subsys/fetchmail.* exists.
        [ -e ${LOCKFILE}.* ] && ERRNO1=10
        ## See if /var/run/fetchmail.pid exists.
        [ -e ${PIDFILE} ] && ERRNO2=11
        ## See if there are any fetchmail daemons running.
        [ -n "`pidof ${PROGRAM}`" ] && ERRNO3=12
        ## BEGIN ERROR CHECKING
        ERRNO4=`expr ${ERRNO1} + ${ERRNO2} + ${ERRNO3}`
        if [ ${ERRNO} -eq 9 ]; then
            echo -n $"User .${prog}rc not found:"
            failure && echo && exit ${ERRNO}
        elif [ ${ERRNO4} -eq 10 ]; then
            echo -n $"${prog} subsys locked:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 11 ]; then
            echo -n $"${prog} PID not cleared:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 12 ]; then
            echo -n $"${prog} daemons are running:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 21 ]; then
            echo -n $"${prog} subsys locked PID not cleared:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 22 ]; then
            echo -n $"${prog} subsys locked daemons are running:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 23 ]; then
            echo -n $"${prog} PID not cleared daemons are running:"
            warning && echo && exit ${ERRNO4}
        elif [ ${ERRNO4} -eq 33 ]; then
            echo -n $"${prog} is already running:"
            failure && echo && exit ${ERRNO4}
        fi
    fi
}

function start() {
    ## Start the daemons.
    depend;
    for u in ${USERS}; do
        [ -f "/home/$u/.fetchmailrc" ] && {
            ## ensure permissions are correct for ~/.fetchmailrc
            chmod 0600 /home/$u/.fetchmailrc 2>/dev/null
            echo -n $"Starting ${prog} for user $u:"
            su $u -c '${PROGRAM} ${OPTIONS}' 2>/dev/null 1>&2 && success || failure
            RETVAL=$?
            echo
        }
    done
    ## Set PID & Lock files.
    [ ${RETVAL} -eq 0 ] && {
        pidof ${prog} > ${PIDFILE} 2>/dev/null
        touch "${LOCKFILE}.$u"
    }
    return ${RETVAL}
}

function stop() {
    ## Stop the daemons.
    [ -n "`pidof ${PROGRAM}`" ] && {
        for u in ${USERS}; do
            [ -e "/home/$u/.fetchmail.pid" ] && {
                echo -n $"Stopping ${prog} for user $u:"
                su $u -c '${PROGRAM} -q' 2>/dev/null 1>&2 && success || failure
                RETVAL=$?
                echo
            }
        done
        ## Cleanup PID & Lock files.
        [ ${RETVAL} -eq 0 ] && {
            rm -f ${PIDFILE} 2>/dev/null
            rm -f "${LOCKFILE}.$u" 2>/dev/null
            find /home -name ".fetchmail.pid" -exec rm -f {} 2>/dev/null \;
            RETVAL=0
        }
    }
    return ${RETVAL}
}

function restart() {
    ## Stop the daemons.
    stop;
    sleep 1
    ## Start fetchmail.
    echo
    start;
}

## If for some reason fetchmail does not go down gracefully, you \
## can run kill to kill the daemons and cleanup the lock and pid files.
## ONLY USE CLEANUP IF STOP FAILS TO FUNCTION PROPERLY.
function cleanup() {
    ## Kill the daemons and cleanup.
    [ -n "`pidof ${prog}`" ] && killall -9 ${PROGRAM} >/dev/null 2>&1
    rm -f ${PIDFILE} 2>/dev/null
    rm -f ${LOCKFILE}.* 2>/dev/null
    find /home -name ".fetchmail.pid" -exec rm -f {} 2>/dev/null \;
    RETVAL=0
    return ${RETVAL}
}

## pid is used with status to show which PIDs belong to which users.
function pid() {
    ## Find individual PIDs for users daemon processes.
    for u in ${USERS}; do
        [ -f /home/$u/.fetchmail.pid ] && {
            echo $"          (pid `cat /home/$u/.fetchmail.pid | awk '{print $1}'`) is assigned to user $u"
        }
    done
    RETVAL=0
}

## See how we are called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload)
        restart
        ;;
    cleanup|kill)
        cleanup
        ;;
    status)
        status ${prog}
        pid
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|cleanup|status}"
        RETVAL=1
esac

unset PROGRAM OPTIONS

exit ${RETVAL}
## EOF

Reply to: