Use chkconfig to manage LINUX System Services

Source: Internet
Author: User
Note: The installation directory of the service is in/etc/rc. d/init. d, and/etc/init. d is the link of/etc/rc. d/init. d. The chkconfig command is used to install and view or modify the startup options of services with system startup. This article contains seven instances to explain how to use the chkconfig command. 1. check service startup in shell script

Note: The installation directory of the service is in/etc/rc. d/init. d, and/etc/init. d is the link of/etc/rc. d/init. d.

The chkconfig command is used to install and view or modify the startup options of services with system startup. This article contains seven instances to explain how to use the chkconfig command.

1. check the settings of the service startup option in the shell script.

When you execute chkconfig and add the service name, if the service is configured to start automatically, it returns true. The following code snippet shows how to check whether a service is configured as automatic start in the script.

# Vi check. sh
Chkconfig network & echo "Network service isconfigured"
Chkconfig junk & echo "Junk service is configured"

#./Check. sh
Network service is configured

You can also check whether it is configured to be automatically started at a run level.

# Vi check1.sh
Chkconfig network -- level 3 & echo "Network service isconfigured for level 3"
Chkconfig network -- level 1 & echo "Network service isconfigured for level 1"

#./Check1.sh
Network service is configured for level 3

2. view the startup options of all services.

The -- list option displays the Configuration status of the startup options of all services.

# Chkconfig -- list
Abrtd 0: off 1: off 2: off 3: on 4: off 5: on 6: off
Acpid 0: off 1: off 2: off 3: off 4: off 5: off 6: off
Atd 0: off 1: off 2: off 3: on 4: on 5: on 6: off
...

View only services started at a certain runlevel, for example, services automatically started under runlevel3.

Chkconfig -- list | grep 3: on

View the setting of startup options for a service, or use grep for the output of chkconfig -- list, for example: chkconfig -- list | grep network

3. add a new service for automatic start.

Use the-add option to add a service so that it can be automatically started when the system starts.

The following instances show how to add a new serviceIptablesTo enable automatic startup when the system starts. Run the 'chkconfig -- add' command to automatically set runlevel 2, 3, 4, and 5.

# Chkconfig -- list | grep iptables
# Chkconfig -- add iptables
# Chkconfig -- list | grep iptables
Iptables 0: off 1: off 2: on 3: on 4: on 5: on 6: off

Note: "chkconfig -- add" only sets an existing service to start automatically. this service must have been installed, that is, it already exists in/etc/rc. d/init. d. If the service does not exist, you need to first install the service under/etc/rc. d/init. d, and then set the automatic start option for it. This is a mistake that many beginners are prone to, so it is worth emphasizing here.

4. delete the setting of automatic startup options for a service.

The following instances show that ip6tables service is configured to start automatically.
# Chkconfig -- list | grep ip6tables
Ip6tables 0: off 1: off 2: off 3: on 4: off 5: off 6: off

To delete the automatic startup setting of a service, use the -- del option as follows:
# Chkconfig -- del ip6tables
# Chkconfig -- list | grep ip6tables

5. set or cancel the automatic startup setting of a service at a certain runlevel.
Sometimes you may not want to cancel the automatic start setting of the entire service. you just need to set or cancel the automatic start setting of the service at a certain runlevel,

The following instances cancel the automatic startup setting of nfserver service in runlevel5.
# Chkconfig -- level 5 nfsserver off
You can also combine multiple runlevels. for example, you can cancel the automatic startup settings of nfserver in runlevel3 and 5.
# Chkconfig -- level 35 nfsserver off

6 chkconfig and rc *. d Directory scripts (* represents the runlevel1-6)
When you use chkconfig to add or delete the setting of automatic startup options for a service, you actually do the following in the rc *. d Directory:
When the chkconfig -- add command is executed, a symbollink file is created under the corresponding rc *. d directory to start and stop the service;
When the chkconfig -- del command is executed, the symbollink file used to start and stop the service is actually deleted in the corresponding rc *. d Directory;

Rc *. in the two symbollink files associated with a service under the d Directory, kill indicates that this file is used to stop the service, starting with S (start) start indicates that this file is used to start the service.

For example, when servicenfsserver does not set the automatic start option, we cannot see the symbollink file used to start or stop the service under/rc *. d.

# Chkconfig -- list | grep nfsserver
Nfsserver 0: off 1: off 2: off 3: off 4: off 5: off 6: off

# Ls/etc/rc. d/rc3.d | grep nfsserver
# Ls/etc/rc. d/rc5.d | grep nfsserver

After we set the automatic startup option for the nfsserver service, we can see the corresponding symbollink file.
# Chkconfig -- add nfsserver
Nfsserver 0: off 1: off 2: off 3: on 4: off 5: on 6: off

# Cd/etc/rc. d/rc3.d
# Ls-l | grep nfsserver
Lrwxrwxrwx 1 root 12 K08nfsserver->.../init. d/nfsserver
Lrwxrwxrwx 1 root 12 S14nfsserver->.../init. d/nfsserver

# Cd/etc/rc. d/rc5.d
# Ls-l | grep nfsserver
Lrwxrwxrwx 1 root 12 K08nfsserver->.../init. d/nfsserver
Lrwxrwxrwx 1 root 12 S14nfsserver->.../init. d/nfsserver

When you cancel the auto-start setting of this service in a runlevel, the symbollink file under the corresponding rc *. d Directory will also be automatically deleted,

# Chkconfig -- level 5 nfsserver off
# Ls/etc/rc. d/rc5.d | grep nfsserver

Complete!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.