RHCSA: manage system start up with chkconfig

If you’re familiar with managing system startup and shutdown services in the rc#.d directory structure, you know what a pain it can be. chkconfig is a utility available in Red Hat that simplifies the steps to adding, updating and removing services from this directory tree.

If managing services is new, here’s a quick rundown. A copy of all the services or commands that need to execute when the system is booted or shutdown can be found in /etc/init.d. The challenge is specifying the order in which to start and stop services as well as which services should be stopped or started at each OS run level.

Linux has the following run levels:

Run Level Description
0 Shutdown
1 Single user or Rescue Mode, console login only, networking disabled
2 Multi-user, console login only, networking disasbled
3 Multi-user, console logins only, networking enabled
4 Not used or user defined
5 Multi-user, graphical desktop and console logins, networking enabled

For each run level there is a corresponding /etc/rc#.d directory. An entry or link needs to be placed in each directory for a service. The name of the link indicates whether or not the service should be started or stopped and in which order. Names start with ‘S’ or ‘K’. ‘S’ indicates start and ‘K’ indicates kill or stop. The next two characters are numbers that can range from 00 to 99. The numbers indicate the order in which to run the commands.

Here is an example directory listing from an /etc/rc2.d directory:

# ls
K00ipmievd         K15svnserve    K72autofs      K87multipathd      K95kudzu            S12syslog               S58ntpd
K01dnsmasq         K20nfs         K73ypbind      K87portmap         K99cpuspeed         S13irqbalance           S64mysql
K01setroubleshoot  K24irda        K74haldaemon   K88wpa_supplicant  K99mAdmin        S15mdmonitor            S80sendmail
K02avahi-daemon    K35smb         K74ipmi        K89dund            K99readahead_later  S25pcscd                S85gpm
K02avahi-dnsconfd  K35vncserver   K74nscd        K89iscsi           S00microcode_ctl    S26acpid                S90crond
K02NetworkManager  K35winbind     K75netfs       K89iscsid          S03sysstat          S26hidd                 S90xfs
K05atd             K44rawdevices  K80kdump       K89netplugd        S04readahead_early  S26lm_sensors           S95anacron
K05conman          K50netconsole  K85mdmpd       K89pand            S08ip6tables        S26lvm2-monitor         S97rhnsd
K05saslauthd       K50snmpd       K85messagebus  K89rdisc           S08mcstrans         S50hplip                S97yum-updatesd
K05wdaemon         K50snmptrapd   K85rpcgssd     K90bluetooth       S10network          S55mysql-monitor-agent  S99local
K10psacct          K50xinetd      K85rpcidmapd   K92iptables        S11auditd           S55sshd                 S99smartd
K15httpd           K69rpcsvcgssd  K86nfslock     K95firstboot       S12restorecond      S56cups 

If the server is booted into run level 2, the OS will run the commands found in /etc/rc2.d according to sort order. The system will start with all the commands beginning with ‘K’, executing them with the ‘stop’ parameter. It will next run all the commands beginning with ‘S’ and passing the ‘start’ parameter. So for example, going by sort order the S56cups command will be executed after S55shd. All of the commands found in /etc/rc2.d are symbolic links to a script file located in /etc/init.d.

Adding a new service requires placing the service script or executable in /etc/init.d and then creating the appropriate symbolic links in each /etc/rc#.d directory. By now you should get the idea that maintaining all these links by hand is not a fun task, which brings us to chkconfig.

There are 3 basic tasks you should know how to perform with chkconfig:

chkconfig –list
chkconfig –add
chkconfig –del

I recently created a script to start and stop all of the mysql servers. It needs to start the instances when the host boots into run level 2, 3, 4 or 5. For all other run levels the databases should be shutdown. For chkconfig to know the run levels as well as the order number to assign to my script, the following lines need to be added to the top of the script file:

# chkconfig: 2345 99 99
# description: Starts and stops mysqld processes for all \
#              mysql databases on the server.

The first line contains the numbers 2345 99 99. The ’2345′ are the run levels where the service should be turned On. By extension all other run levels will have this service set to Off. The first ’99′ indicates the order number for starting the script. I chose 99 so that the script will be started at the very end of the list. The last ’99′ indicates the order in which to stop the service. Again, my service will be one of the last to be stopped.

With a link in /etc/init.d to the actual location of the script in place, I’m ready to run chkconfig:

# ls -lrt
total 792
-rwxr-xr-x 1 root root  1778 Jul 12  2006 gpm
-rwxr-xr-x 1 root root  1877 Jul 12  2006 portmap
-rwxr-xr-x 1 root root  1624 Aug  1  2006 irda
-rwxr-xr-x 1 root root  1441 Dec 18  2006 anacron
-rwxr-xr-x 1 root root  1470 Jun 28  2007 conman
-rwxr-xr-x 1 root root  1331 Nov  9  2007 krb524
-rwxr-xr-x 1 root root  1498 Jan 18  2008 yum-updatesd
-rwxr-xr-x 1 root root   299 Apr 21  2008 cups-config-daemon
-rwxr-xr-x 1 root root  1203 Jul  9  2008 pand
-rwxr-xr-x 1 root root   966 Jul  9  2008 hidd
-rwxr-xr-x 1 root root   996 Jul  9  2008 dund
-rwxr-xr-x 1 root root  1477 Jul  9  2008 bluetooth
-rwxr-xr-x 1 root root  1030 Jul 16  2008 psacct
-rwxr-xr-x 1 root root  1914 Nov 20  2008 mcstrans
-rwxr-xr-x 1 root root  2193 Apr 14  2009 irqbalance
-rwxr-xr-x 1 root root  1387 Apr 24  2009 rdisc
-rwxr-xr-x 1 root root  1407 Aug 25  2009 dnsmasq
...
lrwxrwxrwx 1 root root    31 Mar 21 10:32 mAdmin -> /mysql-common/scripts/mAdmin.sh

Running chkconfig with the –add options will create all the links for us:

# chkconfig --add mAdmin
[root@dbtdev98 etc]# find ./rc*.d -name *mAdmin* | sort
./rc.d/init.d/mAdmin
./rc.d/rc0.d/K99mAdmin
./rc.d/rc1.d/K99mAdmin
./rc.d/rc2.d/S99mAdmin
./rc.d/rc3.d/S99mAdmin
./rc.d/rc4.d/S99mAdmin
./rc.d/rc5.d/S99mAdmin
./rc.d/rc6.d/K99mAdmin

To see a summary for our script we can use the –list option:

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

Using the –delete option will remove all the links, but what if we just want to turn on or off one run level for our service? Use the –level option: chkconfig –level levels service_name

Let’s say we want to turn the service off for run level 3:

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

Turn it back on by using the same command and flipping ‘off’ to ‘on.’ Or, use the reset option without providing a run level, and all run levels will be reset according to the directives in the script header:

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

Hopefully this demonstrates the ease of using chkconfig to manage system start up. For those new to Linux, the biggest challenge will likely be remembering each of the run levels and remembering how to setup the directives for chkconfig at the top of the script file. And of course there is that whole ‘writing the script’ thing too… but that’s another post.

About Chris Houseknecht
Web Developer / Architect / System Administrator When I'm at work, I'm elbow deep in web development, trouble shooting integration issues, creating automation tools for the data center, solving performance challenges, and sometimes delving into Linux system administration. Tackling challenging problems and solving puzzles gets me excited. Want to see what sort of things I'm working on, stop by my blog chouseknecht.com

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: