Linux How to implement boot start program detailed

Source: Internet
Author: User
Tags numeric value

we assume that you are already familiar with the boot process of other operating systems, and understand the self-test boot steps of the hardware, starting with the boot loader for the Linux operating system (usually lilo for personal computers) and introducing the Linux boot steps.

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 the PC (because of their natural design flaws) has not yet been able to access all the memory on the machine. As a result, the kernel must be fully loaded into the first megabyte of available RAM. To achieve this, the kernel is compressed. The header of this file contains the necessary code to set the CPU into Safe mode (to remove the memory limit) and then decompress the remainder of the kernel.

Execution Kernel
once the kernel has been decompressed in memory, it can start running. At this point the kernel only knows its own built-in functions, that is, the kernel part of the module is compiled to not be used. Most fundamentally, the kernel must have enough code to set up its own virtual memory subsystem and root filesystem (usually the ext2 file system). Once the kernel is up and running, the hardware detection determines which device drivers need to be initialized. From here, the kernel is able to mount the root filesystem (a process similar to that used by Windows to identify and access C-disk). After the kernel has mounted the root file system, a program called Init is started and run.

Note: Here we deliberately omit many of the details of the Linux kernel boot, and these details are only of interest to the kernel developers. If you are curious, you can visit the "Kernel Hackers Guide" at the address.
Init process
The init process is first run in a non-kernel process, so its process number PID value is always 1. Init reads its configuration file/etc/inittab, which determines the RunLevel (Runlevel) that needs to be started. Fundamentally, the runlevel specifies the behavior of the system as a whole, with each level (represented by an integer from 0 to 6) that satisfies a specific purpose. If the initdefault level is defined, the value is directly selected, otherwise the user is required to enter a numeric value that represents the run level.
after entering a number representing the run level, Init executes a command script based on the definition in the/etc/inittab file. The default runlevel depends on the selection of the logon program during the installation phase: whether to use text-based or X-window-based login programs.
RC Command Script we already know that when the runlevel changes, it is up to the/etc/inittab file to define which command script to run. These command scripts are responsible for starting or stopping various services that are specific to that runlevel. 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 invoking the appropriate command script for each runlevel in the correct order. We can imagine that such a command script could easily become unmanageable! In order to prevent such events from happening, a well-designed scheme is needed.

for each runlevel, there is a subordinate directory in the/ETC/RC.D subdirectory. The naming method for the subordinate subdirectories of these runlevel is RCX.D, where x is the number representing the run level. For example, all command scripts running level 3 are stored in the/ETC/RC.D/RC3.D subdirectory. In sub-directories at each runlevel, there are symbolic links to the 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, its symbolic link name begins with the letter S; if the command script is used to close a service, its symbolic link will begin 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 use the DNS service to resolve the host name! In order to arrange their execution sequence, the letter S or K is followed by a two-digit number, the value is small in front of the large number of execution. For example:/etc/rc.d/rc3.d/s50inet will be executed before/etc/rc.d/rc3.d/s55named (s50inet configure network settings, 55named to start the DNS server). The command scripts stored in the/ETC/RC.D/INIT.D subdirectory, which are symbolic links, are the real doers, and they complete the process of starting or stopping various services. When/ETC/RC.D/RC runs through each specific run-level subdirectory, it invokes each command script execution sequentially, in order of the number. It runs the command script that starts with the letter K, and then runs the command script that begins with the letter S. For a command script that starts with the letter K, the stop parameter is passed, and the start parameter is passed similarly to a command script that begins with the letter S. Write your own RC command scripts in the days of maintaining the Linux system, you will certainly encounter the need for system administrators to power on or off the command foot
The circumstances in which this modification was made.

There are two ways to achieve the purpose of the modification:
If the changes are only useful when booting the boot, and the changes are not significant, consider simply editing the/etc/rc.d/rc.local script. This command script is executed at the last step of the boot process.
If you make a fine-grained modification, or require the shutdown process to be explicitly stopped, you 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 parameters and complete the appropriate operation.
The first method of editing the/etc/rc.d/rc.local script is, 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 and append the command you intend to execute to the end of the file. This is really handy for the one or two-line revision.

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 to see how it's done (by the way, you can use our example as a template to modify and add as you want).
Suppose you intend 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 command script function (so that it will not be forgotten after one year);
Verify that this command script exists before attempting to run it;
Accept the start and stop parameters and perform the required actions.

