- Linux Run Level
Linux starts running at one level, and these run levels are listed below:
0 System Stop
1 Single user system, no login required
2 Multi-user system but NFS not supported, command-line mode login
3 full multi-user mode, command-line mode login
4 Not used
5 X11 Graphics mode, graphical mode login
6 Rebooting the system
These can be seen in the/etc/initab file in 0-6 levels of comments, the code snippet is as follows:
# Default RunLevel. The runlevels used by RHS is:
# 0-halt (do not set Initdefault to this)
# 1-single User mode
# 2-multiuser, without NFS (the same as 3, if you don't have networking)
# 3-full Multiuser mode
# 4-unused
# 5-x11
# 6-reboot (do not set Initdefault to this)
1. Level 0
The system stops. Be careful not to set this level to the default mode, or the system will stop automatically after each boot and cannot be entered.
This runlevel is primarily used to close tasks, and the individual connection commands in the RC0.D directory are commands at this level. When closed, these commands are executed one by one. They will kill all processes, shut down virtual memory and swap files, unmount the file system, and swap partitions.
The shutdown operation is completely different under DOS and Linux. At the DOS prompt, you can safely turn off the power directly. However, it is not possible to do so under Linux. The Linux file system is mounted at startup and is uninstalled when it is closed. This difference is the premise of Linux's powerful capabilities.
Under Linux, to shut down, restart the computer, or by pressing the hotkey "Ctrl" + "Alt" + "Del" to restart by entering the command shutdown plus parameters.
2. Level 1
Single-user mode. This mode only permits one user to log on to all files in the RC1.D directory from the local computer to be connected to this runlevel. This runlevel is typically used for system management and maintenance, such as upgrading Linux systems, installing new software, and so on.
In this mode, only the administrator can enter and other users cannot log on. Because the file system is loaded at startup and the network is not loaded, you cannot log on through the network.
3. Level 2
Multi-user mode. Users can log on through the network. In cases where the network is not supported, the mode and mode 3 are phase, and all files in the RC2.D directory are connected to this level.
4. Level 3
Full multi-user mode. This is the default run mode, in which all network service programs run together. The files recorded by RC3.D are connected to this level.
5. Level 4
The RC4.D directory is connected to this level without using the pattern. This level is a user-defined runlevel that the user can define by their roots. If you want to run this level, you must put the connection file in the RC3.D directory, just like any other rc*.d directory, and indicate whether the process is started or terminated.
6. Level 5
Running X Window under Linux is the use of this level. Under this level, the named of the domain name server in addition to the Internet is different from level 3, the rest is the same.
7. Level 6
This is the runlevel for restarting the system. The RC6.D directory is connected to this level. Since it is a reboot that shuts down the current system, but does not turn off the power, the connection under this directory is basically the same as the connection at level 0 under RC0.D. The difference is that although they all execute the halt (Close) command, the parameters passed to the halt are different, so level 6 can reboot the system.
- Inittab file Explanation
Inittab file Format:
Label:runlevel:action:process
Label
The label of the 1~4 character, which can be a string of any character, representing the value entered. Some systems have a label of 2 characters. Some specific tags are commonly used, and the tags used in red Hat Linux are:
ID is used to define the default level of Init run
Si is the process of system initialization
ln where n is from 1~6, indicating the level of runlevel that the process can use
UD is the upgrade process
CA indicates a process that is running when Ctrl+alt+del is pressed
PF refers to a process that runs when a UPS indicates a power outage
PR is the process that the UPS needs to run before the system actually shuts down the signal when it sends a power recovery
X is the process that needs to run when the system is transferred to the X terminal
RunLevel
Defines the runlevel at which the record entry is invoked, which can consist of one or more runlevel, or empty, which represents the RunLevel 0~6.
When the request init changes the runlevel, those processes that do not include the new runlevel in the Rstate field will receive a sigterm warning signal and are eventually killed; only the commands that are started by a, B, C are not true runlevel????
Action
Defines the state in which the process should run, that is, how processes specified by the process field are handled.
Code:
Boot only when the system starts, Init processes such record entries, initiates the process, ignores runlevel, and does not wait for the processing to finish to process the next record entry. When such a process terminates, the system does not restart it.
Bootwait The system starts, when the first time from single-user mode into multi-user mode to process such a record entry, Init initiates such a process, and waits for its processing to end, and then the next record item processing, when such a process terminates, the system does not restart it.
Ctrlaltdel when the Ctrl+alt+del three keys are pressed at the same time, the SIGINT signal is sent to Init. Ignore RunLevel
INITDEFAULT Specifies a default runlevel, which is scanned only when Init is first called, if RunLevel
The RunLevel field specifies multiple RunLevel, where the largest number is the default RunLevel, if the
The field is empty, Init considers the field to be 0123456, and then enters Level 6, so that it is caught in a loop that, if the Inittab file does not contain a record entry that contains Initdefault, asks the user to specify an initial run level for it when the system starts
Kbrequest run when Init receives a signal from the keyboard. Here the keyboard combination is required to conform to keyboardsigral (see/usr/share/doc/kbd-* for a document on the keyboard combination)
Off starts the process specified by the processes field if the specified process is running, Init sends it a SIGTERM warning signal, waits 5 seconds before sending it a signal sigkill forcing it to end, ignoring this if such a process does not exist.
Once does not wait for processing to finish before processing the next record entry. When such a process terminates, it is no longer restarted, and if such a process is still running when the new RunLevel is entered, Init does not restart it.
OnDemand function with Respawn when the system specifies a specific runlevel a, B, C run
Powerfail run when Init receives a SIGPWR (power off) signal
Powerokwait run when a sigpwd signal is received and the power state in the/etc/file contains OK
Powerwait when the sigpwd signal is received, and Init waits for the process to run at the end
Respawn no matter when the process is aborted, Init restarts the processes, and Init continues to scan the subsequent process in Inittab without waiting for the start end. If the process already exists, nothing is done.
Sysinit runs before the boot or bootwait process is run, the specified process executes before the access console, such that the record entry is only used for initialization of certain devices, in order for Init to ask the user questions about the runlevel on such a device, Init needs to wait until the process finishes running before continuing.
Wait starts the process specified by the Process field and waits until the processing is finished to process the next record entry in Inittab.
The process field contains the processes that Init executes, and the process takes the same format as running the process at the command line, so the process field begins with the name of the session, followed by the runtime, followed by the arguments that are passed to the process at run time. For example,/sbin/shutdown-t3-rnow, the process is executed when Ctrl+alt+del is pressed, and can be entered directly at the command line to reboot the system.
Processes in the process field can be any waiting process, executable script, or program.
Also: At any time, a new record entry can be added to the file Inittab, the level q/q does not change the current runlevel, the Inittab file is re-examined, and the INIT process can be re-read and processed by command init Q or init Q immediately to process the file Inittab.
Records for special purposes
Learn the example file carefully and learn how to apply the syntax format for Inittab. Most of the content of the file can be ignored, because more than half of the content is commented, and some of the remaining file content is mainly used to implement some special functions:
The value of the ID indicates that the default RunLevel is 3.
The value of UD wakes up the/sbin/update process, which, in order to preserve the integrity of the disk, empties the entire I/O buffer before I/O to the disk.
The values of PF, PR, and CA are only called by specific interrupts.
If the system is a dedicated x terminal, only the input value of x is required.
Getty process to provide services for virtual end-devices, such as:
3:2345:respawn:/sbin/mingetty Tty3
The value of the Label field is 3,3 is the number suffix of the device tty3, Tty3 is associated with the corresponding process, the runlevel that the Getty process can start is 2, 3, 4, and 5, and when the process terminates, Init restarts it immediately. The path name of the startup process is/sbin/mingetty, which is the minimum version of Getty that implements the virtual terminal support, providing tty3 the process to start the virtual appliance.
Si::sysinit:/etc/rc.d/rc.sysinit
This value tells the INIT program to run the/etc/rc.d/rc.sysinit script file to initialize the system, which, like all startup scripts, is just an executable file containing the Linux shell command, and note that the string entered must include the full path of the script. Different versions of Linux store this script location is not the same, but do not deliberately to remember these locations, just view the/etc/inittab file, the file contains the exact location of the startup script file.
Redhat 9 Default Initab file contents:
Default RunLevel
0-halt (do not set Initdefault to this)
1-single User mode
2-multiuser, without NFS (the same as 3, if you don't have a neworking)
3-full multiuser mode
4-unused
5-x11
6-reboot Halt (do not set Initdefault to this)
Id:5:initdefault:
The default init process is called with a run level of 5 (cannot be set to 0 and 6, so the system will not start)
Si::sysinit:/etc/rc.d/rc.sysinit
Si is the system initialization process, the INIT program runs the/etc/rc.d/rc.sysinit script file to initialize the system, and Init waits for the script to run until the end of the next process.
L0:0:WAIT:/ETC/RC.D/RC 0
L1:1:WAIT:/ETC/RC.D/RC 1
L2:2:WAIT:/ETC/RC.D/RC 2
L3:3:WAIT:/ETC/RC.D/RC 3
L4:4:WAIT:/ETC/RC.D/RC 4
L5:5:WAIT:/ETC/RC.D/RC 5
L6:6:WAIT:/ETC/RC.D/RC 6
Run the script/ETC/RC.D/RC for the appropriate runlevel, and pass in the appropriate run-level parameters 0~6
Ca::ctrlaltdel:/sbin/shutdown-t3-r now
Execute shutdown-t3-r Now when Ctrl+alt+del is pressed
PF::p owerfail:/sbin/shutdown-f-H +2
When the UPS power is prompted to power off, he will also provide a few minutes of power supply, when the system executes Shutdown-f-H +2 (-F for restarting the file system to skip the check)
Pr:12345:powerwait:/shutdown-c
Indicates that the executing shutdown command was canceled when the power supply was restored.
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty Tty2
3:2345:respawn:/sbin/mingetty Tty3
4:2345:respawn:/sbin/mingetty Tty4
5:2345:respawn:/sbin/mingetty Tty5
6:2345:respawn:/sbin/mingetty Tty6
2345 run level, run the corresponding terminal program
X:5:respawn:/etc/x11/prefdm-nodaemon
If it's a program to run in X terminal mode
- Linux Auto-start script
Linux boot will run an INIT program, first read the Inittab configuration file, determine the operating mode of the system, in the process of reading Inittab, execute the RC0.D~RC6.D directory run level script, and then execute/etc/rc.local;
Since the execution of the run level script under the RC0.D~RC6.D directory is performed by the/ETC/RC.D/RC script, let's take a look at this script.
/ETC/RC.D/RC Script Description:
#! /bin/bash
#
# RC This file was responsible for starting/stopping
# Services when the runlevel changes.
#
# Original Author:
# Miquel van Smoorenburg, <[email protected]>
#
# Check a file to be a correct runlevel script
Check_runlevel ()
{
# Check If the file exists at all.
#判断文件存在, and executable
[-X $ "] | | Return 1
# Reject backup files and files generated by RPM.
#拒绝备份文件格式的文件名
Case "$" in
*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
Return 1
;;
Esac
return 0
}
# now find out what the previous runlevel is.
# Get to run level, such as/ETC/RC.D/RC in Inittab 0~6 specified run level, here the parameter is inittab the command passed the argument 0~6 number
Argv1= "$"
Set '/sbin/runlevel '
Runlevel=$2
Previous=$1
Export RunLevel Previous
#设置变量, current run level, previous run level
#如果没有上一个运行级别, then previous=n.
#载入/etc/init.d/functions script, load the function definitions that need to be used
. /etc/init.d/functions
# See if we want to BES in user confirmation mode
If ["$previous" = "N"]; Then
if [-f/var/run/confirm] \
|| Grep-i Confirm/proc/cmdline >/dev/null; Then
Rm-f/var/run/confirm
Confirm=yes
Export CONFIRM
echo $ "Entering Interactive startup"
Else
echo $ "Entering non-interactive startup"
Fi
Fi
# Get first argument. Set new runlevel to this argument.
# If $ARGV1 is not an empty string, give RunLevel the run level specified for ARGV1
[-N "$argv 1"] && runlevel= "$ARGV 1"
# Is there a RC directory for this new runlevel?
# if there is no corresponding level of RC ($runlevel). d directory, then the program exits
[-D/ETC/RC$RUNLEVEL.D] | | Exit 0
# First, run the KILL scripts.
#在相应运行级别的rc ($runlevel). d directory, traverse files beginning with k, and remove them individually
#经过一系列判断之后, execute the script, pass in the stop parameter
For I in/etc/rc$runlevel.d/k*; Do
Check_runlevel "$i" | | Continue
#判断文件存在可
# Check If the subsystem is already up.
Subsys=${i#/etc/rc$runlevel.d/k??}
[-f/var/lock/subsys/$subsys-o-f/var/lock/subsys/$subsys. init] \
|| Continue
# Bring the subsystem down.
# Find if the file contains Killproc and action words,-Q Select Yes to not show input, only return status
# Execute script, incoming stop parameter
If Egrep-q "(Killproc |action)" $i; Then
$i Stop
Else
Action $ "Stopping $subsys:" $i stop
Fi
Done
# now run the START scripts.
#获取/ETC/RC ($runlevel). D directory, start with S file
#对每个文件经过check之后, execute the script, pass in the start parameter
For I in/etc/rc$runlevel.d/s*; Do
Check_runlevel "$i" | | Continue
# Check If the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/s??}
[-f/var/lock/subsys/$subsys-o-f/var/lock/subsys/$subsys. init] \
&& continue
# If we ' re in confirmation mode, get user confirmation
If [-N "$CONFIRM"]; Then
Confirm $subsys
Case $? Inch
0):;;
2) confirm=;
*) continue;;
Esac
Fi
# Bring the subsystem up.
If ["$subsys" = "halt"-o "$subsys" = "reboot"]; Then
Export Lc_all=c
EXEC $i Start
Fi
If Egrep-q "(Daemon |action |success |failure)" $i 2>/dev/null \
|| ["$subsys" = "single"-O "$subsys" = "local"]; Then
$i Start
Else
Action $ "Starting $subsys:" $i start
Fi
Done
Analysis of the above RC script, it is not difficult to understand why the startup script in the RC0~6.D directory to be named after the S, stop the script in the RC0~6.D directory to be named after the K.
I wrote an Oracle auto-start article, starting with the script starts with S, the closed script is beginning with K, so if you write an auto-start program, or automatically close the program's script, you need to meet the naming requirements to be executed correctly.
- User logon Start
The user will run/etc/profile after logging in, and at the end of the profile, there is a code that reads:
For I in/etc/profile.d/*.sh; Do
If [-R "$i"]; Then
. $i
Fi
Done
This code will traverse the/ETC/PROFILE.D directory and execute any script with *.sh (end of SH) file name;
After executing/etc/profile, then execute the user $home/.bash_profile script
If/ETC/PROFILE.D in the directory to create a *.sh file name of the script, every time the user login will start, and some programs only need to start once, do not need to start every time, such as database, Web services, application server and so on.
Depending on their situation, the user can decide to start the program at logon or host startup is the startup program.
Because the way the program runs can be constantly changing, some users will continue to copy the program from one directory to another directory, the name will be constantly modified, in fact, the user only need to store the application in one place, in the configuration directory with ln-s to establish a Ruan connection can be;
- Start automatically when exiting
When you log out, the $home/.bash_logout file is automatically executed, and if you add some program execution commands to the command, those commands will be started automatically when the user exits the login. For example, at the end of the file, add: Agentctl stop, which indicates that the command will be executed when exiting. Stop the Apache service.
- Some shell startup files
$HOME/.bash_history Record of user action commands
Execute this script $HOME/.bash_logout when the user exits the login
$HOME/.bash_profile user logs on, executes after/etc/profile executes. bash_profile (Unix under. Profile,linux under Unix)
Automatic execution $HOME/.BASHRC Shell Login
/etc/profile scripts that must be executed every time a user logs on
/etc/fstab system requires mount disk operation information for each boot
Linux autostart Shell and init overview (Fedora use SYSTEMMD now!!!)