Creating customized SysV init scripts in Red Hat/Fedora

Most of the software packages running on GNU/Linux distributions are not by default available in the pre-configured repositories. For developers, these software packages are usually compiled and installed. Quite often these source-compiled software requires a daemon to be run inorder to have the correct execution of the software. The service command in fedora can be used to invoke those background process. Installation from source may or may not contain the initialization scripts to start the particular service. In such situations you could write your own init scripts for launching the daemons using the service command. Let’s start with the basics now.

Run levels
A run level basically tells the system, in which mode it should boot into during the boot-up process. Following are the different run levels available in Red Hat/Fedora Linux distributions.

0 – halt
   1 – single user mode
   2 – multiuser mode
   3 – full multiuser mode
   4 – unused
   5 – graphical mode
   6 – reboot

A system will be booting into a run-level specified in /etc/inittab. But if you are using systemd, this default run level is specified by default.target inside /lib/systemd/system/.

General format
#!/bin/bash
#
# <service> <Brief Description>
#
# chkconfig: <runlevels> <start_number> <kill_number>
# description: <More detailed description of the service>
# processname: <process_name>

case $1 in
    start)
        <write commands for initiating the process>
        ;;
    stop)
        <wrtie commands related to stopping the process>
        ;;
    status)
        <get status of the process>
        ;;
    restart)
        <stop and start the process>
*)
esac

Let us now have look at the various fields inside the given template.
service                : Name of the service
runlevels            : Specify the different runlevels that this script is to be started
start_number    : Specify the order in which the script should be started in each level
kill_number      : Specify the order in which the script should be stopped in each level
processname     : Name under which the process will be running in background

You will have to fill up the different cases with corresponding commands accordingly to start, stop or restart the services. When you are done with your script, copy the same to /etc/init.d/ and make it executable by the following command.

chmod a+x /etc.init.d/<service_script>

That’s all regarding the basic stuffs. For more options and examples you can refer to other service scripts under /etc/init.d/.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s