Quick and Dirty How To Write an Init Script

June 4, 2007

Often times you may have an application that needs to be boot strapped or started automatically via an Init script. Below is a quick and dirty sample Init script to help you get on your way. Also the the article will cover chkconfig options to start the script at the correct run level.

The script will reside in /etc/init.d/BLAH

# Below is the chkconfig syntax for auto startup at different run levels
# Note runlevel 1 2 and 3, 69 is the Start order and 68 is the Stop order
# Make sure these are unique by looking into /etc/rc.d/*
# Also below is the description which is necessary.
# chkconfig: 123 69 68
# description: Description of the Service
# Below is the source function library
. /etc/init.d/functions
if [ -f /etc/sysconfig/BLAH ]; then
. /etc/sysconfig/BLAH
# Below is the Script Goodness controlling the service
case "$1" in
echo -n "Start service BLAH"
/usr/sbin/BLAH start
echo -n "Stop service BLAH"
/usr/sbin/BLAH stop
echo -n "Restart service BLAH"
/usr/sbin/BLAH restart
echo "Usage: $0 {start|stop|restart}"
exit 1

Now to run chkconfig commands:

bash-3.00$ chkconfig BLAH --add
bash-3.00$ chkconfig BLAH on

The above will read the Init script and add and enable the script at the proper run level
specified in the script. Appending the start command on boot.

You could also do the below to be more specific in which run level to enable:

bash-3.00$  chkconfig --level 123 BLAH on (to turn on at run level 1 2 3)


bash-3.00$ chkconfig --level 123 BLAH off (to turn off at run level 1 2 3)

To list all run:

bash-3.00$ chkconfig --list

This should be a quick and dirty to get you going.

Comments for “Quick and Dirty How To Write an Init Script”

  1. Ravi commented on August 16, 2015

    thanks a lot dude for this.. helped me

Leave a Reply