Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

Fetchmail Init script

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
SteveHodge
Asked:
SteveHodge
  • 5
  • 4
2 Solutions
 
ahoffmannCommented:
> 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
 
SteveHodgeAuthor Commented:
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
 
ahoffmannCommented:
silly question: is started at boot *after* network initialisation?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SteveHodgeAuthor Commented:
It is one of the last items to be started after boot - index 97  - so yes after network initialisation
0
 
ahoffmannCommented:
no more ideas as long as you don't have a descriptive error message or alike
0
 
SteveHodgeAuthor Commented:
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
 
SteveHodgeAuthor Commented:
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
 
ahoffmannCommented:
> 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
 
antoniogcCommented:
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
 
ahoffmannCommented:
> 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now