Solved

Fetchmail Init script

Posted on 2008-09-30
10
823 Views
Last Modified: 2013-12-06
I am running Debian Linux V3
I have an init script to start a fetchmail daemon, running as root

It starts and stops fetchmail perfectly OK if I run it 'standalone' via the program invoke-rc.d, but will not start fetchmail on a  boot. And I can start the fetchmail daemon just by calling fetchmail from the command line - in both cases running as su. And fetchmail appears in all the right rc*.d directories

I think the boot up problem is something to do with users and passwords, as when it fails the user log shows a password error, but for the life of me I cannot see what. Can anyone help??

the init script is below - it isn't very pretty I have been tinkering with it
Quote:
#!/bin/bash
#
# Fetchmail init script
# $Id: init 157 2004-09-14 03:26:54Z bob $
#
# A fetchmailrc file containg hosts and passwords for all local users should be
# placed in /etc/fetchmailrc.  Remember to make the /etc/fetchmailrc mode 600
# to avoid disclosing the users' passwords.
#
set -e
#
# Defaults
DAEMON=/usr/bin/fetchmail
CONFFILE=/root/.fetchmailrc
#OPTIONS="-f $CONFFILE"
PIDFILE=/var/run/fetchmail.pid
USER=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin
#
#test -f $DAEMON || exit 0
#test -r $CONFFILE || exit 0
#
#
case "$1" in
        start)
                echo -n "Starting mail retrieval agent: fetchmail"
                start-stop-daemon -S -v -u root --exec /usr/bin/fetchmail
                ;;

        stop)
                echo -n "Stopping mail retrieval agent: fetchmail"
                start-stop-daemon -K -o -v -q -p $PIDFILE  --user $USER
                ;;

        restart)
                echo -n "Re-starting mail retrieval agent: fetchmail"
                start-stop-daemon -S --exec /usr/bin/fetchmail
                start-stop-daemon -K -o -q -p $PIDFILE ---exec $DAEMON --user $U
SER
                ;;
        test)
                echo -n "test test"
                ;;
        *)
                echo "Usage: /etc/init.d/fetchmail {start|stop|restart}"
                echo "  start - starts system-wide fetchmail service"
                echo "  stop  - stops system-wide fetchmail service"
                echo "  restart - starts a new system-wide fetchmail service"
                exit 1
                ;;
esac
#
exit 0
0
Comment
Question by:SteveHodge
  • 5
  • 4
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22612857
> USER=root
USER is a common well known shell environment variable and should not be overwritten
As we see in your script it it used to hold a special username, not       essentially that of the current (shell) user. So I'd rename that variable, for example MYUSER.
Not sure if this causes your problem, but worth a try.
0
 

Author Comment

by:SteveHodge
ID: 22613885
Thanks, tried it but it made no difference - the script still doesn't work on boot but does with invoke-rc.d (run as root)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22616929
silly question: is started at boot *after* network initialisation?
0
 

Author Comment

by:SteveHodge
ID: 22619497
It is one of the last items to be started after boot - index 97  - so yes after network initialisation
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22619684
no more ideas as long as you don't have a descriptive error message or alike
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 

Author Comment

by:SteveHodge
ID: 22663568
I have simplified the script, as below; It definitely runs on boot, as I made it print the environment immediately after starting - also below. It is clearly something to do with fetchmail's peculiarities, and I still suspect to do with which user is running fetchmail

Environment:
CONSOLE=/dev/console
TERM=linux
INIT_VERSION=sysvinit-2.86
PATH=/bin:/usr/bin:/sbin:/usr/sbin
RUNLEVEL=2
runlevel=2
PWD=/
PREVLEVEL=N
previous=N
HOME=/
SHLVL=2
_=/usr/bin/env

Revised script
#!/bin/sh
# Fetchmail start script

case "$1" in
'start')
        /usr/bin/fetchmail -d 600
        echo "fetchmail started"
        env > /tmp/test.log
        ;;
'stop')
        /usr/bin/fetchmail --quit
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
0
 

Author Comment

by:SteveHodge
ID: 22663793
Cracked it! fetchmail needs to find its configuration file in the home directory of the calling user. So far as init is concerned the home directory of root is /. (saw this from the env print out above - once started root's home directory seems to become /root - obviously something I don't do right here). Once I put a copy of the config file in / it started OK on init
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22663977
> HOME=/
grrr, the same as with
    USER=root

never ever, in no circumstances, overwrite system variables, except you're 101% sure what happens
Always use your own variables, if you need to access the real home directory use ~ (in most shells) as it is aware of stupid overwritings of $HOME ;-)

Just my 2 pence ...
0
 
LVL 2

Accepted Solution

by:
antoniogc earned 250 total points
ID: 22691564
When linux boots up the root's $HOME variable point to / so, fetchmail trait to get the the config file at /.fetchmailrc not at /root/.fetchmailrc

To resolv this, at the RC script add the -f flag and the path to the config file


#!/bin/sh

# Fetchmail start script
 

case "$1" in

'start')

        /usr/bin/fetchmail -d 600 -f /root/.fetchmailrc

        echo "fetchmail started"

        env > /tmp/test.log

        ;;

'stop')

        /usr/bin/fetchmail --quit

        ;;

*)

        echo "Usage: $0 { start | stop }"

        ;;

esac

exit 0

Open in new window

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 250 total points
ID: 22691838
> When linux boots up the root's $HOME variable point to /
hmm, I'd double check this. You never know what smart admins try in /etc/.profile ...
better you use ~ as it is always the login directory as specified in /etc/passwd.

Anyway, giving fetchmail a full path for its rc-file is not that bad too;-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now