≡ Menu

10 insserv Command Examples for Linux System Init Scripts

insserv command is used to control the start and stop order of the services that are on a Linux system.

It enables an installed system init script (boot script) by reading comment header of the init script and calculating the dependencies between all the scripts.

Insserv scans for the system facilities in the configuration file /etc/insserv.conf and the directory /etc/insserv.conf.d.

1. init.d Script Header Format

All the scripts under /etc/init.d directory should have header like below. Even if the lines are commented out (like below) in the header section of each script, insserv should still be able to read all these lines and enables all init scripts accordingly.

In the following example, the line starting with “# Required-Start: $local_fs boot.localnet” lets the insserv to run/verify the $local_fs script and boot.localnet scripts next before boot.kdump script can be executed.

In the same way, “# Required-Stop: $local_fs boot.localnet” lines lets insserv to stop the services before this script is executed.

If you are new to init scripts, you should understand how you can write custom init scripts based on LSB Init Standard.

# cat boot.kdump
#!/bin/bash
#
# Copyright 2005 Red Hat, Inc.
# Author: Jeff Moyer <jmoyer@redhat.com>
### BEGIN INIT INFO
# Provides:    boot.kdump
# Required-Start: $local_fs boot.localnet
# Should-Start:
# Should-Stop:
# Required-Stop: $local_fs boot.localnet
# Default-Start: B
# Default-Stop:
# Short-Description: kdump boot configuration
# Description:  This script loads the kdump kernel on startup.
### END INIT INFO
. /etc/sysconfig/kdump
. /etc/rc.status

2. insserv.conf File Format

The following is an example of the /etc/insserv.conf, which is the config file used by the insserv command.

# cat /etc/insserv.conf

# All local filesystems are mounted (done during boot phase)
$local_fs    boot.localfs +boot.crypto

# Low level networking (ethernet card)
$network    network

# Named is operational
$named     +named +dnsmasq +lwresd $network

# All remote filesystems are mounted (note in some cases /usr may
# be remote. Most applications that care will probably require
# both $local_fs and $remote_fs)
$remote_fs   $local_fs +nfs +smbfs

# System logger is operational
$syslog     syslog

# SunRPC portmapper available
$portmap    portmap

# The system time has been set correctly
$time      boot.clock +xntpd

# Services which need to be interactive
<interactive>  apache apache2 boot.clock boot.crypto boot.crypto-early boot.localfs boot.rootfsck kbd kdump ntp

In the above config file:

  • $network is pseudo name referencing to the script /etc/init.d/network.
  • Similarly you will see the pseudo name for each script in /etc/insserv.conf. These are the pseudo names that will be used in the boot script header section.
  • Each line which begins with $ and a following name defines a system facility accordingly to the Linux Standard Base Specification (LSB)
  • All names followed by such a system facility will declare the required dependencies of the facility.
  • If the service with the name after the plus sign is available, it will be used. If it is not available, it is ignored silently.
  • Words beginning with < and ending with > are keywords.
  • Currently <interactive> is the only know keyword for marking a service as an interactive one, e.g. a service which requires a passphrase or password input during boot or runlevel change.

3. Add a Service

Here is an example on how to add a simple service using insserv utility.

First, verify the executable file is under /etc/init.d directory.

# ls -l /etc/init.d/joystick
-rwxr-xr-x 1 root root 2296 May 5 2010 /etc/init.d/joystick

Next, run the insserv command to enable the joystick service.

# insserv joystick

After the above insserv command, execute chkconfig to verify that it is added to the startup list.

# chkconfig --list | grep joystick
joystick         0:off 1:off 2:on  3:on  4:off 5:on  6:off

Please note that the above is a very simple example without dependencies. But, the power of insserv is where it will automatically resolve and add all appropriate dependent scripts.

4. Perform Test Run

To perform dry run without enabling any of the services, use the -n flag as shown below.

# insserv –n
insserv: Loading ypserv
insserv: Loading /etc/insserv/overrides/ypserv
insserv: Loading network
insserv: Loading /etc/insserv/overrides/network
insserv: Loading aaeventd
insserv: Loading /etc/insserv/overrides/aaeventd
insserv: Loading ntp
insserv: Loading hawk
insserv: Loading /etc/insserv/overrides/hawk
insserv: Loading fetchmail
insserv: Loading /etc/insserv/overrides/fetchmail
insserv: Loading boot.cycle
insserv: Loading /etc/insserv/overrides/boot.cycle
insserv: Loading autoyast
..
..

5. Remove a Service

Use option -r as shown below to remove a service.

# insserv -r joystick

Afer you remove a service, you’ll not see it in the chkconfig output.

# chkconfig --list | grep joystick

6. Use Default Run Level

To use the default run-levels defined in the scripts use the -d option. If you’ve edited a runlevel link scheme, this may restore those changes.

# insserv –d

