Linux startup script learning tutorial

  
 

One, init and runlevel

Friends who have been exposed to Linux should have learned that the traditional init defines seven run levels, such as level 0 is

off System, level 2 entry is a multi-user character interface, level 5 is a graphical interface, and so on.

The /etc/inittab file specifies the commands that need to be run when the system enters each runlevel. When the machine boots, the init starts at the

row level 0 level, level one level. Run up to the default runlevel set in the /etc/inittab file.

Modify the default run level of the system:

#vi /etc/inittab

Find the following fields

id:5:initdefault:


//Edit inittab file

//The default runlevel is 5, which means you can enter the graphical interface directly when the system boots. We can

by modifying this The default runlevel for digital custom system booting

Second, know the service startup script

An article in front of my blog has introduced how to write and configure a new service startup script (detailed See linux

Configure an application (software) as a system service), simply took the concept of a startup script, and here again.

All the startup scripts are stored in /etc/init.d/. We can start

an application by using the /etc/init.d/script name startup mode.

For example, enter the command “/etc/init.d/smb start” to start the smb service.

If you are on a RedHat system, you can also start a service with service smb start.

Although scripts in the /etc/init.d directory can start and stop individual services, init is not directly found in the

/etc/init.d directory when the system boots. The startup script for each service is searched in the /etc/rc.d/directory, which contains subdirectories of different init startup levels, such as rc0.d,

rc1.d, and so on. The directory contains the startup script for the corresponding startup level,

as shown:

Startup script1

Next, let's take a look at the directories included in these rcx.d directories. The content, the content is all symbolic links, linked to the script in the upper level init directory

.

These symbolic link names all begin with S or K followed by a number and the service name controlled by the script, such as

S10network.

When init transitions from a low run level to a high run level, it runs all scripts starting with S in numerically increasing order,

S=start, which corresponds to the startup script. Service; K = kill, that is, kill the script corresponding service

First look at the system default run level init5 (that is, boot directly to the graphical interface) corresponding script directory rc5.d,

Figure:

Startup script 2


The rc5.d directory contains both K scripts and S scripts. When the system starts, init links by these symbols. The sequence of numbers starts

All S scripts are opened. When the system is shut down, init also closes the applications corresponding to all scripts in order.

You can see that these symbolic links actually point to the startup script in the init.d directory:

Startup script 3

Startup script 4

Now look back at the contents of the rc0.d (ie shutdown system) directory:

Startup script 5

You can see all the symbolic links starting with K in the rc0.d directory, when we type All system services are closed by

when init 0 is commanded.

Other rcx.d directory content is similar, are some symbolic links to the startup script, just put different startup script symbolic links according to different runlevels

.

Three, the order of custom startup services

Before introduced to the rcx.d directory, the symbolic link to the startup script is composed of K or S + number + service name, in the middle of this

Numbers are very important. When the system starts, all scripts starting with S are executed in increments of this number. When the system is shut down, all scripts starting with K are executed according to this

number.

Knowing this, we can control the order in which services are started according to our own needs.


How do I customize symbolic links?

Startup script:

#ln -s /etc/init.d/smb S60smb //Redefining the script startup order of the smb service

Figure:

Startup script 6


Start script 7

Close script:

#ln -s /etc/init.d/smb K60smb //The same as the startup script, S changed to linux boot automatically start script method 1, related basic knowledge points 1) Redhat startup mode and execution order is loaded execution init program /etc/rc.d/rc.sysinit # executed by init The first script /etc/rc.d/rc $RUNLEVEL # $RUNLEVEL default run mode /etc/rc.d/rc.local #The corresponding level after the service is started, the file is executed (in fact, it can also be needed The executed command is written to the file) /sbin/mingetty #Wait for user login

In Redhat, /etc/rc.d/rc.sysinit mainly does the same initialization work in each run mode, including : Calling keymap and system fonts

Starting swapping

Setting hostname

Setting NIS domain name

Checking (fsck) and mounting file system

Open quota

Load sound card module

Set system clock

Wait.

/etc/rc.d/rc The scripts in the appropriate directory of the

line are executed according to the run mode specified by their parameters (run level, which you can set in the inittab file). Anything that starts with Kxx is called with stop as the argument; if it starts with Sxx,

is called with start as the argument. The order of the calls is executed from xx to small. (where xx is a number, indicating the order of the

). For example, assuming the default mode of operation is 3, /etc/rc.d/rc will call /etc/rc.d as described above.

script under /rc3.d/. It is worth mentioning that the running modes 2, 3, and 5 in Redhat use /etc/rc.d/rc.local as the last

in the initialization script, so the user can own this file. Add some commands that need to be executed

before logging in after other initialization work.

init After waiting for /etc/rc.d/rc to finish (because the action in /etc/rc.d/rc in /etc/inittab is wait),

will be Run /sbin/mingetty on each specified virtual terminal and wait for the user's login.

At this point, the startup of LINUX ends.

2)init runlevels and directives

1. What is INIT:

init is one of the indispensable programs in Linux system operations.

The so-called init process, which is a user-level process initiated by the kernel.

After the kernel starts itself (has been loaded into memory, started running, and all device drivers and data nodes have been initialized), it is started by a user-level program init. The way to complete the boot process. Therefore, init is always the first

process (its process number is always 1).

The kernel will look it up in a few places where init has been used in the past, and its correct location (for Linux systems) is

Copyright © Windows knowledge All Rights Reserved