CentOS boot service and chkconfig usage.

Source: Internet
Author: User

  1. Service Overview
    In a linux operating system, you often need to create some services which are made into shell scripts. These services must be automatically started when the system starts and closed.
    In the/etc/rc. d/init. d directory of the script to be automatically started, run the command chkconfig -- add filename to enable automatic registration, start, and shut down. The essence is to generate some file connections under the rc0.d-rc6.d directory, these connections connect to the shell scripts of the specified file under the/etc/rc. d/init. d directory.

    Manually create a service
    Create a shell script named auto_run in the/etc/rc. d/init. d directory.
    Set the script running permission chmod + x auto_run.
    Create File connections under the rc0.d-rc6.d directory.
    Ln-s/etc/rc. d/init. d/auto_run/etc/rc. d/rc2.d/S99auto_run
    Ln-s/etc/rc. d/init. d/auto_run/etc/rc. d/rc3.d/S99auto_run
    Ln-s/etc/rc. d/init. d/auto_run/etc/rc. d/rc5.d/S99auto_run
    Ln-s/etc/rc. d/init. d/auto_run/etc/rc. d/rc0.d/K01auto_run
    Ln-s/etc/rc. d/init. d/auto_run/etc/rc. d/rc6.d/K01auto_run

    In this way, when the system starts, it will run auto_run and add the start parameter, which is equivalent to executing the command auto_run start.
    When the system is disabled, auto_run is run and the stop parameter is added, which is equivalent to running the command auto_run stop.

    You can use the command chkconfig -- add auto_run to create the six commands for the connection. This is much simpler and is not prone to errors.
    The following describes the chkconfig command.
  2. Chkconfig command usage
    Syntax:
    Chkconfig -- list [name]
    Chkconfig -- add name
    Chkconfig -- del name
    Chkconfig [-- level levels] name <on | off | reset>
    Chkconfig [-- level levels] name

    Note:
    Chkconfig provides a simple command line tool to help administrators directly operate on many symbolic links under the/etc/rc [0-6]. d directory level.
    This command is authorized by the chkconfig command in the IRIX operating system. You do not need to directly maintain the configuration information in the/etc/rc [0-6]. d directory, but directly manage the link files under/etc/rc [0-6. The configuration information in the running-level directory indicates which services will be started initially.
    Chkconfig has five clear functions: adds a new function for management, deletes a function, lists the startup information of the current service, changes the startup information of a service, and detects the startup status of a special service.
    When chkconfig does not have a parameter to run, it displays its usage. If only one service name is provided, it checks whether the service name has been configured to the current running level. If it is already configured, true is returned. Otherwise, false is returned. The -- level option can be used to query multiple running levels, not just one.
    If on, off, or reset is specified after the service name, chkconfig changes the startup information of the specified service. On or off indicates that the service is enabled or stopped, especially when the running level is changed. The Reset flag resets the startup information of the service.
    By default, the on or off option affects only 2, 3, 4, and 5, while the reset option affects all running levels. The -- level option can be used to specify the running level to receive the impact.
    Note: For each service, each running level has a start point and an end point. When a running level is enabled or disabled, init does not restart a running service or stop a non-running service.
    Option:
    -- Level levels
    Specifies an operation suitable for the running level. The value range is 0-7.
    -- Add name
    Add a new service.
    -- Del name
    Delete a service
    -- List name
    Display service status
    RUNLEVEL FILES
    Each service managed through chkconfig requires two or multiple lines of comment in the corner of its init. d directory. The first line tells chkconfig which running-level services can be started by default, that is, the priority of start or end services. If the service does not have a default level, the build starts at all running levels. A-will be used to replace the running-Level list. The second comment line contains the description of this service, which can be expanded to multiple rows through the backslash symbol.

    For example, the first three lines of auto_run are as follows:
    #! /Bin/sh
    # Chkconfig: 2345 80 90
    # Description: auto_run
    The first line tells the system the shell used, so the shell scripts are like this.
    In the second line, there are three parameters 2345, 80, and 90 following chkconfig to tell the chkconfig program that the parameters must be in rc2.d ~ Under the rc5.d directory, create a file connection named S80auto_run and connect to the auto_run script under the/etc/rc. d/init. d directory. The first character is S. When the system starts the script auto_run, a start parameter is added to indicate that the script is in startup mode. At the same time, in the rc0.d and rc6.d directories, create a file connection named K90auto_run. The first character is K. When the system is shut down, it runs auto_run and adds a stop, tell the script that the current mode is disabled.
    Note that the preceding three rows are required: middle, second, and third. Otherwise, an error is returned when chkconfig -- add auto_run is run.
  3. Common Errors
    "The service does not support chkconfig ":
    Check whether there are two complete rows before the script:
    # Chkconfig: 2345 80 90
    # Description: auto_run
    There are no fewer lines in front of the script. Otherwise, the chkconfig command will report an error.
    If an error is always reported when running chkconfig, if the script is correct, we recommend that you directly go to rc0.d ~ The principle of creating a file connection to the script under rc6.d is the same.

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.