In order to use the default run-levels defined in the scripts use –d option. In the following example, insserv will use the run-level defined in the script instead of executing the script from the run-level defined by the system or the user.

# insserv –d <script name>

# insserv -d /etc/init.d/lighttpd

# chkconfig --list | grep lighttpd
lighttpd                  0:off  1:off  2:off  3:on   4:off  5:on   6:off

7. Force Run the Service

To force run ignoring the required service use the -f option as shown below.

# insserv –f

To force the system to ignore the other required services or dependent scripts use –f option. In the following example, in order for the /etc/init.d/openhpid script to be executed, other dependencies like network, remote ,syslog services needs to be started first.

# more /etc/init.d/openhpid
#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          openhpid
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start OpenHPI daemon at boot time
# Description:       Enable OpenHPI service which is provided by openhpid.
### END INIT INFO

You can force the system to execute the /etc/init.d/openhpid script ignoring the status of network, remote, syslog services by using the insserv command as shown below:

# insserv -f /etc/init.d/openhpid

Also, you can use -v option along with any of the above output to see more details on what the command is doing.

# insserv –dv

8. Change the Script Directory

As you see from all the previous examples, the script is placed under /etc/init.d directory by default. If you want insserv to use different directory for the script, then use the -p option.

For example, the following will use the /etc/init.d.custom directory as the script location.

# insserv –p /etc/init.d.cusom

9. Insserv Custom Config File

To specify the path of the config file use the -c option. This is used for both insserv.conf file and the insserv.conf.d directory.

# insserv –c <config file>

Let us say for example, you would like to use a different path for the configuration file instead of /etc/insserv.conf then you can use the command as shown in the following example.

This is very helpful when you want to make changes to /etc/insserv.conf file and run a dry-run (-n) procedures for validation purposes. Backup the original file, modify with the changes you need and then point to the new location.

Once you are done with all your testing, you can run the same command to revert to the original configuration file or use the new location.

# insserv -c /var/tmp/insserv_karthik.conf

10. Insserv Override and Related Files

To override the path to replace /etc/insserv/overrides, please use the following option.
By default LSB comment headers found under the default /etc/insserv/overrides directory will override the LSB comment headers for the scripts found under the /etc/init.d directory.

But, if you want to specify your own override directory, use -o option. The following defines /etc/insserv/my.overrides as the override directory.

# insserv –o /etc/insserv/my.overrides

In order to override or replace the LSB comment headers with the new comments, you can create a file with the new headers in the specific path and instruct insserv to look for new file instead of looking for comments defined in the original script.

In the following example, /etc/init.d/splash is a script that has the below LSB comments, these comments are you used to determine which services needs to be started or stopped first.

# more /etc/init.d/splash
#! /bin/bash
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Michael Schroeder <feedback@suse.de>
#
# /etc/init.d/splash
# /usr/sbin/rcsplash
#
# System startup script for console splash screens
#
### BEGIN INIT INFO
# Provides: splash
# Required-Start: $remote_fs
# Should-Start:   fbset
# Required-Stop:  $remote_fs
# Should-Stop:    fbset
# Default-Start:  1 2 3 5 S
# Default-Stop:
# Description:    Splash screen setup
### END INIT INFO

If you do not want to use the above LSB comments and also do not wish to modify the existing script then you can copy the file to /etc/insserv/overrides directory and run the below insserv command to tell the system to use the script defined in the override directory,

insserv -o splash

You need not specify the location of the override directory as the default path is /etc/insserv/overrides. You can also run insserv dry-run (-n) to verify how the script is getting executed.

The following are the various files that will be processed by the insserv command:

  • /etc/insserv.conf – Configuration file for insserv which defines the LSB System Facilities.
  • /etc/insserv.conf.d/ – Directory for further configuration files for declaring LSB System Facilities.
  • /etc/insserv/overrides/ – Path to replace existing LSB comment headers with the comment headers found in this path.
  • /etc/init.d/ – Path to the SuSE init script base directory as required by the Linux Standard Base Specification (LSB)
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • jameslee August 7, 2014, 3:49 am

    Good to know.thanks

  • Bhagyaraj August 10, 2014, 9:50 pm

    Hi,
    In my CentOS, not able to see/get insserv commmd, which flavour of Linux you refering.

    [root@blrec12-vm6 boot]# cat /etc/issue
    CentOS release 5.9 (Final)
    Kernel \r on an \m
    [root@blrec12-vm6 boot]# yum install ins*
    Loaded plugins: fastestmirror, security
    Loading mirror speeds from cached hostfile
    * base: centos.aol.in
    * extras: centos.aol.in
    * updates: centos.aol.in
    Setting up Install Process
    No package ins* available.
    Nothing to do
    [root@blrec12-vm6 boot]#

  • Bhagyaraj August 11, 2014, 9:50 pm

    Hi all,
    Have anybody tried this,?
    Are you able to find the command.