Notes and records of linux boot script

Source: Internet
Author: User
Tags stop script

In linux, the Automatic startup script involves the following knowledge and methods:
1. Basic knowledge points
1) The redhat Startup Mode and execution sequence are as follows:

Load the kernel

Run the init program
/Etc/rc. d/rc. sysinit # The first script executed by init
/Etc/rc. d/rc $ RUNLEVEL # $ RUNLEVEL is the default running mode.
/Etc/rc. d/rc. local # after the corresponding level of service is started, execute the file (in fact, you can also write the command to be executed to this file)
/Sbin/mingetty # Wait for User Login

In Redhat,/etc/rc. d/rc. sysinit mainly performs the same initialization in each running mode, including:

Incoming keymap and system font

Start swapping

Set Host Name

Set the NIS domain name

Check (fsck) and mount the File System

Open quota

Mount sound card module

Set system clock

And so on.
/Etc/rc. d/rc executes scripts in the corresponding directory according to the running mode specified by its parameters (the running level, which can be set in the inittab file. All parameters starting with Kxx are called with the stop parameter. All parameters starting with Sxx are called with the start parameter. The call sequence is from xx to xx. (Xx indicates the Starting sequence.) For example, assume that the default running mode is 3,/etc/rc. d/rc will call/etc/rc as described above. d/rc3.d.

It is worth mentioning that the operating modes 2, 3, and 5 in Redhat put/etc/rc. d/rc. local is the last in the initialization script, so you can add some commands that need to be executed before logging on to this file.
Init is waiting for/etc/rc. d/rc after execution (because/etc/inittab/etc/rc. d/rc's action is wait). It will run/sbin/mingetty on each specified virtual terminal and wait for the user to log on.

So far, the LINUX Startup has ended.

2. 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. Common examples are 2, 3, 4, and 5 of multiple users. Many people know that 5 is the level of 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. When the -- level option is specified, you can select a specific running level.

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
Create.
-- 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
2. instance introduction:
1. the apache service is installed in linux (by downloading binary files for economic compilation and installation, rather than rpm packages) and the apache service startup command:/server/apache/bin/apachectl start. Run the apache service under runlevel 3.
The command is as follows:
1) touch/etc/rc. d/init. d/apache
Vi/etc/rc. d/init. d/apache
Chown-R root/etc/rc. d/init. d/apache
Chmod 700/etc/rc. d/init. d/apache
Ln-s/etc/rc. d/init. d/apache/etc/rc. d/rc3.d/S60apache # S is short for start, meaning start, K is short for kill, and stands for close. 60 digits
Indicates the order of startup. (For the iptv system, many services can be started normally only when the database is started. You can adjust the script startup sequence with this number ))
Apache content:
#! /Bin/bash
# Start httpd service
/Server/apache/bin/apachectl start

Now the apache service can be automatically started at random under runtime Level 3. (You can adjust the startup service with chkconfig ).
The following startup sequence is for reference only because the variables are defined differently.
Start the script in the Redhat Redflag centos fc linux System
Successively:
Step 1: Start vmlinuz through/boot/vm
Step 2: init/etc/inittab
Step 3: Start the script and open the terminal
Rc. sysinit
Rc. d (script in it)
Rc. local
Step 4: Start the login logon Interface
Step 5: Execute the sh script sequence During User Logon: It will be fully executed during each logon.
/Etc/profile. d/file
/Etc/profile
/Etc/bashrc
/Root/. bashrc
/Root/. bash_profile
Editor's note:
The Ntsysv command can also enable different services based on different running levels. However, you must note that the ntsysv command is used to manage Service Startup in a graphical way by default, however, in this case, the service is only effective for the current running level! Therefore, it is recommended that chkconfig be used for service management.

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.