After the parameter is given, we can write a script for the command. This program is very simple, you can write it yourself, I do not give it 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 relevant run-level subdirectories. 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 runlevel are the work of the day. Finally, it is expected that this command script will be turned off when it enters RunLevel 6 (reboot).

Activating or banning a service item sometimes discovers that a particular service is not required to be started when booting. This is especially true if you are considering replacing Windows NT file and print servers with Linux. We already know that by changing the name of a symbolic link in a specific runlevel subdirectory, you can let the service 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 disable the service.

When you learn this renaming method, you may find the graphical interface ksysv easier to master. Although it was originally designed for use in KDE environments, it also works well in the GNOME environment, which is installed by default in red Hatlinux 7.2. If you want to start it, simply open a xterm window and enter the KSYSV command. A window appears, listing 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. When trying to make changes to the startup script, keep in mind that the changes you make may cause your system to not work properly and that it cannot be resumed with a restart. Do not experiment with new settings on a functioning system, and back up all the files you are ready to modify. The most important thing is to prepare a boot disk safekeeping at hand.

How the Linux system starts automatically when the program is powered on

Core Tip: The system's services can be started automatically when booting, that under the Linux system if you want the program to start automatically when the boot? We know the Windows system "start"-"All Programs"-"Start" Just put a shortcut in it, what about the Linux system? ... System services in the boot generally can automatically start, that under the Linux system if you want the program to start automatically when the boot? We know that in the Windows system "start" and "All Programs" and "Start" put a shortcut on the line, the Linux system?

This is also a relatively simple problem, there are many ways to solve, here are three ways to introduce. Because it is a simple introduction, so the details are not very detailed, you can see the manual through the man.

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

Using the command VI /etc/rc.local

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

For example, each time you turn on a, this script is placed under/OPT, you can add a line "/opt/./" in "/etc/rc.local", or two lines of "cd/opt" and "./".
Second, crontab (similar to the Windows Task Scheduler service)
with Crontab, you can set a program's execution schedule, for example, to have 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;
"-E" is an editor, and VI is no different (in fact, with VI editing a specific file);
"-D" is basically not used, because it removes all of the user's schedule, usually with the "-E" editor to delete the time-line;
So how 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 execute, such as
M: Minutes (0-59)
h: Hours (0-23)
D: Date (1-31)
m: Month (1-12)
D: One day of the one week (0-6, 0 for Sunday)
These 5 time fields are separated by a space with a value that can be a number or a comma-separated number (or other), or "*" If you do not need to set it.
For example, a daily 8:5 execution is "5 8 * * */opt/./".
as if and "boot program automatically start" Pull away, now return to the point. In fact, the above introduction of the CRONTAB function has the ability to boot automatically start, you can write a monitoring script, every 5 minutes (*/5 * * * *./, if the program is not in the restart.
Third, registration system services
operating system comes with services, such as SSH,FTP, etc., the boot is automatically started, we can also use this way to develop their own programs to improve the "price."

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

chkconfig--add Service name (First, add as system service, note that add is preceded by two bars)

Chkconfig-leve Boot level service name on

(Description, Level 3 is started in the command line mode, Level 5 represents the start in the graphical interface, on means open)

Chkconfig-leve Boot level service name off

(description, off means off-boot)

Example:chkconfig-level 3 mysql on (description: Let MySQL service in command line mode, boot with system)

You can also use the Chkconfig--add service name to remove system services


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 self-booting, you can use the command:

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


Let's say, for example, how to add a shell script as a system service and follow the system boot up:

you can see that there are a lot of files under "/etc/rc.d/init.d", each one can see the content, in fact, some shell scripts.
The startup 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 to put here.
the contents of the script file are also very simple, similar to this (for example, a name called "Hahad"):
start () {
echo "Starting my Process"
Stop () {
echo "stoped"
after you've written the script file, it's not over yet, so go ahead and complete the following steps:
chmod +x Hahad #增加执行权限
Chkconfig--add Hahad #把hahad添加到系统服务列表
Chkconfig Hahad on #设定hahad的开关 (on/off)
Chkconfig--list Hahad #就可以看到已经注册了hahad的服务

It was time to finish all the work.

Article turned from:

Linux How to implement boot start program detailed

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: 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.