#! /Bin/bash
# Chkconfig: 35 99 05
# Description: Test chkconfig
# File:/etc/init. d/foo
Env>/tmp/envlog
35: Which runlevel is started?
99: s99foo is generated under rc3.d and rc5.d.
05: k05f00 is generated under rc0.d, rc1.d, rc2.d, rc4.d, and rc6.d.
S: Start
K: Indicates kill.
Init running level and command
1. What is INIT:
Init is an indispensable program in Linux.
The INIT process is a user-level process started by the kernel.
After the kernel is started by itself (it has been loaded into the memory, started to run, and initialized to all the device drivers and data structures), it starts a user-level program init, complete the boot process. Therefore, init is always the first process (its process number is always 1 ).
In the past, the kernel used init to find it. The correct position (for Linux) is/sbin/init. If the kernel cannot find init, it will try to run/bin/sh. If the run fails, the system will also fail to start.
Ii. Operation Level
So what is the runtime level?
Simply put, the running level is the function level currently running in the operating system. This level ranges from 1 to 6 and has different functions.
Different runtime levels are defined as follows:
#0-stop (never set initdefault to 0)
#1-single-user mode # s init S = init 1
#2-multiple users without NFS
#3-full multi-user mode (Standard Operation Level)
#4-useless
#5-X11 multi-user graphic mode (XWindow)
#6-Restart (do not set initdefault to 6)
These levels are specified in the/etc/inittab file. This file is the main file found by the INIT program. The first service to run is the file stored in the/etc/rc. d directory. In most Linux releases, the startup script is located in/etc/rc. d/init. d. These scripts are connected to the/etc/rc. d/rcN. d directory by using LN commands. (Here N is the running level 0-6)
3): The chkconfig command (in the RedHat operating system)
Unlike DoS or windows, Linux can have multiple running levels. The common ones are the 2, 3, 4, and 5 of 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 init 1 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.
Syntax:
Chkconfig -- list [name]
Chkconfig -- add name
Chkconfig -- del name
Chkconfig [-- level levels] Name
Chkconfig [-- level levels] Name
Chkconfig has five functions: Add a service, delete a service, list a service, change the startup information, and check the startup status of a specific service.
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. The -- level option specifies the running level to be viewed, not necessarily the current running level.
If on, off, or reset is specified after the service name, chkconfig changes the startup information of the specified service. On and off indicate the start and stop of the service when the running level is changed. Reset refers to the initialization service information, no matter what is specified by the problematic initialization script.
For ON and OFF switches, the system is only valid for 3, 4, and 5 by default, but the reset can be valid for all running levels. You can select a specific running level when the -- level option is specified.
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.
Options:
-- Level levels
A running-level string consisting of numbers 0 to 7, for example:
-- Level 35 indicates the running level 3 and 5.
To stop the NFS service at running levels 3, 4, and 5, run the following command: chkconfig -- level 345 NFS off.
-- Add name
This option 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.
-- Del name
Delete a service and delete the connection from/etc/rc [0-6]. D.
-- List name
List. If the name is specified, only the specified service name is displayed. Otherwise, the status of all services at different running levels is listed.
Running 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 and can be annotated across rows.
For example, random. init contains three rows:
# Chkconfig: 2345 20 80
# Description: saves and restores system entropy pool
# Higher quality random number generation.
It indicates that the random script should be started at runtime Level 2, 3, 4, 5. The start priority is 20, and the Stop priority is 80.
Now, let's take a look at the script under/etc/rc. d/init. d In your directory.
Set auto-start service: chkconfig -- level 345 NFS on
Example
#! /Bin/bash
#
# Httpd STARTUP script for the Apache HTTP Server
#
# Chkconfig: 235 98
# Description: Apache is a World Wide Web server.
# It is used to serve HTML files and CGI.
# Processname: httpd
# Source function library.
./Etc/rc. d/init. d/functions
# See how we were called.
Case "$1" in
Start)
Echo "Starting Apache server ..."
/Usr/Apache/bin/apachectl start
;;
Stop)
Echo "Stopping Apache server ..."
/Usr/Apache/bin/apachectl stop
;;
Restart)
Echo "Restarting Apache server ..."
/Usr/Apache/bin/apachectl restart
;;
Status)
Statusproc/usr/Apache/bin/httpd
;;
*)
Echo "Usage: $0 {START | stop | restart | status }"
Exit 1
;;
Esac
Exit 0