Linux Automatic startup shell and init Overview

Source: Internet
Author: User
Tags linux shell commands sigint signal stop script egrep
  1. Linux running level
    After Linux is started, it runs at one level. The following lists the running levels:
    0 system stopped
    1 single user system, no login required
    2 multi-user system, but does not support NFS, command line mode Login
    3 full multi-user mode, command line mode Login
    4 unused
    5x11 graphic mode, graphical mode Login
    6. restart the system.
    The comments of level 0-6 can be seen in the/etc/initab file. The code segment is as follows:
    # Default runlevel. The runlevels used by RHS are:
    #0-halt (do not set initdefault to this)
    #1-Single User Mode
    #2-multiuser, without NFS (the same as 3, if you do not have networking)
    #3-full multiuser Mode
    #4-unused
    #5-X11
    #6-Reboot (do not set initdefault to this)
    1. Level 0
    The system stops. Do not set this level to the default mode. Otherwise, the system stops automatically after each startup and cannot enter.
    This running level is mainly used to close the task. All the connection commands in the rc0.d directory are at this level. When the command is disabled, these commands are executed one by one. They will kill all processes, shut down virtual memory and swap files, unload file systems, and swap partitions.
    The shutdown operation is different in DOS and Linux. At the DOS prompt, you can safely turn off the power. However, you cannot do this in Linux. The Linux File System is loaded at startup and uninstalled when it is disabled. This difference is a prerequisite for the powerful functions of Linux.
    In Linux, you must enter the command shutdown and add parameters to shut down or restart the computer, or press the hot key CTRL + ALT + DEL to restart the computer.
    2. Level 1
    Single User mode. In this mode, only one user can log on to all files in the rc1.d directory from a local computer and connect them to the running level. This running level is generally used for system management and maintenance, such as upgrading the Linux system and installing new software.
    In this mode, only the administrator can log on, but other users cannot log on. Because the file system is loaded but the network is not loaded at startup, you cannot log on through the network.
    3. Level 2
    Multi-user mode. You can log on through the network. If the network is not supported, this mode is connected to Mode 3. All files in the rc2.d directory are connected to this level.
    4. Level 3
    Full multi-user mode. This is the default running mode, in which all network service programs run together. Files in the rc3.d directory are connected to this level.
    5. Level 4
    The mode is not used. The rc4.d directory is connected to this level. This level is a custom running level, which can be defined by the root user. If you want to run this level, you must put the connection file in the rc3.d directory, like the files in other rc *. d directories, and specify whether to start or stop the process.
    6. Level 5
    This level is used to run X Window in Linux. At this level, except for the named and Level 3 domain name servers of the Internet, the others are the same.
    7. Level 6
    This is the running level of the restart system. The rc6.d directory is connected to this level. Since the system is restarted, that is, the current system is shut down, but the power is not turned off, the connections in this directory are basically the same as those in rc0.d with a level of 0. The difference is that although they all execute the halt (close) command, the parameters passed to halt are different, so level 6 can restart the system.
  2. Description of the inittab File
    Inittab file format:
    Label: runlevel: Action: Process
    Label:
    1 ~ A 4-character tag. It can be a string consisting of any character and represents the input value. Some system labels contain 2 characters. Some specific labels are commonly used. The labels used in Red Hat Linux are:
    ID is used to define the default init running level
    Si is the process of system initialization.
    In LN, N ranges from 1 ~ 6. indicates the runlevel level that can be used by the process.
    UD is the upgrading process
    CA indicates that the process is running when CTRL + ALT + DEL is pressed.
    PF indicates the process that runs when the ups instance is powered off.
    PR is the process to run when the ups sends a signal of power recovery before the system is actually shut down.
    X is the process to run when the system is transferred to the X terminal.
    Runlevel:
    Defines the running level when this record item is called. It can be composed of one or more running levels, or it can be empty. If it is null, it indicates the running level 0 ~ 6.
    When the init is requested to change the running level, processes that do not include the new running level in the rstate field will receive the sigterm warning signal and finally be killed; only commands started by A, B, and C (A, B, and C are not really running )????

    Action:
    Defines the status of the process, that is, how to process the process specified by the process field.
    Code:
    Boot only processes such records when the system starts. It starts the corresponding process and ignores runlevel. It does not process the next record item until the processing is complete. When such a process is terminated, the system does not restart it.
    After the bootwait system is started, it processes such records when it enters the multi-user mode for the first time from single-user mode. init starts such a process and waits until its processing ends, then the next record item is processed. When such a process is terminated, the system does not restart it.
    Ctrlaltdel run when CTRL + ALT + DEL are pressed simultaneously, and the SIGINT signal is sent to init. Ignore runlevel
    Initdefault specifies a default running level, which is scanned only when init is called at the beginning. If runlevel
    The field specifies multiple running levels. The maximum number is the default running level.
    The field is empty, and init considers the field to be 0123456, so it enters Level 6, so it falls into a loop. If the inittab file does not contain the initdefault record item, when the system starts, the user is requested to specify an initial running level for it.
    Kbrequest runs when init receives a signal from the keyboard. The keyboard combination is required to comply with keyboardsigral (see/usr/share/doc/KBD-* Documents on the keyboard combination)
    Off START process field specified process if the specified process is running, init sends it a sigterm warning signal, wait 5 seconds before it sends a signal sigkill to force it to end, if such a process does not exist, ignore this item.
    Once, the next record item will be processed before the processing is completed. When such a process is terminated, it will not be restarted. When it enters the new running level, if such a process is still running, init will not restart it.
    The OnDemand function is the same as the respawn function when the system specifies a specific running level A, B, and C.
    Powerfail runs when init receives the sigpwr (power-off) Signal
    Powerokwait runs when the sigpwd signal is received and the power status in the/etc/file contains OK
    Powerwait runs when it receives the sigpwd signal and init waits for the process to end
    The respawn restarts the process whenever the process is aborted, And the init continues to scan the subsequent process in the inittab until the start ends. If the process already exists, do nothing.
    Sysinit runs before running the boot or bootwait process. The specified process is executed before accessing the console. Such record items are only used for initialization of certain devices, the purpose is to enable init to ask the user about the running level on such a device. init needs to wait until the process finishes running.
    Wait starts the process specified by the Process Field and waits until the processing is completed to process the next record in the inittab.

    The process field contains the process executed by init. The process adopts the same format as running the process on the command line. Therefore, the Process field starts with the name of the process, it is followed by the parameter to be passed to the process during running. For example,/sbin/shutdown-T3-R now, the process is executed when CTRL + ALT + DEL is pressed. You can also enter it in the command line to restart the system.
    The process in the process field can be any waiting process, executable script, or program.
    In addition, you can add new record items in the file inittab at any time. Level Q/Q does not change the current running level, and re-check the inittab file, you can run init Q or init Q to re-read the INIT process and process the inittab file.

    Special purpose records
    Take a closer look at the example file and learn how to apply the syntax format of inittab. Most of the content of this file can be ignored, because more than half of the content is annotated, and the remaining content is mainly used to implement some special functions:
    The value of ID indicates that the default runlevel is 3.
    The UD value can wake up the/sbin/update process. To ensure the integrity of the disk, the whole I/O buffer is cleared before I/O operations are performed on the disk.
    The PF, PR, and Ca values are called only for specific interruptions.
    If the system is a dedicated X terminal, only the input value of X is required.
    The getty process provides services for virtual terminal devices. For example:
    3: 2345: respawn:/sbin/mingetty tty3
    The value of the tag field is 3, which is the digital 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, when the process is terminated, init immediately restarts it. The name of the startup process is/sbin/mingetty, which is the smallest version of Getty supported by the virtual terminal and provides the process for tty3 to start the virtual device.
    Si: sysinit:/etc/rc. d/rc. sysinit
    This value tells the INIT program to run/etc/rc. d/RC. the sysinit script file is used to initialize the system. The script file is similar to all started scripts. It is only an executable file containing Linux shell commands, note that the entered string must include the full path of the script. Different Versions of Linux store this script in different locations, but you don't have to remember these locations. You only need to view the/etc/inittab file, this file contains the exact location of the startup script file.

    RedHat 9 default initab file content:
    Default runlevel
    0-halt (do not set initdefault to this)
    1-Single User Mode
    2-multiuser, without NFS (the same as 3, if you do not have a neworking)
    3-full multiuser Mode
    4-unused
    5-X11
    6-Reboot halt (do not set initdefault to this)
    ID: 5: initdefault:
    // By default, the running level of the INIT process is 5 (it cannot be set to 0 or 6, so that the system cannot 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. init will continue to run the next process until the script stops running.
    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
    // For the corresponding running level, run the script/etc/rc. d/RC, and input the corresponding running level parameter 0 ~ 6
    CA: ctrlaltdel:/sbin/shutdown-T3-R now
    // Execute shutdown-T3-R now when pressing CTRL + ALT + DEL
    PF: powerfail:/sbin/shutdown-f-h + 2
    // When the UPS Power Supply prompts a power failure, it will also provide power supply for several minutes, in this case, the system executes shutdown-f-h + 2 (-F indicates that the file system check is skipped during restart)

    PR: 12345: powerwait:/shutdown-C
    // Cancel the shutdown command being executed when the power supply is 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/preofdm-nodaemon
    // The program to run in X terminal mode

  3. Linux Automatic startup script
    After Linux is started, it runs an INIT program. First, it reads the inittab configuration file and determines the operating mode of the system. When reading inittab, it runs rc0.d ~ Run scripts in the rc6.d directory, and then execute/etc/rc. Local;
    Because the rc0.d ~ The run-level script in the rc6.d directory is executed 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 is responsible for starting/stopping
    # Services when the runlevel changes.
    #
    # Original Author:
    # Miquel van smoorenburg, <miquels@drinkel.nl.mugnet.org>
    #
    # Check a file to be a correct runlevel script
    Check_runlevel ()
    {
    # Check if the file exists at all.
    # Determine whether a file exists and can be executed
    [-X "$1"] | return 1

    # Reject Backup files and files generated by rpm.
    # Reject the file name in the backup file format
    Case "$1" in
    *. Rpmsave | *. rpmorig | *. rpmnew | *~ | *. Orig)
    Return 1
    ;;
    Esac
    Return 0
    }
    # Now find out what the current and what the previous runlevel are.
    # Obtain the running level, such as/etc/rc. d/RC 0 ~ in inittab ~ 6. The running level is specified. The $1 parameter here is the parameter 0 ~ passed by the command in inittab ~ 6 digits
    Argv1 = "$1"
    Set '/sbin/runlevel'
    Runlevel = $2
    Previous = $1
    Export runlevel previous
    # Set the variable, current run level, last run level
    # If there is no previous running level, previous = N
    # Load the/etc/init. d/functions script and load the function definitions needed
    ./Etc/init. d/functions
    # See if we want to be 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, assign runlevel to the running level specified by argv1
    [-N "$ argv1"] & runlevel = "$ argv1"
    # Is there an RC directory for this new runlevel?
    # If there is no directory for RC ($ runlevel). d at the corresponding level, the program exits.
    [-D/etc/rc $ runlevel. d] | exit 0

    # First, run the kill scripts.
    # In the RC ($ runlevel). d directory of the corresponding running level, traverse the files starting with K and retrieve them one by one
    # After a series of judgments, execute the script and pass in the Stop Parameter
    For I in/etc/rc $ runlevel. d/K *; do
    Check_runlevel "$ I" | continue
    # Determining whether a file exists
    # 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.
    # Check whether the file contains killproc and action words. If you select "-Q", the input is not displayed and only the status is returned.
    # Run the script and pass in the Stop Parameter
    If egrep-Q "(killproc | action)" $ I; then
    $ I stop
    Else
    Action $ "Stopping $ subsys:" $ I stop
    Fi
    Done
    # Now run the start scripts.
    # Get the file starting with S in the/etc/rc ($ runlevel). d directory
    # After checking each file, execute the script and 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 $? In
    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
    It is not difficult to understand why the above RC Script is used in RC0 ~ 6. The name starts with "S" in the d directory, and the Stop script must start with "RC0 ~ 6. The Directory D should start with K.

    I wrote an article about Oracle Auto Start. All the startup scripts start with S, and all the closed scripts start with K. Therefore, if you are writing an Auto Start program, or, when the script of the program is automatically closed, it must meet the naming requirements to be correctly executed.

  4. User Login start
    After the user logs on, the/etc/profile will run. When the user executes the profile, there is a piece of code, as shown below:
    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 all scripts with the *. Sh (ended with SH) file name;
    Run the/etc/profile script, and then run the $ home/. bash_profile script.
    If/etc/profile. d. Create a directory *. the sh file name script is started every time a user logs on. Some applications only need to be started once and do not need to be started every time a user logs on, such as databases, web services, and application servers.

    You can choose whether to start the program at login or host startup based on your own situation.

    Because the program running method may change constantly, some users will constantly copy the program from one directory to another, and the name will be constantly modified, in fact, you only need to store the application in one place and use ln-s to establish a persistent connection in the configuration directory;

  5. Auto Start upon exit
    $ Home /. bash_logout file. If you add some program execution commands to the command, these commands will be automatically started when the user logs out. for example, add "agentctl stop" at the end of the file to stop the apache service.
  6. Some shell startup files
    $ Home/. bash_history records user operation commands
    $ Home/. bash_logout the script is executed when the user logs out.
    $ Home/. bash_profile: log on to the user. After/etc/profile is executed, run. bash_profile (. Profile in UNIX and UNIX in Linux)
    $ Home/. bashrc automatically executed during shell Logon
    /Etc/profile the script that must be executed when each user logs on.
    The/etc/fstab system needs to mount disk operation information each time it is started.
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.