Solved

Fetchmail Init script

Posted on 2008-09-30
10
830 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

New-MailboxSearch Powershell Command and step by step approach to Search and Extract Emails form Exchange 2013 Journaling server.
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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