Linux custom startup service and chkconfig usage

Source: Internet
Author: User

Linux custom startup service and chkconfig usage

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.

2. 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.
3. 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.

example: the first three lines of auto_run are as follows:
#! /Bin/sh
# chkconfig: 2345 80 90
# description: the first line of auto_run
indicates the shell used by the system. Therefore, this is the case for shell scripts.
in the second row, chkconfig is followed by three parameters 2345, 80, and 90 to notify chkconfig Program , 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 closes the system, it runs auto_run and adds a Stop command to tell the script, the current mode is disabled.
note that the second and third rows of the preceding three rows are required. Otherwise, an error is returned when chkconfig -- add auto_run is run.
4. common Errors
"the service does not support chkconfig":
check before the script, whether there are two complete rows:
# chkconfig: 2345 80 90
# description: auto_run
there are no fewer lines in front of the script. Otherwise, the chkconfig command cannot report an error.
if an error is always reported when chkconfig is run and 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.