Account Links: Cart | Your Account | Logout

Skip to content

Red Hat Knowledgebase

Red Hat Knowledgebase Search:

Updated Within the Last:

New Solutions within the last day New Solutions within the last week New Solutions within the last month

Browse by topics:


Click to View a Topic
Red Hat Applications > Developer Suite > Issue <<  9 of 22 >>

Solution Tools:


Email a Solution Postcard Printer version Submit a comment on this answer Update notifications Request an answer Back

Article Reference

Article ID: 5120
Last update: 05-11-06
Issue:
How can I write a System V init script to start and stop my application as a service?
Resolution:

All System V init scripts are named /etc/rc.d/init.d/servicename where servicename is the name of the service. Below is a sample init script.

Please note that one convention of init scripts for Red Hat Enterprise Linux, that may be different on other distributions, is the necessity of touching and removing the lock file, as highlighted in bold in the example below. If these two lines are not present, your script may not execute as expected in all run levels.

#!/bin/bash
#
# chkconfig: 35 90 12
# description: My server
#

#Source function library.
. /etc/init.d/functions

start() {
        initlog -c "echo -n Starting my server: "
        /path/to/servicename &
    ### touch the lock file ###
        touch /var/lock/subsys/servicename
        success $"My server startup"
        echo
}

stop() {
        initlog -c "echo -n Stopping my server: "
        killproc servicename
    ### Remove the lock file ###
        rm -f /var/lock/subsys/servicename
        echo
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status servicename
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
  
exit 0

Line #3 above allows the service to be controlled by chkconfig, which controls starting and stopping the service when rebooting or switching runlevels. For more information on the syntax of this line, see man chkconfig.


How well did this entry answer your question?


good wrong incomplete out of date
Red Hat Applications > Developer Suite > Issue <<   9  of  22  >>