Solved

Fetchmail Init script

Posted on 2008-09-30
10
825 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux  Script File Attributes 5 53
CentOS 7 Installation 7 59
Backup & Restore 3 46
Choosing CentOS 16 57
Nearly six years ago I was hired by a company to be their senior server engineer. One of my first projects was to implement Exchange Server 2007 on a Windows Server 2008 Single Copy Cluster for high availability. That was the easy part; read on to l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 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

14 Experts available now in Live!

Get 1:1 Help Now