Detailed Linux boot start program and implementation of automatic start configuration

Source: Internet
Author: User
Tags safe mode

Maybe it's used to Windows, and sometimes we want to be able to load some apps automatically when Linux starts, and now we're going to do that.


Load Kernel

After Lilo starts, if you choose Linux as the boot-ready operating system, the first thing to be loaded is the kernel. Keep in mind that there are no operating systems in the computer's memory at this time, and PCs (because of their natural design flaws) have no way of accessing all the memory on the machine. Therefore, the kernel must be fully loaded into the first megabyte of available RAM. To achieve this, the kernel is compressed. The file's head contains the necessary code to set the CPU into Safe mode (to unlock the memory limit), and then unzip the remainder of the kernel.

Execute kernel

After the kernel is uncompressed in memory, it can start running. At this point the kernel knows only the various functions built into it itself, that is, the kernel part of the module that is compiled is not yet available. Most fundamentally, the kernel must have enough code to set up its own virtual memory subsystem and root file system (usually the Ext2 file system). Once the kernel starts running, testing the hardware determines which device drivers need to be initialized. From here, the kernel is able to mount the root file system (a process similar to the process that Windows recognizes and accesses the C disk). After the kernel mounts the root file system, a program called Init is started and run.

Note: Here we deliberately omit many of the details of Linux kernel startup, which are only interesting to kernel developers. If you are curious, you can access the "Kernel Hackers Guide" at the http://www.redhat.com:8080 address.

Init process

The init process is the first to run in a non-kernel process, so its process number PID value is always 1. Init reads its profile/etc/inittab to determine the run level (RunLevel) that needs to be started. Fundamentally, the run level prescribes the behavior of the entire system, each of which is represented by a 0 to 6 integer to satisfy a particular purpose. If the initdefault level is defined, the value is directly selected, or the user is required to enter a value that represents the run level.
After you enter a number that represents the run level, Init executes a command script based on the definition in the/etc/inittab file. The default run level depends on the selection of the logon program during the installation phase: using text-based, or using a X-window based login program.
RC Command Script we already know that when the run level changes, the/etc/inittab file defines which command script to run. These command scripts are responsible for starting or stopping the various services that are specific to the run level. Because of the large number of services that need to be managed, you need to use the RC command script. One of the most important is/ETC/RC.D/RC, which is responsible for calling the corresponding command script in the correct order for each run level. We can imagine that such a command script could easily become unmanageable! In order to prevent the occurrence of this kind of event, need to use carefully designed scheme.

For each run level, there is a corresponding subordinate directory in the/ETC/RC.D subdirectory. The naming method for the subordinate subdirectories of these run levels is RCX.D, where x is the number that represents the run level. For example, all command scripts that run level 3 are saved in the/ETC/RC.D/RC3.D subdirectory. In the subdirectories of each run level, there are symbolic links to command scripts in the/ETC/RC.D/INIT.D subdirectory, but these symbolic links do not use the original name of the command script in the/ETC/RC.D/INIT.D subdirectory. If the command script is used to start a service, the name of the symbolic link begins with the letter S, and if the command script is used to turn off a service, the name of the symbolic link begins with the letter K.
In many cases, the order in which these command scripts are executed is important. If you do not configure the network interface first, there is no way to resolve the host name using the DNS service! To arrange the order of their execution, a two-digit number follows the letter s or K followed by a small number in front of the larger number. For example:/etc/rc.d/rc3.d/s50inet will execute before/etc/rc.d/rc3.d/s55named (s50inet configure network settings, 55named start the DNS server). The command scripts on the signed links that are stored in the/ETC/RC.D/INIT.D subdirectory are real doers, and they complete the process of starting or stopping a variety of services. When/ETC/RC.D/RC runs through each specific run-level subdirectory, it invokes each of the command script programs sequentially, in the order of numbers. It runs a command script that starts with the letter K and then runs a command script that begins with the letter S. For a command script that starts with the letter K, the Stop argument is passed, and similarly to the command script that starts with the letter S, the start argument is passed. Write your own RC command scripts in the days when the Linux system is running, you will definitely encounter the need for system administrator to boot or shutdown command foot
This is the case for modification.

There are two ways to implement the purpose of the modification:
If you make changes that work only when booting the boot, and if the changes are small, consider simply editing the/etc/rc.d/rc.local script. This command script is executed at the last step of the boot process.
If the changes are meticulous, or if you also require the shutdown process to explicitly stop running, you will need to add a command script to the/ETC/RC.D/INIT.D subdirectory. This command script must be able to accept the start and stop arguments and complete the appropriate action.
The first way to edit the/etc/rc.d/rc.local script is, of course, simpler in both ways. If you want to add content to this command script, just use a favorite editor program to open it, and then attach the command you intend to execute to the end of the file. This is really handy for the one or two-line revisions.


If you do need to use a command script, you must select the second method. The process of writing an RC command script is not as difficult as it might seem. Let's give an example of how it's done (by the way, you can take our example as a template and modify and add it to your own needs).
Suppose you plan to call a special program every 60 minutes to pop up a message reminding yourself that you need to take a break from the front of the keyboard, and the command script will include the following sections:
A description of the function of the command script (so that it will not be forgotten after a year);
Verify that this command script does exist before attempting to run it;
Accept the start and stop parameters and perform the required actions.

