How to use the service management command chkconfig in Linux

Source: Internet
Author: User
Tags stop script
Chkconfigchkconfig is frequently used in command line operations. It allows you to conveniently set and query system services at different running levels. You can easily manage your startup services. Note: Remember that chkconfig does not automatically disable or activate a service immediately, but simply changes the symbolic connection. C

Chkconfig

Chkconfig is frequently used in command line operations. It allows you to conveniently set and query system services at different running levels. You can easily manage your startup services.
Note: Remember that chkconfig does not automatically disable or activate a service immediately, but simply changes the symbolic connection.

Chkconfig syntax:

Chkconfig [-- add] [-- del] [-- list] [System Service]
Chkconfig [-- level/levels] [level code] [system service] [on/off/reset]
Syntax explanation:

When chkconfig does not have a parameter for running, the usage is displayed. If the service name is added, check whether the service is started at the current running level. If yes, true is returned; otherwise, false is returned. If on, off, or reset is specified after the service name, chkconfi changes the startup information of the specified service. On and off indicate that the service is started and stopped, and reset indicates that the service startup information is reset, no matter what is specified by the problematic initialization script. On and off switches. by default, the system is only valid for 3, 4, and 5 running levels, but the reset can be valid for all running levels.
-The-level option specifies the running level to be viewed, not necessarily the current running level.
It must be noted that for each running level, there can be only one start script or stop script. When you switch to the running level, init does not restart the started service or stop the stopped service again.
Chkconfig? -List: displays the running status information (on or off) of all running system services ). If the name is specified, only the status of the specified service at different running levels is displayed.
Chkconfig? -Add name: adds a new service. Chkconfig ensures that each running level has a startup (S) or kill (K) entry. If the init script is missing, it is automatically created from the default init script.
Chkconfig? -Del name: delete the service and delete the connection from/etc/rc [0-6]. d.
Chkconfig [-- level | levels] [runtime level] system service [on | off | reset]: Sets whether a service is started, stopped, or reset at the specified runtime level.
For example, to stop the nfs service at level 3, 4, and 5, the command is as follows:

Chkconfig -- level 345 nfs off

Run-level files:

Each service managed by chkconfig needs to add two or more lines of comments to the script in the corresponding init. d.

The first line tells chkconfig the default startup running level and the priority of start and stop. If a service is not started at any running level by default, use-to replace the running level.

The second line describes the service. you can use the \ cross-line comment.

For example, random. init contains three rows:

# Chkconfig: 2345 20 80
# Description: Saves and restores system entropy pool \
# Higher quality random number generation.

This document introduces the concept of running level in Linux:

Linux OS divides the operating environment into the following seven levels:
0: Shutdown
1: Single user mode (single user, no network)
2: Multi-user mode without network support (multi-user, no network)
3: Multi-user mode with network support (multiple users and networks)
4: reserved, not used
5: Multi-user mode with network support and X-Window support (multi-user, network, X-Window interface)
6. reboot the system.

In Linux, there are multiple running levels, common being 2, 3, 4, and 5 for multiple users. many people know that 5 is the level for running X-Windows, while 0 is the shutdown. You can use the init command to change the running level. For example, if you want to maintain the system to enter the single-user status, you can use init1 to switch. During the Linux running-level switch, the system will automatically find the corresponding running-level directory/etc/rc [0-6]. files starting with K and S under d are executed in numerical order. The maintenance of these scripts is cumbersome. Linux provides the chkconfig command to update and query system services at different running levels.

Example:

1. view the status of various services in different execution levels:

$ Chkconfig -- list

2. list the startup status of the system service vsftpd at each execution level:

$ Chkconfig -- list vsftpd

3. disable the vsftpd system service when the execution level is 3 or 5:

$ Chkconfig -- level 35 vsftpd off

4. enable the vsftpd system service for levels 2, 3, and 5:

$ Chkconfig -- level 235 vsftpd on

. Restart the vsftpd system service when the execution level is 2, 3, and 5:

Chkconfig -- level 235 vsftpd restart

5. disable services that you do not need:

If no printer is available:

$ Chkconfig -- level 235 cups off

If there is no Lan:

$ Chkconfig -- level 235 smb off

If you do not need remote user logon:

$ Chkconfig -- level 235 sshd off

If you do not need a scheduled task:

$ Chkconfig -- level 235 crond off

If you do not need to add new hardware:

$ Chkconfig -- level 235 kudzu off

View the process status of a specific system service, such as httpd:

$ Chkconfig -- list | grep httpd

1. how to add a service?

First, the service script must be stored in the/etc/ini. d/directory;
Second, you need to use chkconfig ?? Addservicename to add this service to the chkconfig tool service list. in this case, the service will be assigned a K/S entry in/etc/rc. d/rcN. d.
Finally, you can modify the default startup level of the service as instructed above.

2. in Linux, how does one automatically start MySQL?

First, make sure that/etc/rc. d/init. d/MysqlD exists. if the service is usedMysqlD start can be started normally, indicating that the service exists
(Note: For rpm installation, the corresponding service is automatically registered in the/etc/rc. d/init. d directory ).
Run the following command:

Chkconfig -- add mysqld

Run the following command:

Chkconfig -- level 345 mysqld on

Restart

How does one install the source code and enable the software to start up? take Apache as an example?
In linux, the apache service is installed (by downloading binary files for economical compilation and installation, rather than rpm packages) and the apache service startup command is as follows:
/Server/apache/bin/apachectl start. Run the apache service under runlevel 3. The command is as follows:


Touch/etc/rc. d/init. d/apache
Vi/etc/rc. d/init. d/apache
Chown-R root/etc/rc. d/init. d/apache
Chmod 700/etc/rc. d/init. d/apache
Ln-s/etc/rc. d/init. d/apache/etc/rc. d/rc3.d/S60apache # S is short for start, meaning start, K is short for kill, and stands for close. The number 60 indicates the order of startup.


Apache content:


#! /Bin/bash
# Start httpd service
/Server/apache/bin/apachectl start


Now the apache service can be automatically started at random under runtime level 3.

(Note: after the/etc/rc. d/init. d/apache file is created, you can adjust the startup service with chkconfig .)

In addition, you can directly write the startup command/server/apache/bin/apachectl start to the/etc/rc. d/rc. local file to enable startup!

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.