Learn HowTo Write Init scripts To Automatically Start Any Service

In one of our earlier articles, we demonstrated how we can add commands or scripts in the  /etc/rc.local file and execute them as part of the system boot. This is useful for many situations but when it comes to starting or stopping applications during system boot and shutdown, it is highly recommended that we create an init script for this purpose. In this article, we will demonstrate how we can create our own init scripts and use it within the sysvinit service startup system.

Anatomy of an init script

The init scripts reside in the /etc/init.d directory which in fact is a soft link to the /etc/rc.d/init.d directory. An init script is a shell script that has some metadata information which is meant to be used by sysvinit followed by the code to manage a service/task/process. As you will see in the sample init script we’ve written and you may also take a look at other init scripts in the /etc/init.d directory, the general structure of the scripts is quite similar. The script will begin with some metadata information. This will be followed by a couple of functions which will contain code for actions to be performed. The functions and the code within them is essentially used to start or stop the service or to check it’s status. After the function definitions have been completed, there would be a case statement. The expression used with the case statement will be the argument specified to the service script. The case statement will contain cases corresponding to the functions that were defined earlier in the script.

Given below is a sample init script I’ve written named arkit-service and kept it under the /etc/init.d directory on my local system.

Init Scripts Example

#!/bin/bash
#
# Demonstrate creating your own init scripts
# chkconfig: 2345 92 65

### BEGIN INIT INFO
# Provides: Welcome
# Required-Start: $local_fs $all
# Required-Stop:
# Default-Start: 2345
# Default-Stop:
# Short-Description: Display a welcome message
# Description: Just display a message. Not much else.

### END INIT INFO
# Source function library.
. /etc/init.d/functions


##Service start/stop functions## 
start() {
    echo "Service starting up"   
    sleep 2
}

stop () {    
    echo "Service shutting down"
    sleep 2
}

status () {
    echo "Everything looks good"
}

##case statement to be used to call functions##
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status 
;;
*)
echo $"Usage: $0 {start|stop|status}"
exit 5
esac
exit $?

Understanding the sample init script

INIT info/metadata
The commented information in the beginning of the script is the metadata information to be used by sysvinit.

chkconfig section
The chkconfig directive defines at which run levels the service should be enabled and also specifies the numbered prefixes to use while creating the links to start up and kill scripts.

Required-Start
Here we define what service should already be available before this service can be started. $local_fs and $all imply that all local file systems should be mounted and this service should be started towards the end of the boot process.

Default-Start
This defines the run levels at which the service should be available.

Required-Stop
This directive implies the service in consideration for which this script is written should stop before the boot facilities or services mentioned in this directive.

Default-Stop
This directive implies the run levels at which the script should be stopped.

Code to start/stop service and get service status

Once this metadata section has been appropriately populated, we come to the actual code that defines the service behavior.
Within the body of the script, we define three functions named start, stop and status consisting of simple echo statements.

Case statement

After defining logic to start/stop and get status of the service, we’ve written a case statement using the first argument written with the script during script execution as the expression for the case statement.We’ve written a couple of cases in the case statement corresponding to common actions we associate with an init script. The logic for defining the service behavior could’ve been written within the case statements themselves but it’s recommended to write the actual code as functions earlier in the script and then call those functions within the case statements. If you read some more scripts under the /etc/init.d directory you would realize that most of the scripts incorporate the behavior that we have demonstrated in this example Init Scripts.

Demonstration

Now that our script is ready, let’s test it out.

First, let’s ensure that the script has execute permissions set.

[root@arkit-centos init.d]# chmod +x arkit-service
[root@arkit-centos init.d]# ls -l arkit-service
-rwxr-xr-x. 1 root root 749 Feb 6 19:36 arkit-service
[root@arkit-centos init.d]#

Now let’s run the script.

[root@arkit-centos init.d]# ./arkit-service status
Everything looks good
[root@arkit-centos init.d]# ./arkit-service
Usage: ./arkit-service {start|stop|status}

The script appears to be running as expected. Now let’s test this using the service command.

[root@arkit-centos init.d]# service arkit-service status
Everything looks good
[root@arkit-centos init.d]# service arkit-service start
Service starting up

Thus far we’ve created our init script and tested that it’s working. Now let’s enable it on boot using the chkconfig command.

[root@arkit-centos init.d]# chkconfig arkit-service on

With that done, we’ll now verify that the soft link names have been set according to the numbers we defined in the chkconfig directive in the init script.

[root@arkit-centos init.d]# cd /etc/rc3.d/
[root@arkit-centos rc3.d]# ls -l S92arkit-service
lrwxrwxrwx. 1 root root 23 Feb 6 19:39 S92arkit-service -> ../init.d/arkit-service
[root@arkit-centos etc]# cd /etc/rc1.d/
[root@arkit-centos rc1.d]# ls -l K65arkit-service
lrwxrwxrwx. 1 root root 23 Feb 6 19:39 K65arkit-service -> ../init.d/arkit-service
[root@arkit-centos rc1.d]#

Conclusion

In this article, we demonstrated with the help of a very simple example how you can create start/stop scripts for a service and then integrate them within the sysvinit system and the server’s boot and shutdown process. We hope that you’ve found this init scripts article to be useful and we look forward towards your feedback.

Related Articles

Awk Scripting with best examples

Wiki

Thanks for your wonderful Support and Encouragement

Sahil Suri

I am a system administrator who loves to learn and share my knowledge with the community. I've been working in the IT industry since 2011.

1 Response

  1. Khadeer says:

    Thanks for the information
    Can someone tell me how do we add statup scripts to run level provided by application teams

Leave a Reply

Your email address will not be published. Required fields are marked *