[Boot start] Linux boot and run levels

Source: Internet
Author: User
Tags naming convention

Program self-starting method in embedded system

In many embedded systems, due to the low availability of resources, it is often possible for the application to start automatically after the system starts to reduce user action and conserve resources.    How do I get my app to start automatically? In a Linux system, there are three ways to configure an application to start automatically:
1. Launch directly via/LINUXRC script
Once the Linux kernel is started, it will initialize all hardware devices through the driver, which can be seen on the PC monitor at startup, with each driver printing some relevant information. When initialization is complete, init is usually called, and the Init=/app_program statement within Init is called through loader(You can customize the program that runs first by loader to the core by passing in Init=/program)
For example, in a desktop Linux system, the INIT process reads the/etc/inittab file to determine the execution level and which scripts and commands. Embedded application development, according to the actual situation can decide whether to use the standard init execution mode, perhaps this init is a static program, it can complete our embedded application of the specific task, it does not have to consider inittab, here can adopt more flexible measures.
2. Add a startup script under/ETC/INIT.D
In general, most Linux operating systems use scripts under/etc/init.d/(or/ETC/RC.D/INIT.D) to configure the application to start automatically.
For example, in some Linux systems, the corn program is started through the/etc/init.d/corn script, Apache starts with/ETC/INIT.D/HTTPD, SYSLOGD starts with/ETC/INIT.D/SYSLOGD, And sshd is started by/etc/init.d/sshd script.
Typically these scripts run through symbolic links from a specific rc.d directory. To configure which RC.D directory to run scripts from, the Linux system provides many different tools and can also be configured manually. The Linux system has a directory containing all the actual startup script files. It may be/etc/init.d, or it may be/etc/rc.d/rc.d. There is also an additional directory for each runlevel (RunLevel), which may be/etc/rc2.d or/etc/rc.d/rc2.d. The files in these directories are usually symbolic links to the actual script files.
3. Add commands directly to the/etc/rc.d/rc.local script
In the Linux system, there is a similar to the Windows system Autoexec.bat files, it is/etc/rc.d/rc.local, the system automatically run after the user's application or start the system service commands saved in the Development Board root file system in this file. You can therefore edit the rc.local file and add the program (command) you want to execute to the folder.the Linux system will automatically execute the program (command) before it has been logged on after booting.The purpose of running the user's application automatically on the boot.

Run level

Linux system has 7 runlevel (runlevel)
RunLevel 0: System down state, system default RunLevel cannot be set to 0, otherwise it will not start normally
RunLevel 1: Single User working status, root privilege, for system maintenance, remote login Forbidden
RunLevel 2: Multi-User State (no NFS)
RunLevel 3: Full multiuser State (with NFS), enter console command line mode after login
Run Level 4: System not used, reserved
Run level 5:x11 console, login to GUI mode
RunLevel 6: The system shuts down gracefully and restarts, the default runlevel cannot be set to 6, otherwise it will not start normally

The operating level principle:
1. There are many server scripts under directory/ETC/RC.D/INIT.D, commonly referred to as services (service)
2. Under/ETC/RC.D There are 7 directories named RCN.D, corresponding to the 7 operating levels of the system
3.RCN.D directories are symbolic link files that point to a service script file under the INIT.D directory, with a naming convention of k+nn+ service name or s+nn+ service name, where NN is a two-digit number.
4. The system will enter the corresponding RCN.D directory according to the specified RunLevel, and retrieve the linked files under the directory in the order of file name.
For files starting with K, the system terminates the corresponding service
For files beginning with S, the system will start the corresponding service
5. Viewing runlevel: RunLevel
6. Enter other RunLevel: Init N
7. Additional Init0 for shutdown, init 6 reboot system

From the following is my understanding: InitThe file name refers to the first script that is started by the system
INIT.DDirectory name, in the directory is the start-up scripts for each service, such as sshd, httpd, etc.
RCX.DThe directory name, followed by x, represents each runlevel. There is a link file within the directory where each service launches a script within INIT.D, starting with the name of the linked file. K starts with a service that does not start, and S begins to start the service
rc.localLink file, refer to/etc/rc.d/rc.local. When the system is initialized and the service is started, finallyExecution of a script, some simple scripts can be put into one of the problems encountered in reality

1. Want to start an application automatically, and the application depends on an environment variable

2. Auto-start applications can be terminated using CTRL + C

For the above two requirements, multiple problems encountered

1. Add the environment variable to/etc/profile, then use source in/etc/rc.local to enable the environment variable, but since Rc.local script starts with #!bin/sh as Dash instead of bash, Resulting from the log can be seen rc.local execution failure, of course, the last #!bin/sh->#!bin/bash can

2. If executed in rc.local./application The boot will be stopped in rc.local, resulting in unable to exit, complete the boot; if./application & will ctr+c unable to exit, currently using the latter

#!bin/bash-/etc/5/usr/Local. /application)&

[Boot start] Linux boot and run levels

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.