Init script without Start-Stop-Daemon

Greg T Hill greghill at terranova.net
Fri Jul 14 13:47:56 PDT 2000


There was some discussion about doing away with the start-stop-daemon program
last month, so I thought I would post this script.
I did this primarily as an excersise for learning shell scripting, and wrote
this using 'pidof'  with some sanity checks, instead of start-stop-daemon.  The
only warranty is that it works fine on my system for samba, inetd, and nfs,
all of which require  starting 2 daemons.   Its about 3 times longer than a
comparable script using ssd, and probably slower, but not that I can notice.
Also bear in mind that Miquel van Smoorenburg (sysvinit's author) recommends
the ssd over scripts using pidof.   I will probably redo my scripts again using
ssd once I really test it to find out what the options actually do and/or don't
do.  The documentation is kind of sparse and leaves some things open to
interpretation.  Or if someone comes up with an alternative method I would like
to test that out also.
#!/bin/sh
# Begin /etc/rc.d/init.d/inet
# skeleton for init scripts using pidof instead of start-stop-daemon
# LOOSELY based on Miquel van Smoorenburg's script
# Hacked and comments added by Greg T Hill <greghill at terranova.net>
#
# This script calls two daemons, used for where daemons are started together,
# i.e Samba: smbd & nmbd, NFS: nfs & mountd, Inetd: inetd & portmap
#
# Read functions into this script so they can be called
# functions are evaluate_retval and status
source /etc/rc.d/init.d/functions # "source" : synonym for "."
#
# Set path as a precaution against it not having been already exported
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#
#----------------------*-- Section to edit--*------------------#
DESC="Network Services "  #example "Samba daemons"
NAME1=inetd
NAME2=rpc.portmap
PID_DIR=/var/run/   # examples: /var/run/, /var/lock/
OPT="" # Any command line options, example: Samba uses " -D" (note space after
1st quote)
INIT_DIR=/etc/rc.d/init.d/ #or /etc/init.d or your init.d location
DAEMON_DIR=/usr/sbin/  #Not always
CONFIGFILE=/etc/inetd.conf # Absolute path to config file, if any
# i.e /etc/inetd.conf, /etc/samba.d/smb.conf
# If none reqired, leave "" to return zero length string
#--------------------*---------*-----------*------------------#
#
SCRIPT=$(basename $0)  #returns name of script, removes "../"
N=$INIT_DIR$SCRIPT  #Prompts using absolute path to script
#
# Causes exit on errors, comment out for debugging
#set -e
#
# check if the daemons are installed, bail out if not
if [ ! -e "$DAEMON_DIR$NAME1" ] || [ ! -e "$DAEMON_DIR$NAME2" ]
then
   echo "Cannot find one or more daemons for $DESC"
   echo "Check to see if properly installed"
   exit 0
fi
# If config file required, check that it exists
if [ -n "$CONFIGFILE" ] && [ ! -e "$CONFIGFILE" ] ; then
      echo "$CONFIGFILE not found, $DESC not configured!"
      exit 0
fi
#
case "$1" in   # "$1" is first argument in calling command
  start)
    for DAEMON in $NAME1 $NAME2
    do
        # Check if smbd daemon is already running * a simple 'pidof <filename>
would probably suffice,
        # -o $$ omits pid of current shell, -o %PPID  omits pid of shell or
script calling pidof,
        # -x includes pid's of scripts running daemon
        PID=$(pidof -x -o $$ -o %PPID  $DAEMON_DIR$DAEMON)
        if [ -n "$PID" ] ; then  # if $PID is not a zero length string
           echo -n "$DAEMON already running! "
           /bin/false # do nothing, unsuccesfully, so that
           evaluate_retval  # can print red [ FAILED ]
           echo  #generate newline so messages don't overlap
        else
           echo -n "Starting $DESC:...$DAEMON"
           #Check if there is a pidfile, remove it if it exist
           if [ -e "$PID_DIR$DAEMON.pid" ] ; then
              rm $PID_DIR$DAEMON.pid
           fi
           $DAEMON_DIR$DAEMON$OPT #Start daemon
           evaluate_retval  #print green "OK" or red "FAILED"
        fi
    done
    ;;
  stop)
    for DAEMON in $NAME1 $NAME2
    do
       #Check if daemon is running
        PID=$(pidof -x -o $$ -o %PPID $DAEMON_DIR$DAEMON)
        if [ -n "$PID" ] ; then
          echo -n "Stopping $DESC:...$DAEMON "
          kill $PID
          evaluate_retval #print green "OK" or red "FAILED"
          # Check for pid file, remove if it exists
          if [ -e "$PID_DIR$DAEMON.pid" ] ; then
              rm $PID_DIR$DAEMON.pid
          fi
        else
          echo -n "$DAEMON not running, no daemon to kill..."
          /bin/false  #do nothing, unsuccessfully: return an error so that
          evaluate_retval #can print red [ FAILED ]
          echo  #generate newline so messages don't overlap
        fi
    done
    ;;
  reload)       #If the daemon can reload its config files on the fly
        #       for example by sending it SIGHUP, do it here.
        #   Example: inetd will reread inetd.conf
        #       If the daemon responds to changes in its config file
        #       directly anyway, make this a do-nothing entry.
     for DAEMON in $NAME1 #$NAME2
     do
       #Check if daemon is running
        PID=$(pidof -x -o $$ -o %PPID $DAEMON_DIR$DAEMON)
        if [ -n "$PID" ] ; then
            echo -n "Reloading $DESC configuration files...$DAEMON"
            kill -HUP $PID
            evaluate_retval  #print green "OK" or red "FAILED"
            echo  #generate newline so messages don't overlap
        else
          echo "Cannot reload, $DAEMON not running..."
          /bin/false  #do nothing, unsuccessfully: return an error so that
          evaluate_retval #can print red [ FAILED ]
          echo  #generate newline so messages don't overlap
        fi
     done
     ;;
  restart)
     echo "Restarting $DESC: "
     for DAEMON in $NAME1 $NAME2
     do
       #Check if daemon is running
        PID=$(pidof -x -o $$  -o %PPID DAEMON_DIR$DAEMON)
        if [ -n "$PID" ] ; then
          echo -n "Stopping $DAEMON. "
          kill $PID
          evaluate_retval #print green "OK" or red "FAILED"
          rm $PID_DIR$DAEMON.pid  #remove pidfile
          sleep 1 #pause for a second
          echo -n " $DAEMON. "
          $DAEMON_DIR$DAEMON$OPT #Restart daemon
          evalute_retval  # print green "OK" or red "FAILED"
        else
          echo -n "$DAEMON not running, try $N start ..."
          /bin/false # do nothing, unsuccesfully, so that
          evaluate_retval  # can print red [ FAILED ]
          echo  #generate newline so messages don't overlap
       fi
    done
    ;;
  status)  #Do nothing, call status function to print daemon info
     status $NAME1
     status $NAME2
     ;;
  *)  # If all the above fails, script probably was called wrong
      # echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
      echo "Usage: $N {start|stop|restart|status}" >&2
      # exit 1
      ;;
esac
exit 0
# End /etc/rc./init.d/inet

--
Mail archive: http://www.pcrdallas.com/mail-archives/lfs-discuss
IRC access: server: irc.linuxfromscratch.org port: 6667 channel: #LFS
Unsubscribe: email lfs-discuss-request at linuxfromscratch.org and put
"unsubscribe" (without the quotation marks) in the body of the message
(no subject is required)



More information about the lfs-dev mailing list