The Chkconfig command is used to set, view or change the services that are automatically started at startup. Five practical examples to illustrate the use of the Chkconfig command.
1. Using Shell Script to Detect Service System Startup Item Status
When you execute the chkconfig command with only the service name, it returns true if the service has been configured to the system startup item. The following code snippet is used to check whether a service has been configured to start up.
# vi check.sh chkconfig network && echo “Network service is configured” chkconfig junk && echo “Junk service is configured”
# ./check.sh Network service is configured
You can also specify to check whether the service is configured to the specified run level.
# vi check1.sh chkconfig network –level 3 && echo “Network service is configured for level 3” chkconfig network –level 1 && echo “Network service is configured for level 1”
# ./check1.sh Network service is configured for level 3
2. View the status of current service system startup items
The –list option is used to display the status of the system startup items for all current services.
You can use grep to filter and display services with specified conditions.
The following command shows only services with run level 3.
chkconfig –list | grep 3:on
The following command shows only the startup item status of the network service.
chkconfig –list | grep network
3. Add a new service to the startup
Use the –add option to add a specified service to the system startup service list.
The following example shows how to add a new service (such as iptables) to the list of services that need to be started. The “chkconfig -add” command will also automatically enable runlevels 2, 3, 4, and 5, as follows: