Tuesday, March 16, 2010

How do I modify a service startup script so that chkconfig can manage in which runlevels it's active?

Problem

Not all scripts with start/stop/status options are manageable by chkconfig, but you can add a self-defined script into system service which can be handled by chkconfig.



Environment
Red Hat Enterprise Linux 3, 4, 5



Resolution

1. The script should adhere to the following requirements:

1) Indicates what shell is used to run the script.


2) Indicates which run level should the service be started by default as well as the start and stop priority levels. The start/stop priority levels are important for services that depend on other services.In the following example, the numbers 345 after the chkconfig directive represent the runlevels that the service will be enabled for by default. In this example the service will be enabled by default for runlevels 3, 4, and 5. The 99 is the start priority. The lower the number the higher the priority and the sooner a service will be started. The 01 is the stop priority. The lower the number the higher the priority and the sooner a service will be stopped when changing runlevels.



3) A short description for the service. The description may extend up to several (commented) lines as necessary. Use the backslash character if the comment extends to several lines.


Below is an example of /etc/init.d/yourscript :


#!/bin/sh
#
# chkconfig: 345 99 01
#
# description: your script is a test service
#
case $1 in
start)
echo "your script starting"
# TODO:
;;
restart)
echo "your script stopping"
echo "your script starting"
# TODO:
;;
status)
echo "your script running"
# TODO:
;;
stop)
echo "your script stopping"
# TODO:
;;
esac
2. Make sure that the script is executable.


# chmod 0755 /etc/init.d/yourscript
3. Create a soft link to /etc/init.d/yourscript


If you want to start the service in runlevel 5:

# cd /etc/rc.d/rc5.d
# ln -s ../init.d/yourscript S99yourscript

If you want to start the service in runlevel 3:

# cd /etc/rc.d/rc3.d
# ln -s ../init.d/yourscript S99yourscript
4. Add the script to service


# chkconfig --add yourscript
5. Now, you can test this self-defined service


# service yourscript start
# service yourscript status
# service yourscript stop
6. If you want to yourscript started when system boot up


# chkconfig yourscript on

Finally, if you do not want to put yourscript into service, you can just call it from /etc/rc.d/rc.local.


# echo /etc/init.d/yourscript >> /etc/rc.d/rc.local


link--> http://kbase.redhat.com/faq/docs/DOC-7239

No comments: