Coredump

Work, play, and everything in-between.

Writing a custom init script

with one comment

It’s been a long time since I did a source tar-ball install. But the production servers at work require that applications that are not part of the standard install base (from Kickstart/Jumpstart and VMWare images) should be compiled from source. It was a very refreshing experience. :)

So after going through the usual configure; make; make install invocation, it was time to make sure that the app is persistent at boot. I would have copied an init script from my *nix desktop, but good thing SLES had a baseline script in /etc/init.d/skeleton.

In a nutshell, the steps are:

  1. Define the INIT INFO section, as per the LSB specs.
  2. Fill in the variables.
  3. Use /etc/rc.status and rc_* functions for sanity checks.
  4. Create the basic start, stop and status functions.
  5. Use YAST (System > System Services (Runlevel)) — or, if available, innserv — to enable the startup script.

Below is a script I wrote for Apache:

#!/bin/sh
#
# Author: Ian Dexter R. Marquez
#
# /etc/init.d/httpd
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the Apache httpd daemon
### END INIT INFO
 
HTTPD=/path/to/apache/2.2.4/bin/httpd
APACHECTL=/path/to/apache/2.2.4/bin/apachectl
PROG=httpd
 
test -x $HTTPD || exit 5
test -s /etc/rc.status && . /etc/rc.status && rc_reset
 
start() {
   echo -n "Starting $PROG: "
   $HTTPD -k start
   rc_status -v
}
 
stop() {
   echo -n "Stopping $PROG: "
   /sbin/killproc $HTTPD
   rc_status -v
}
 
status() {
   echo -n "Checking $PROG: pid "
   /sbin/pidofproc $HTTPD
   rc_status -v
}
 
case "$1" in
   start)
      start
      ;;
   stop)
      stop
      ;;
   restart)
      stop
      start
      rc_status
      ;;
   status)
      status
      ;;
   try-restart|condrestart)
      if test "$1" = "condrestart"; then
         echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
      fi
      $0 status
      if test $? = 0; then
         $0 restart
      else
         rc_reset   # Not running is not a failure.
      fi
      # Remember status and be quiet
      rc_status
      ;;
   graceful|help|configtest|fullstatus)
      $APACHECTL $@
      rc_status
      ;;
   *)
      echo "Usage: $PROG {start|stop|restart|status|graceful|help|configtest|fullstatus}"
      exit 1
esac
rc_exit

References:

Read more:

Written by Ian Dexter

January 24th, 2007 at 3:27 pm

Posted in Work

Tagged with , , ,

One Response to 'Writing a custom init script'

Subscribe to comments with RSS or TrackBack to 'Writing a custom init script'.

  1. Hi, can you help me creating my own script?
    Contcact me by MSN, pĺease, i need to explain my needs:

    Live ID: programad@yahoo.com.br

    Daniel

    30 Jun 07 at 23:16

Leave a Reply