LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 05-17-2012, 01:31 PM   #1
Old_Fogie
Senior Member
 
Registered: Mar 2006
Distribution: SLACKWARE 4TW! =D
Posts: 1,519

Rep: Reputation: 63
Request for peer review of my rc.fetchmail daemon script please


Hello all,

I've been working on a set of build scripts for courier imap, in hopes that I can get it usable enough to post to the mailing list for further work and review.

So because I need to have a lot of mail generated, and fetched for testing purposes, I found that I need fetchmail running as a daemon for my testing.

Since Slackware does not come with a daemon for fetchmail, and yes I know that cron can do it, but that's another issue, I set out to write one for my testing purposes.

If I can get this to be reviewed by others I can put it on Slackwiki or perhaps send it to Mr. V for it to be added (if he wants) to /usr/doc/fetchmail/contrib or the like.

Any how, here's what I've got so far, and it does indeed work.

The only issue, is that when you do something such as "ps aux |grep fetchmail" it will only show "204" instead of the user 'fetchmail' due to posix requirements of a username less that 8 characters.

How to set up user and group info is at the end.

Please scrutinize it, rip it apart, yet at me, it's all good

Ok here's the code:

Code:
#!/bin/sh

INTERVAL=30
CONFIG=/etc/fetchmailrc

case "$1" in
  start)
if [ -e /var/run/fetchmail/.fetchmail.pid ]; then
  echo
  echo "The fetchmail daemon appears to be running by the presence \
of /var/run/fetchmail/.fetchmail.pid."
  echo
  echo "To resolve this, try: fetchmail -quit or simply rm \
/var/run/fetchmail/.fetchmail.pid"
  echo
  echo "Start command failed: exiting now"
  echo
exit
fi
    echo "Starting fetchmail with uid/gid pair of `id fetchmail`"
      # add the -v after CONFIG to get verbose output
      # add the --invisible option to stop fetchmail adding itself to the headers
      su - fetchmail -s /bin/bash -c "/usr/bin/fetchmail -L /var/log/fetchmail -d \
        $INTERVAL -f $CONFIG -v --invisible"
      echo "fetchmail: starting at `date`" >> /var/log/fetchmail
    ;;
  stop)
    echo "Stopping fetchmail"
      su - fetchmail -s /bin/bash -c "/usr/bin/fetchmail -quit"
      echo "fetchmail: stopped at `date`" >> /var/log/fetchmail
      echo "" >> /var/log/fetchmail
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0


# slackware commands to add fetchmail user and group
#groupadd -g 204 fetchmail
#useradd -u 204 -d /var/run/fetchmail -s /bin/false -g fetchmail fetchmail

# create fetchmails home:
#install -d -o fetchmail -g fetchmail -m700 /var/run/fetchmail

# create our config file template
#touch /etc/fetchamilrc <------------- if you don't have one already there
#chmod 600 /etc/fetchamilrc
#chown fetchmail:fetchmail /etc/fetchmailrc <------------if you dont have one there already

# crate the log file for fetchmail to use
#touch /var/log/fetchmail
#chown fetchmail:fetchmail /var/log/fetchmail
#chmod 600 /var/log/fetchmail
 
Old 05-18-2012, 11:14 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Here's my take on things keeping it simple:
Code:
#!/bin/sh

NAME="fetchmail"
CONFIG=/etc/${NAME}rc
BINARY="/usr/bin/${NAME}"
PIDDIR="/var/run/${NAME}"
LOGFILE="/var/log/${NAME}.log"
INTERVAL=30
umask 027

preflight() { # Add user and group, configuration and log file
getent group ${NAME} >/dev/null 2>&1; 
[ $? -ne 0 ] && { groupadd -g 204 ${NAME}; useradd -u 204 -d ${PIDDIR} -s /bin/false -g ${NAME} ${NAME};
install -d -o ${NAME} -g ${NAME} -m 0700 ${PIDDIR}; }
[ ! -e ${CONFIG} ] && { touch ${CONFIG}; chmod 600 ${CONFIG}; chown ${NAME}:${NAME} ${CONFIG}; }
[ ! -e /var/log/${NAME} ] && { touch ${${LOGFILE}}; chmod 600 ${${LOGFILE}}; chown ${NAME}:${NAME} ${${LOGFILE}}; }
}

isStopped() { # Stop Fetchmail if necessary
pgrep -f "${BINARY} -L ${${LOGFILE}}" >/dev/null 2>&1
case $? in 0) $0 stop;;	*) rm -f "${PIDDIR}/.${NAME}.pid";; esac
}

case "$1" in
 start)  preflight; isStopped
  echo "Starting ${NAME} as `id ${NAME}`"
  su - ${NAME} -s /bin/bash -c "${BINARY} -L ${${LOGFILE}} -d $INTERVAL -f ${CONFIG}"
  logger -f "${${LOGFILE}}" -t "${NAME}" "Starting."
 ;;
 stop)
  echo "Stopping ${NAME}"
  su - ${NAME} -s /bin/bash -c "${BINARY} -quit"
  logger -f "${${LOGFILE}}" -t "${NAME}" "Stopped."
  isStopped
 ;;
 restart)
  $0 stop; sleep 1; $0 start
 ;;
 *)
  echo "usage: $0 {start|stop|restart}"
  exit 0
 ;;
esac

exit 0
 
Old 05-20-2012, 08:14 PM   #3
Old_Fogie
Senior Member
 
Registered: Mar 2006
Distribution: SLACKWARE 4TW! =D
Posts: 1,519

Original Poster
Rep: Reputation: 63
Thank you verfy much for the assistnace unSpawn.

There are a few commands in that script you posted that I am not familiar with. So I've been reading up on them.

A question for you please, what does the "*" do in the following section:

Code:
isStopped() { # Stop Fetchmail if necessary
pgrep -f "${BINARY} -L ${${LOGFILE}}" >/dev/null 2>&1
case $? in 0) $0 stop;;    *) rm -f "${PIDDIR}/.${NAME}.pid";; esac
}

Thank you again :)
 
Old 05-21-2012, 01:20 AM   #4
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,858

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by Old_Fogie View Post
A question for you please, what does the "*" do in the following section:

Code:
isStopped() { # Stop Fetchmail if necessary
pgrep -f "${BINARY} -L ${${LOGFILE}}" >/dev/null 2>&1
case $? in 0) $0 stop;;    *) rm -f "${PIDDIR}/.${NAME}.pid";; esac
}
Well, if you write the case statement like this...
Code:
case $? in
    0)
        $0 stop
        ;;
    *)
        rm -f "${PIDDIR}/.${NAME}.pid"
        ;;
esac
...it might be more clear. The case statement attempts to match the individual case patterns in order. The "*" pattern matches anything, so it is more-or-less used as a default.

You can run the "man bash" command and search for the case command's documentation, which tells you other interesting details too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Peer Review Starts for Software Patent Applications weibullguy General 0 06-23-2007 08:01 AM
fetchmail daemon ChrisScott Linux - Software 6 12-25-2006 11:29 PM
fetchmail and pop3 daemon? scuffell Linux - Networking 1 08-29-2004 04:30 PM
Fetchmail - daemon mode - manfrake 9.2 VincentB Linux - Newbie 1 02-11-2004 01:13 PM
Running fetchmail daemon automatically iihay Linux - General 3 09-02-2001 05:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration