News
Features
Guest Essay
Technicalia
Security
Community
Commentary
Home

Example 6. Skeleton of a Subsystem control program in /etc/init.d

#!/bin/sh
#
# /etc/init.d/mysystem
# Subsystem file for "MySystem" server
#
# chkconfig: 2345 95 05                                    
# description: MySystem server daemon
#
# processname: MySystem
# config: /etc/MySystem/mySystem.conf
# config: /etc/sysconfig/mySystem
# pidfile: /var/run/MySystem.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings                               
[ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem

RETVAL=0
prog="MySystem"
.
.                                                          
.

start() {                                                  
        echo -n $"Starting $prog:"
        .
        .                                                         
        .
        RETVAL=$?
        [ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
        echo
}

stop() {                                                   
        echo -n $"Stopping $prog:"
        .
        .                                                         
        .
        killproc $prog -TERM
        RETVAL=$?
        [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
        echo
}

reload() {                                                 
        echo -n $"Reloading $prog:"
        killproc $prog -HUP
        RETVAL=$?
        echo
}

case "$1" in                                               
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        reload)
                reload
                ;;
        condrestart)
                if [ -f /var/lock/subsys/$prog ] ; then
                        stop
                        # avoid race
                        sleep 3
                        start
                fi
                ;;
        status)
                status $prog
                RETVAL=$?
                ;;
        *)                                                        
                echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
                RETVAL=1
esac
exit $RETVAL
NOTES:
# chkconfig: 2345 95 05: Besides being comments, these are used by the chkconfig command and must be present. This particular line defines that on runlevels 2,3,4 and 5, this subsystem will be activated with priority 95 (one of the last), and deactivated with priority 05 (one of the first).
# pull in sysconfig settings: Besides your software's own configuration, this script can also have a configuration file. The standard place for it is under the /etc/sysconfig directory, and in our case we call it mySystem. The followinng code line reads this configuration file.
start() {, stop() {, reload() {: Your script can have many functions, but it is chiefly responsible for the implementation of start and stop methods, because they are responsible for (de)activation of your Subsystem on boot. Other methods can be called from the command line, and you can define as much as you want.
case "$1" in: After defining the script actions, the command line is analyzed and the requested method (action) is called.
*) : If this script is executed without any parameter, it will return a help message like this:
bash# /etc/init.d/mysystem
Usage: mysystem {start|stop|restart|reload|condrestart|status}
. : Here you put your Software's specific command.