Sunday, August 28, 2016

insserv - Runlevels

Whenever you install a daemon, whether during system installation or by using the rpm utility, an init script is copied to your init directory (/etc/init.d or /etc/rc.d/init.d). You can use either the insserv or chkconfig command to configure which runlevels each of your init scripts are associated with. 
The insserv command reads the information in the INIT INFO block of an init script to determine which runlevels the associated service should run in by default. The INIT INFO block of the ntp init script on an openSUSE system is shown in this example: 
### BEGIN INIT INFO
# Provides:       ntp ntpd xntpd
# Required-Start: $remote_fs $syslog $named
# Required-Stop:  $remote_fs $syslog
# Should-Start: network-remotefs
# Should-Stop: network-remotefs
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Network time protocol daemon (ntpd)
# Description:    Start network time protocol daemon (NTPD).
### END INIT INFO
As you can see in this example, the INIT INFO block provides several key pieces of information for a given Linux daemon: 
The service the init script manages (Provides)
The services that must be started before this service can start (Required-Start)
The default runlevels that the daemon should run in (Default-Start)
The default runlevels that the daemon should not run in (Default-Stop) 
The Default-Start and Default-Stop parameters are used by the insserv command to determine which runlevel directories the appropriate start and kill scripts should be created in. Using the INIT INFO block shown in the preceding example, the insserv command will create start script links in the rc3.d and rc5.d directories while also creating kill script links in the rc1.d and rc2.d directories. 
The syntax for using insserv is insserv service_name. This command enables the daemon specified at the runlevels listed in the Default-Start directive of the INIT INFO block of the service’s init script. It also disables the daemon at the runlevels listed in the Default-Stop directive of the INIT INFO block. 
The insserv utility can also be used to disable the service (thus removing all links for the service in the various rcx.d directories) and stop the service if it is currently running. The syntax to do this is insserv –r service_name
If a service has already been enabled and you want to change its default runlevels, you can use insserv to implement the change. Do the following: 
1. Open the appropriate init script in your init script directory and edit the Default-Start and Default-Stop parameters in the INIT INFO block.
2. Save your changes to the file.
3. Enter insserv –d service_name at the shell prompt.
When you do, the insserv command re-creates the appropriate start and kill script links in the various rcx.d directories. 
You can also enable a daemon using insserv but completely ignore the Default-Start and Default-Stop parameters in its init script. To do so, take these steps: 
1. If the daemon has already been enabled at its default runlevels, remove all start and kill script links by entering insserv –r service_name at the shell prompt.

2. Create new customized start and kill script links by entering insserv service_ name,start=comma-separated_list_of_runlevels. For example, enter insserv ntp,start=3,5.

No comments:

Post a Comment