fetchmail-init script
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 12 of 12
  1. #1
    knurd Guest

    fetchmail-init script

    Greetings one and all!

    This script has been broken into several files.
    The best way to install it is with the rpm. Check the links below.

    I wrote an init script to manage fetchmail on a multi user mail server.
    It's useful for me due to the fact that I have many users that have multiple email accounts across the internet. And they only want to have one account setup in evolution.
    I use fetchmail in conjunction with hotwayd to pull email from hotmail accounts.

    I'm in the process of rebuilding my site, but you can also get this script from
    fetchmail-init home

    Corrections were made to the chkconfig line, thanks to tberer for catching that.

    If you find the following script useful, please let me know.

    be sure to have a working ~/.fetchmailrc

    you can also get the rpm here:
    http://bink.homeip.net/projects/fetchmail/index.shtml

    Setup: rpm -ivh http://www.vparsons.org/data/fetchma...1-0.noarch.rpm
    rpm may complain about the gpg key, you can get that here:
    http://www.vparsons.org/projects/RPM...Y.vparsons.txt

    OLD - Copy the text into /etc/init.d/fetchmail
    Make it executable.
    Code:
    chmod 755 /etc/init.d/fetchmail
    chkconfig --add fetchmail
    chkconfig --level 345 fetchmail on
    service fetchmail start

    Script Text below...
    Code:
    THIS SCRIPT IS OLD, PLEASE INSTALL THE RPM FOR THE LATEST...
    http://www.vparsons.org/data/fetchmail-init-1.1-0.noarch.rpm
    #!/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
    cheers
    Last edited by knurd; 14th April 2006 at 12:33 AM.

  2. #2
    knurd Guest
    I've updated this script with some fixes. Should work without any problems.

  3. #3
    jult Guest
    Thanks for the script, but:

    # chkconfig --add fetchmail
    service fetchmail does not support chkconfig

    # chkconfig --level 345 fetchmail on
    service fetchmail does not support chkconfig

    How do you use it by the way?
    Do you run it from a cron-job every now and then?
    You are aware of the daemon <interval> option of fetchmail?
    Last edited by jult; 9th January 2005 at 07:18 PM.

  4. #4
    Join Date
    Mar 2004
    Location
    germany
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you create the file /etc/init.d/fetchmail?
    chkconfig should recognise fetchmail as service then

    if you check the script there is a line
    export OPTIONS="-d 300 --syslog --nobounce -K"
    so it will run as daemon and poll your servers every 5 minutes

  5. #5
    knurd Guest
    jult: be sure to create /etc/init.d/fetchmail then copy the script text into it.
    Then you should be able to use chkconfig and service tools on it.

  6. #6
    tberer Guest
    Thank you for the script knurd.

    But i have some problems with it under FC2. I can not set the runlevels with chkconfig or ntsysv.

    Code:
    # chkconfig --list fetchmail
    fetchmail       0:Aus   1:Aus   2:Aus   3:Aus   4:Aus   5:Aus   6:Aus
    # chkconfig fetchmail on
    # chkconfig --list fetchmail
    fetchmail       0:Aus   1:Aus   2:Aus   3:Aus   4:Aus   5:Aus   6:Aus
    # chkconfig --level 345 fetchmail on
    # chkconfig --list fetchmail
    fetchmail       0:Aus   1:Aus   2:Aus   3:Aus   4:Aus   5:Aus   6:Aus
    Aus means off.

    I tried chkconfig --del fetchmail and --add.

    Currently i start it with "service fetchmail start" in /etc/rc.local. This works.

  7. #7
    Join Date
    Feb 2004
    Location
    Seattle, WA, USA
    Age
    63
    Posts
    3,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to copy the entire script, including the header, for chkconfig to work. It's the comments in the header that make it work.
    Linux User #28251 (April '93)
    Professional Java Geek :cool:

  8. #8
    tberer Guest
    Reading the chkconfig man pages i found it.

    The line
    Code:
    # chkconfig: - 345 80 30
    Should contain 3 parameters. The default runlevels or a '-' and the start- and stop priorities.

    Code:
    # chkconfig: 345 80 30
    Works now.

  9. #9
    knurd Guest
    Thanks for catching that tberer, i've added your correction to the script.

  10. #10
    bayer Guest
    as far as i can see, there should also be a "description" in the chkconfig header part

  11. #11
    blainehilton Guest
    I tried the script located here and could not get that to work (asked for help here) so I thought I would try this one. I was able to get it started okay, however I need to poll every 60 seconds, instead of every 300 seconds as is in this init script. In the .fetchmailrc file I have it as 60 seconds, but it was not working correctly when ran using this script. So I changed the 300 to 60 and it still did not work.

    Also I keep getting messages saying "fetchmail subsy locked: [WARNING]".

    If I do a "service fetchmail cleanup" it works. But even when I close it using "service fetchmail stop" and then start again I get the same error.

  12. #12
    Join Date
    Jul 2006
    Location
    Sao Paulo, SP - Brazil
    Age
    40
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello:

    In the /contrib folder of source package is this script named redhat_rc:

    Code:
    #!/bin/sh
    #
    # fetchmail	This shell script takes care of starting and stopping
    #		fetchmail.
    #
    # chkconfig: 2345 81 45
    # description: The Fetchmail daemons allows to retrieve mail using various
    #	       mail protocols and route them to the local MTA just as if
    #	       the mail was sent directly to the local MTA. This is
    #	       specially useful on intermittent dial-up connections.
    # processname: fetchmail
    # config: /etc/fetchmailrc
    # author[s]:
    #	Andrea Sterbini	<a.sterbini@itelcad.it>
    #	ObiTuarY <obituary@freshmeat.net>
    
    . /etc/rc.d/init.d/functions
    
    # Source networking configuration.
    . /etc/sysconfig/network
    
    # Check that networking is up.
    if [ ${NETWORKING} = "no" ]
    then
    	exit 0
    fi 
        
    # See how we were called.
    case "$1" in
      start)
    	if [ -s /etc/fetchmailrc ]; then
    		echo -n "Loading fetchmail: "
    		daemon /usr/bin/fetchmail -f /etc/fetchmailrc
    		echo
    		touch /var/lock/subsys/fetchmail
    	else
    		exit 1
    	fi
    	;;
      stop)	
    	echo -n "Shutting down fetchmail: "
    	/usr/bin/fetchmail -q >/dev/null 2>&1 && echo fetchmail
    #	killproc fetchmail
    	rm -f /var/lock/subsys/fetchmail
    	;;
      status)
            status fetchmail
            ;;
      restart|reload)
            $0 stop
            $0 start
            ;; 
    	*)
    	echo "Usage: fetchmail {start|stop|status|restart|reload}"
    	exit 1
    esac
    
    exit 0
    
    # === End of File ===
    duli

Similar Threads

  1. Fetchmail init script
    By blainehilton in forum Servers & Networking
    Replies: 3
    Last Post: 30th June 2006, 04:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •