How to write and configure startup or init script using linux command chkconfig

If you restart you server and you want your whole stack up and running, you need to configure start or init script. It’s just a simple shell script with some extra headers that the command chkconfig can understand. The approach of adding stratup script is same for most of the distribution with little bit variation in syntax or flow. In the fedora or redhat linux distribution we use the command chkconfig to add startup or init script.

First you need to understand the run levels to understand how linux initiate the start up script

Run Levels

There are different run levels that init script runs as configured, those run levels are described below. Each run level is a state of machine, like 0 describes the shutdown and 6 describes the reboot. This configuration slightly varies between linux distributions. There are different stats assigned from 0 to 6. That is total 7 run levels.

After the Linux kernel has booted, the /sbin/init program reads the/etc/inittab file to determine the behavior for each runlevel. Unless the user specifies another value as a kernel boot parameter, the system will attempt to enter (start) the default runlevel.

levelDescription

0 Halt
1 Single User Text Mode
2 Not Used (User Definable)
3 Full Multi User Text Mode
4 Not Used (User Definable)
5 Full Multi User Graphical Mode
6 Reboot

Little bit more descriptive. Modes may vary little bit as distribution to distribution.

ID Name Description
0 Halt Shuts down the system.
1 Single-user mode Mode for administrative tasks.
2 Multi-user mode Does not configure network interfaces and does not export networks services.
3 Multi-user mode with networking Starts the system normally.
4 Not used/user-definable For special purposes.
5 Start the system normally with appropriate display manager (with GUI) Same as runlevel 3 + display manager.
6 Reboot Reboots the system.

As I said, it’s just a shell script with some extra comments that chkconfig program can understand. Those comments look like

Chkconfig Configuration

Checkconfig is the tool where we can configure our startup script in different  runlevels. This command can also be used to check currently active init scripts

Install chkconfig

If the command chkconfig is not available, it can be installed using package manager (yum on centos)

List Configured Startup Services

Out put describes on which level that corresponding script is set to run.

You can query the specific service by passing that service name as argument

 

….

To configure shell script as a init script using chkconfig, we should keep the following comments in  the file. Usually we put them after shabang line. Formally the chkconfig config/comments would look like. These commands would instruct the chkconfig about init script configuration.
init script are usally located in location /etc/init.d

Enable Service

You can enable the service to run in runlevels 2, 3, 4 and 5  by using following command

# chkconfig service_name on

Let’s say if you want enable httpd service to run on startup

# chkconfig httpd on 

If you want to enable service in particular run level pass the option –level to this command followed by run levels from 0 to 6. Something like,

# chkconfig service_name on –level runlevels

For instance, to enable httpd service in runlevels 2, 3 and 4 by specifying runlevels explicitly

# chkconfig httpd on –level 234

Disable Service

To disable a service in all runlevels (2,3,4 and 5)

# chkconfig service_name  off

Let’s say if you want to disable httpd service in all runleves,

# chkconfig httpd off

You can specify the run explicitly with option –level to  disable those specific runlevels

# chkconfig  service_name off –level runlevels

For instance, to disable httpd in runlevels 2  and 3,

# chkconfig httpd off –level 23

REFERENCES

[1] https://www.redhat.com/archives/psyche-list/2002-December/msg01555.html

 

Leave a Reply

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