Ubuntu init and system service settings

Source: Internet
Author: User
InitInit is a program located in/sbin/init. It is in linux, after all the device drivers and data structures are initialized during system startup, A user-level program started by the kernel, and the init program then completes the system startup process. Ubuntu is slightly different from traditional Linux. upstart is used to start the system, but the init program is still maintained on the surface. Traditionally, linux has several different running levels.

Init
 
Init is a program located in/sbin/init. It is in linux, after all the device drivers and data structures are initialized during system startup, A user-level program started by the kernel, and the init program then completes the system startup process.

Ubuntu is slightly different from traditional Linux. upstart is used to start the system, but the init program is still maintained on the surface.
 
Running level
 
Traditionally, linux has several different running levels, including:

#0-stopped
#1-single-user mode
#2-multiple users without NFS
#3-full multi-user mode (Standard Operation Level)
#4-reserved by the System
#5-X11 (x window)
#6-Restart

After the system is started, the default level settings in the/etc/inittab file read by init are used to determine the level at which the system enters Level 3.

However, ubuntu is not the same as the traditional one. By default, the/etc/inittab file cannot be found, and the running level is also different. The specific levels are as follows:

#0-Shut down the system

#1-single-user mode

#2 ~ 5-Complete Multi-User Mode

#6-Restart

That is to say, Levels 2, 3, 4, and 5 are the same by default, and the system's default level settings are not in the inittab file, it is written in/etc/init/rc-sysinit. conf file. Open this file and find the following sentence:

Env DEFAULT_RUNLEVEL = 2

This indicates that the system is currently in Level 2 by default.

In addition, there is a piece of code starting with if [-r/etc/inittab] in this article. The default running level function specified by the system using inittab is retained here, that is, if you have manually created/etc/inittab, init starts the system at the default running level specified in/etc/inittab. For example, if you want the system to use level 3 as the default running level, you only need to add the following line to the inittab file:

Id: 3: initdefault:

After the/etc/init/rc-sysinit.conf determines the run level, init will further run/etc/init. d/rc and then enter/etc/rc [?] According to the level. D. Start or close the corresponding service.
 
Service Startup and shutdown scripts
 
The script for starting and disabling services in ubuntu is stored with/etc/rc [?]. D directory. [X] indicates 0 ~ 6, corresponding to level 0 ~ 6. For example, rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d In the/etc directory. Assuming that the default level specified in the rc-sysinit.conf or inittab is 2, init will execute a script in the/etc/rc2.d directory to start or close the corresponding service.

If/etc/rc [?] is enabled. D directory, you will find that all the files under these directories are symbolic links, such as Snnxxxx or Knnxxxx, and all point to/etc/init. d. That is to say, the startup or shutdown scripts of services at different running levels are put under/etc/init. d, but according to the needs of different levels, in the corresponding/etc/rc [?]. D. Place a link to the directory. Different levels require different services, so different/etc/rc [?]. The linked files in the d directory are also different.

In the link file, the name starting with S indicates that/etc/init is called. the corresponding script in the d directory will pass a start parameter, that is, start the corresponding service, and start with K is to pass a stop parameter, thus disabling this service, K indicates kill.

The nn after S and K is a number, indicating the order in which the script is executed. The trumpet is placed after the front and rear, so as to solve the possible successive dependencies between different services. For example, the ftp service depends on the start of the network service, so the ftp service number must be greater than the number of the network service, and then start the network service after it is started.

The last xxxx is the service name.

In addition, except for/etc/rc [0 ~ 6] in addition to the. d file, there is also a/etc/rc. d directory, in which the service script and/etc/rc [0 ~ 6] The. d format is similar. It is also a link to the script in/etc/init. d, but it will be in/etc/rc [0 ~ 6]. d is executed first before the script is executed.
 
Service Installation
 
According to the above introduction, it is clear how to install a software as a service, that is, in/etc/init. d Add a STARTUP script for the service, and then at the corresponding level of/etc/rc to start the service [0 ~ 6] Add a symbolic link to the script in/etc/init. d according to the file name format.

Take apache2 as an example. By default, apache2 is compiled and installed in/usr/local/apache2, And the startup script of apache2 is/usr/local/apache2/bin/apachectl, to install the service, copy this apachectl to the/etc/rc [?] corresponding to the running level of The apache2 server to be started. In the d directory, ubuntu runs under level 2 in half, so copy it to/etc/rc2.d. The command is as follows:

Sudo cp/usr/local/apache2/bin/apachectl/etc/init. d/httpd

Here, we change the file name of the copied script to httpd, so the name of apache2 in the system service is httpd.

To manually add a service, you need to create a link in the form of Snnxxxx for httpd in/etc/rc2.d. Assume that the Starting sequence is set to 80, and a link to/etc/init. d/httpd/etc/rc2.d/S80httpd is created. The command is as follows:

Sudo ln-s/etc/init. d/httpd/etc/rc2.d/S80httpd

Run sysv-rc-conf or chkconfig-list to check whether the httpd service has been created under runlevel 2. After the service is restarted, apache2 is automatically started.

To manually start, shut down, or restart the httpd server, you can use the service command + service name + parameter format. The following three commands are used to start, shut down, and restart the apache2 server:

Service httpd start

Service httpd stop

Service httpd restart

To install the service in addition to the above manual Links, you can also use some tool software to achieve, such as commonly used update-rc.d, chkconfig and sysv-rc-conf and so on.

Related Article

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.