[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: init script config files



Christopher W. Curtis wrote:
> My idea is to have a script, '/etc/init.d/defaults', which every init
> script sources.  'defaults' will read the default settings and define
> common functions for the scripts to use.  Here is a rough draft of the
> idea:

Are you familiar with start-stop-daemon? Because you've just
re-implemented it.

> /etc/init.d/defaults:
> ----------------------------------------
> PACKAGE=$1
> shift
> 
> ECHO=echo
> 
> # Ensure proper usage
> if [ "X$PACKAGE" = "X" ] ; then
>         echo "Invalid use of configuration file"
>         return 100
> fi
> 
> # Some settings must be present
> if [ ! -r /etc/init.d/defaults.d/$PACKAGE ] ; then
>         $ECHO "Package configuration not found."
>         return 101
> fi
> 
> # Read the settings
> . /etc/init.d/defaults.d/$PACKAGE $@
> 
> # And any user-defined overrides
> if [ -r /etc/init.d/defaults.d/${PACKAGE}.user ] ; then
>         . /etc/init.d/defaults.d/${PACKAGE}.user $@
> fi

Everything from here on down is already done quite nicely by
start-stop-daemon, thank you anyway.

> # This is where execution ends; functions follow
> # start the program
> start()
> {       DAEMON=$1
>         shift
> 
>         # Make sure we can execute the program
>         if [ ! -x "$DAEMON" ] ; then
>                 $ECHO "Daemon removed?"
>                 return -1        # removed, not purged
>                 # returning error because the init script is
>                 # no longer user-editable & should be removed
>         fi
> 
>         # Make sure we can write the pid
> 	(echo test > /var/state/pid/$PACKAGE) >/dev/null 2>&1
>         if [ -f /var/state/pid/$PACKAGE ] ; then
>         	writeable=`cat /var/state/pid/$PACKAGE`
>         else
>                 writeable="no"
>         fi
>         if [ "X$writeable" != "Xtest" ] ; then
>                 $ECHO "Cannot create state file"
>                 return 1
>         fi
> 
>         # Inform the user that $NAME is starting
>         if [ "X$NAME" != "X" ] ; then
>                 $ECHO "Starting $NAME [from $PACKAGE]..."
>         fi
> 
>         # Time to run it; we can handle forking, non-forking
>         if [ $FORKING ] ; then
>                 $DAEMON $@
>                 error=$?
>                 pid=$!
>         else
>                 # this probably can't happen
>                 ($DAEMON $@) &
>                 pid=$!
>                 sleep $SLEEP # time to start or exit
>                 error=$?
>         fi
> 
>         echo $pid > /var/state/pid/$PACKAGE
> 
>         $ECHO "Done"
>         return $error
> }
> # kill via HUP
> hup()
> {       generic_kill HUP $1 $HUP
> }
> # kill via USR1
> usr1()
> {       generic_kill USR1 $1 $USR1
> }
> # the actual kill call
> generic_kill()
> {       if [ ! -r /var/state/pid/$PACKAGE ] ; then
>                 return 1        # program not running
>         fi
> 
>         # Get the pid -- will need to be smarter
>         pid=`cat /var/state/pid/$PACKAGE`
> 
>         # Inform the user
>         if [ "X$NAME" != "X" -a "X$3" != "X" ] ; then
>                 $ECHO $3...
>         fi
> 
>         # kill the pid
>         kill -$1 $2
>         return 0
> }

-- 
see shy jo



Reply to: