Linux Boot program details

Let's assume that you are familiar with the boot process of other operating systems , Understand the hardware self-test boot steps , Just from Linux Boot loader for the operating system ( For personal computers, it's usually LILO) Start , Introduce Linux Boot steps .

Loading kernel
LILO After starting , If you choose Linux As an operating system ready to boot , The first thing to load is the kernel . Remember that there is no operating system in the computer memory at this time ,PC( Because of their natural design flaws ) There is no way to access all the memory on the machine . therefore , The kernel must be fully loaded into the available RAM Within the first megabyte of . To achieve this goal , The kernel is compressed . The header of this file contains the necessary code , First set up CPU Enter safe mode ( To remove the memory limit ), Then decompress the rest of the kernel .

Execute kernel
After the kernel is decompressed in memory , You can start running . At this time, the kernel only knows its built-in functions , That is to say, the kernel part compiled as module cannot be used yet . The most basic thing is , The kernel must have enough code to set up its own virtual memory subsystem and root file system ( It's usually ext2 file system ). Once the kernel is up and running , The detection of the hardware will determine which device drivers need to be initialized . So let's start here , The kernel can mount the root file system ( This process is similar to Windows Identify and access C The process of the disk ). After the kernel mounts the root file system , It's called a run init The program .

Be careful : Here we deliberately omit Linux Many details of kernel startup , These details are of interest only to kernel developers . If you're curious , You can visit At the address "Kernel Hackers Guide".
init process
init Process is the first non kernel process to be started , So its process number PID The value is always 1.init Read its configuration file /etc/inittab, Determine the run level that needs to be started (Runlevel). Basically , The operational level defines the behavior of the entire system , Every level ( By 0 To 6 The integer representation of ) Meet a specific purpose . If you define initdefault Level , This value is selected directly , Otherwise, the user needs to enter a value representing the running level .
After entering the number representing the run level ,init according to /etc/inittab The definition in the file executes a command script program . The default run level depends on the choice of the login program during the installation phase : It's using text-based , Or do you use it based on X-Window Login program for .
rc Command scripts we already know , When the run level changes , Will be made by /etc/inittab The file defines which command script program to run . These command scripts are responsible for starting or stopping various services specific to the run level . Because of the large number of services that need to be managed , So you need to use rc Command script program . among , The main one is /etc/rc.d/rc, It is responsible for calling the corresponding command scripts for each run level in the correct order . We can imagine , Such a command script can easily become uncontrollable ! In order to prevent such incidents , A well-designed solution is needed .

For every run level , stay /etc/rc.d There is a corresponding subdirectory in each subdirectory . The way to name the subdirectories of these runlevels is rcX.d, Among them X It's the number representing the operating level . for instance , Operation level 3 All of the command scripts are stored in /etc/rc.d/rc3.d Subdirectory . In subdirectories at each run level , It's all built up to /etc/rc.d/init.d Symbolic links to command scripts in subdirectories , however , These symbolic links do not use command scripts in /etc/rc.d/init.d The original name in the subdirectory . If a command script is used to start a service , The name of the symbolic link is in letters S Lead ; If a command script is used to shut down a service , The name of the symbolic link is in letters K Lead .
In many cases , The order in which these command scripts are executed is very important . If the network interface is not configured first , There's no way to use DNS The service resolves the host name ! In order to arrange their execution order , In the letters S perhaps K Followed by a two digit number , The small ones are executed before the large ones . such as :/etc/rc.d/rc3.d/S50inet Will be in /etc/rc.d/rc3.d/S55named Before execution (S50inet Configure network settings ,55named start-up DNS The server ). Store in /etc/rc.d/init.d In subdirectories 、 The command script program on the symbolic link is a real doer , They complete the operation process of starting or stopping various services . When /etc/rc.d/rc When running through each specific run level subdirectory , It will call each command script program to execute according to the numerical order . It runs first with letters K The first command script program , And then run it with letters S The first command script program . Yes, with letters K For the leading command script program , It will deliver Stop Parameters ; Similarly with letters S For the leading command script program , It will deliver Start Parameters . Write your own rc The command script is maintaining Linux In the days when the system works , There must be a need for the system administrator to boot or shut down the command foot
The revision of this article .

There are two ways to do this :
● If the changes only work at boot time , And if it doesn't change much , Consider simply editing /etc/rc.d/rc.local Script . The command script is executed at the last step of the boot process .
● If the changes are more detailed , Or ask to shut down the process so that it explicitly stops running , You need to in /etc/rc.d/init.d Add a command script program to the subdirectory . This command script must be acceptable Start and Stop Parameters and complete the corresponding operation .
The first method , edit /etc/rc.d/rc.local Script , Of course, the simpler of the two methods . If you want to add content to this command script , Just open it with your favorite editor program , You can attach the command you want to execute to the end of the file . It's really convenient for a line or two of changes .

If you really need to use a command script , The second method has to be chosen . Write a rc The process of command scripting is not as difficult as you think . Here's an example , See how it works ( By the way , You can use our example as a model , Modify and add according to your own needs ).
Suppose you're going to 60 Minutes to call a special program to pop up a message , Remind yourself that you need to take a break from the front of the keyboard , The command script program will include the following parts :
● A description of the function of this command script program ( So you don't forget it in a year );
● Verify that the command script exists before trying to run it ;
● Accept start and stop Parameters and perform the required actions .

After the parameters are given , We can script the command . This program is very simple , You can write it yourself , I won't give it here .
After writing the new command script program , Add the necessary symbolic links from the relevant runlevel subdirectories , To control the start or stop of the command script . In my impression , Just want it at the run level 3 Or run level 5 Start in , The reason is that I think only these two operation levels are the place of daily work . Last , I hope this command script program will enter the run level 6( Restart ) When it's closed .

Activate or disable service items sometimes , There is no need for a specific service to be started at boot time . If you are considering using Linux Replace Windows NT File and print servers for , Even more so . We already know , Change the name of the symbolic link in a specific runlevel subdirectory , So that the service will not be started , If the first letter of its name is replaced by S Change it to K. Once you've mastered the command line and symbolic links , You will find that this is the fastest way to activate or disable services .

While learning this method of renaming , You may feel like a graphical interface ksysv It's easier to master . Although it was originally designed to be used in KDE In the environment , But in Red HatLinux 7.2 The default installation is GNOME It's also working very well in the environment . If you want to start it , Simply open one xterm window , And enter the ksysv Just order . A window will appear on the screen , It lists all the parameters that can be modified , Online help is also available when needed . Warning : If you are learning the knowledge of this article on a real system , Use common sense more . When trying to modify the startup script , Keep in mind that the changes you make may cause your system to not work properly , And it can't be recovered by restarting . Don't experiment with new settings on a functioning system , Backup all the files you are going to modify . most important of all , Have a guide disk at hand in case of accidents .



stay Linux How to start the program automatically when the system starts up


     Core tips : The service of the system can start automatically when it is started , That's in linux System, if you want the program to start automatically at boot how to do ? We know that windows System “ Start ”-->“ All the procedures ”-->“ start-up ” Just put a shortcut inside , that Linux What about the system ?... The service of the system can start automatically when it is started , That's in linux System, if you want the program to start automatically at boot how to do ? We know that  windows System “ Start ”-->“ All the procedures ”-->“ start-up ” Just put a shortcut inside , that Linux What about the system ?
This is also a relatively simple problem , There are many ways to solve , Here are three ways . Because it's a brief introduction , So the details are not very detailed , Can pass man Look at the relevant manuals .
One 、/etc/rc.local
This is the easiest way , edit “/etc/rc.local”, Start the program shell Just enter the command ( To enter the full path of the command ), Be similar to windows Under the “ start-up ”.

Use command  vi  /etc/rc.local   

Then add the full path of the program to be executed on the last line of the file .

for example , Execute one... Every time you turn on the machine, This script is in /opt below , That can be in “/etc/rc.local” Add a line “/opt/./”, Or two lines “cd /opt” and “./”.
Two 、crontab( Be similar to windows Our mission planning service )
adopt crontab You can set the execution schedule of the program , For example, let the program in the daily 8 spot , Or every Monday 10 Click to execute once .
crontab -l Make a schedule ;
crontab -e Edit the schedule ;
crontab -d Delete schedule ;
“-l” Nothing to say , It's just a check ;
“-e” It's the editor , and vi No difference ( In fact, it is to use vi Edit a specific file );
“-d” Basically not , Because it deleted all the user's schedules , It's usually used “-e” The editor deleted the schedule line by line ;
How to edit it ?
crontab The format of the document is :M H D m d CMD.
One 6 A field , The last one CMD Is the program to be executed , Such as
M: minute (0-59)
H: Hours (0-23)
D: date (1-31)
m: month (1-12)
d: One day of the week (0-6,0 On behalf of the Sunday )
this 5 Time fields are separated by spaces , The value can be a number , You can also use multiple numbers separated by commas ( Or others ) , If you don't need to set , The default is “*”.
for example , Daily 8 spot 5 Points to perform, Namely “5 8 * * * /opt/./”.
Like and “ The boot program starts automatically ” Far away , Now back to the point . In fact, the above crontab The function of has the ability of starting automatically , You can write a monitoring script , Every time 5 Once per minute (*/5 * * * * ./, If the program is gone, restart it once .
3、 ... and 、 Register system services
Services provided by the operating system , Such as ssh,ftp wait , Start up is automatic , We can also improve our own programs in this way “ worth ”.


For example, I want to add an installed Service as a system service , You can execute the following commands :

chkconfig --add The service name          ( First , Add as system service , Be careful add There are two bars in the front )


chkconfig -leve Start level service name on         

( explain ,3 Level represents starting in command line mode ,5 The level represents starting in the graphical interface ,on Open for indication )


chkconfig -leve Start level service name off              

( explain ,off Indicates to turn off auto start )


for example :chkconfig -level 3 mysql on                     ( explain : Give Way mysql The service is in command line mode , Start with the system )

You can also use    chkconfig --add The service name     To delete system services


If you want to see which services are added as system services, you can use the command :

   ntsysv   perhaps chkconfig --list


If you want to see which programs are added as self starting , You can use commands   :

cat   /etc/rc.local    ( See which program paths are added to this file )



  Here's an example , How to put a shell Scripts are added as system services , And follow the system up :

You can see “/etc/rc.d/init.d” There are a lot of documents , Every file can see the content , In fact, they are all shell Script .
The start of system service is through “/etc/rc.d/init.d” The script file in . We can also write our own script here .
The content of the script file is also very simple , It's like this ( For example, it's called “hahad”):
. /etc/init.d/functions
start() {
        echo "Starting my process "
        cd /opt
stop() {
        echo "Stoped"
After writing the script file, it's not finished , Continue with the following steps :
chmod +x hahad                    # Add execution permission
chkconfig --add hahad             # hold hahad Add to system services list
chkconfig hahad on                 # Set up hahad The switch of (on/off)
chkconfig --list hahad               # You can see that you have registered hahad Service for


That's when all the work is done .