11/03/2013

Centos: make on-boot startup script

This post guide how to make script that start up at boot time on CentOS.

Write a script named, e.g launchd, that follows the pattern below:

Sample script 

#----------------------------------------------------------
#!/bin/bash

# name of service
# chkconfig: 345 90 90
# description: describe what your script would do

start(){

   # create a lock file
   touch  /var/lock/subsystem/launchd

   # tasks to do on boot
}



stop(){
    # remove the lock file
    rm -f /var/lock/subsystem/launchd
   # tasks to do to stop scripts
}

case "$1" in
        start)
                start
        ;;


        
        stop)
                stop
        ;;

*)

esac
# ------------------------------------------------------------------------------------

Elaboration

This script must implement at least start() for the system to call.
The line that starts with # chkconfig  is required to let chkconfig utility know which levels to run this script, start and stop priority.

Put this script in /etc/init.d directory.
Run: chkconfig --add launchd so that chkconfig can recognize your script.
Run: chkconfig launchd on to enable the script at some run-levels. 

Lockfile

Lockfile lets the system know whether there has been a running instance of this script. When the system starts up your script, it checks if lock file exists or not so that it won't have to run the script again and similar for stop(). This is why the function stop() won't be called if you haven't created a lockfile earlier.
If you look at other script in /etc/init.d, you will find all scripts have lock file created in start() and removed in stop().

No comments:

Post a Comment