After the argument is given, we can write the script for the command. This program is very simple, you can write their own, I do not give out here.
After you have written a new command script, you can control the start or stop of the command script by adding the necessary symbolic links from the associated run-level subdirectory. In my mind, I just want it to start at run Level 3 or run Level 5, because I think only these two levels of operation are the work place. Finally, it is hoped that this command script will be shut down when it enters run Level 6 (reboot).

Activating or banning a service project sometimes discovers that a particular service is not required to be started when booting. This is especially true if you are considering using Linux to replace Windows NT file and print servers. We already know that changing the name of a symbolic link in a specific run-level subdirectory allows the service to not be started, such as changing the first letter of its name from S to K. Once you have mastered the command line and symbolic links, you will find that this is the quickest way to activate or prohibit a service.

In learning the renaming method, you may find that the graphical interface ksysv easier to master. Although it was originally designed to be used in a KDE environment, it works well in the GNOME environment that is installed by default under Red Hatlinux 7.2. If you want to start it, simply open a xterm window and enter the KSYSV command. A window appears that lists all the parameters that can be modified, including online help when needed. Warning: If you are learning the knowledge of this article in a real-world system, use common sense a lot. When trying to modify the startup script, keep in mind that the modification may cause your system to not function properly and that it cannot be recovered by restarting. Do not experiment with new settings on a functioning system, and back up all of the files you are about to modify. The most important thing is to prepare a guide disk safekeeping at hand.



start automatically when a Linux system makes a program boot

System services in the boot generally can be automatically started, that in the Linux system if you want the program to start automatically when the boot how to do? We know that in the Windows system "start"--> "All Programs"--> "Start" inside a shortcut to do, that Linux system? ... System services in the boot generally can be automatically started, that in the Linux system if you want the program to start automatically when the boot how to do? We know that in the Windows system "start"--> "All Programs"--> "Start" inside a shortcut to do, that Linux system?

This is also a relatively simple problem, there are many ways to solve, here are three methods. Because it is a simple introduction, so the details are not very detailed, you can look at the relevant manual.

First,/etc/rc.local

This is the easiest way to edit "/etc/rc.local" to enter the shell command of the launcher (to enter the full path of the command), similar to "Startup" under Windows.

Use command vi/etc/rc.local

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

For example, to perform a haha.sh on each boot, this script is placed under/OPT, and you can add a line of "/opt/./haha.sh" to "/etc/rc.local" or two lines of "cd/opt" and "./haha.sh".

Second, crontab (similar to Windows Task scheduling service)

Crontab allows you to set a program's execution schedule, such as having the program run at 8 points per day, or 10 points per Monday.
CRONTAB-L lists the timetable;
CRONTAB-E editing schedule;
crontab-d Delete schedule;

"-L" nothing to say, is a view only;
"-E" is the editor, and VI no difference (in fact, is to use VI to edit a specific document);
"-D" basically does not need, because it has removed all of the user's schedule, is generally used "-e" editor to delete the schedule by line;

So how exactly do you edit it?

The format of the crontab file is: M H d M D CMD.
A 6 field, where the last cmd is the program to be executed, such as haha.sh.
M: Min (0-59)
H: Hours (0-23)
D: Date (1-31)
M: Month (1-12)
D: One day in one weeks (0-6, 0 for Sunday)

These 5 time fields are separated by a space, the value can be a number, or multiple digits (or other) separated by commas, and the default is "*" If you do not need to set it.

For example, a daily 8:5 execution haha.sh is "5 8 * * */opt/./haha.sh".

As if the "boot program automatically start" pull away, now back to the point. In fact, the crontab features described above have the ability to start automatically, you can write a monitoring script, every 5 minutes (*/5 * * * * * *./haha.sh), if the program is not on the restart once.

Third, registration system services

Operating system with its own services, such as SSH,FTP, and so on, the boot is automatically started, we can also use this way to develop their own procedures to improve the "price".



For example, I want to add a service that has already been installed as a system service, you can execute the following command:

Chkconfig--add Service Name (first, add as system services, note that add front has two bars)



Chkconfig-leve Start level service name on

(Note that the Level 3 representation is started in the command-line mode, the level 5 representation is started in the graphical interface, on means open)

Chkconfig-leve start level service name off

(description, off indicates shutdown from startup)



For example: Chkconfig-level 3 MySQL on (note: Let MySQL service in command line mode, start with System)

You can also remove system services by using the Chkconfig--add service name

******************************************************************************************

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

Ntsysv or Chkconfig--list

If you want to see which programs are added as a self starter, you can use the command:

Cat/etc/rc.local (see which program paths are added to this file)

*******************************************************************************************


Here's an example of how to add a shell script as a system service and follow the system launch:

You can see that there are a lot of files under "/etc/rc.d/init.d", and every file can see the content, in fact, are some shell scripts.
The boot of the system service is implemented through the script file in "/etc/rc.d/init.d". We can also write a script of our own here.
The contents of the script file are also simple, similar to this (for example, a name called "Hahad"):
. /etc/init.d/functions
Start () {
echo "Starting my Process"
Cd/opt
./haha.sh
}
Stop () {
Killall haha.sh
echo "stoped"
}

After you've written the script file, it's not over yet, so follow these steps:

Chmod +x hahad                     #增加执行权限
Chkconfig--add hahad              #把hahad添加到系统服务列表
Chkconfig Hahad on                  #设定hahad的开关 (on/off)
Chkconfig--list hahad                #就可以看到已经注册了hahad的服务
 

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.