Monday, July 23, 2012

chkconfig Command Examples

chkconfig command is used to setup, view, or change services that are configured to start automatically during the system startup.

chkconfig has five distinct functions: adding new services for management, removing services from management, listing the current startup information for services, changing the startup information for services, and checking the startup state of a particular service.

chkconfig --list [name]
chkconfig --add name
chkconfig --del name
chkconfig [--level levels] name <on|off|reset>
chkconfig [--level levels] name 

OPTIONS

--level levels
Specifies the run levels an operation should pertain to. It is given as a string of numbers from 0 to 7. For example, --level 35 specifies runlevels 3 and 5.
--add name
This option adds a new service for management by chkconfig.
--del name
The service is removed from chkconfig management.
--list name
This option lists all of the services which chkconfig knows about, and whether they are stopped or started in each runlevel. If name is specified, information in only display about service name
 
Example : 

1 .  How to add any scripts in the chkconfig ?

You have to modify the script a little bit to make it work at boot time. Just add the following lines at the beginning of the file

#!/bin/sh
#chkconfig: 2345 80 30 
#description: Service Description


This says that the random script should be started in levels 2, 3, 4, and 5, that its start priority should be 80, and that its stop priority should be 30. You should be able to figure out what the description says; the \ causes the line to be continued. The extra space in front of the line is ignore
Note : There is no space in between '#' and 'chkconfig'.

2.  Copy your scripts into init.d folder.
    copy the scripts file to /etc/init.d.

3. How to add service to the startup

   chkconfig --add service_name

4. How to view status of startup services
     chkconfig --list
     To view particular service : chkconfig --list | grep service_name
   
 5. How to remove a service from startup list
   chkconfig --del service_name

6. How to Turn-on or Turn-off a service for a selected Run level
   Sometimes you might not want to delete the whole service. Instead, you might just want to turn the  flag on or off for a particular run level (for a particular service).
   chkconfig --level 3 service_name on/off 

 7. How to check service startup status from shell script
    create a file check.sh
    vi check.sh
    chkconfig service_name && echo "${service_name} service is configured"
    chkconfig service_name --level 3 && echo "${service_name} service is configured for level 3"
  
   ./check.sh


NOTE:

 - Run level 0 – /etc/rc.d/rc0.d/
 - Run level 1 – /etc/rc.d/rc1.d/
 - Run level 2 – /etc/rc.d/rc2.d/
 - Run level 3 – /etc/rc.d/rc3.d/
 - Run level 4 – /etc/rc.d/rc4.d/
 - Run level 5 – /etc/rc.d/rc5.d/
 - Run level 6 – /etc/rc.d/rc6.d/

 - Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
 - Programs starts with S are used during startup. S for startup.
 - Programs starts with K are used during shutdown. K for kill.
 - There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.