Linux service startup item configuration

systemd boot since boot

The two methods mentioned above apply to the classic system V control system startup and shutdown, but currently (October 2018), systemd system software control methods have been used on most distributions, including Ubuntu16, centos The .systemd system manages the running of processes under Linux and belongs to the application program, not the scope of the Linux kernel.

It is also very simple to set the system to boot and start automatically on the systemd system (although the systemd software management tool is not simple).

Determine whether the system is managed by the systemd tool

It should be noted here that systemd is a pre-installed tool on the Linux distribution, which is used to manage the start-up and end of system software, so generally speaking, this set of things depends on the distribution. Tool, then you can use it to manage the process, if not, even if you install it, it will not be configured as a system management tool by default.

To check whether the system uses the systemd tool, we can use the following command:

systemd --version

If the system returns information similar to the following, it indicates that the system is managed by the systemd tool:

systemd 232
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN

Use of systemctl

The management of the software is mainly through the systemctl command in the systemd tool. Compared with the previous control method of system V, systemd is more concise and user-friendly. Take httpd as an example:

开启httpd服务:
sudo systemctl start httpd
设置httpd服务自启动:
sudo systemctl enable httpd

As for the shutdown and cancellation since the start, everyone should know it.

Set boot from boot

Let's go back to the key point and set the boot to start automatically.

We need to set up a configuration file for the target. In fact, this is predictable. As a complex system, Linux must depend on the user to specify the dependency, run level, operating environment, etc., which must be specified at startup. The system knows how to run the software correctly. This configuration file is fixed with .service as a suffix. For example, if we want to run the test.sh script in the / home / downey directory, we can add a configuration file test.service :

[Unit]
Description=
Documentation=
After=network.target
Wants=
Requires=

[Service]
ExecStart=/home/downey/test.sh
ExecStop=
ExecReload=/home/downey/test.sh
Type=simple

[Install]
WantedBy=multi-user.target

Put the file in the / usr / lib / systemd / system or / etc / systemd / system directory, and then you can test it:

sudo systemctl start test.service  

Then you can check whether your /home/downey/test.sh script has been run, if it has run, it means there is no problem with the configuration file. Then you can type:

sudo systemctl enable test.service  

Set the test script to start at boot. If there is no problem in the previous step, basically there will be no problems in this step, the system will print the following information:

Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /usr/lib/systemd/system/test.service.  

As you can see, a soft link of the /usr/lib/systemd/system/test.service file is created here in the /etc/systemd/system/multi-user.target.wants/ directory. finished.

Simple parsing of configuration files

In the above configuration file, for demonstration purposes, I also wrote out some important configuration items that are not required by this test script, but can be deleted if they are not needed, but [Unit] / [Service] / [Install] Three tags need to be retained.
Let's briefly introduce the configuration items one by one:

Description:运行软件描述
Documentation:软件的文档
After:因为软件的启动通常依赖于其他软件,这里是指定在哪个服务被启动之后再启动,设置优先级
Wants:弱依赖于某个服务,目标服务的运行状态可以影响到本软件但不会决定本软件运行状态
Requires:强依赖某个服务,目标服务的状态可以决定本软件运行。
ExecStart:执行命令
ExecStop:停止执行命令
ExecReload:重启时的命令
Type:软件运行方式,默认为simple
WantedBy:这里相当于设置软件,选择运行在linux的哪个运行级别,只是在systemd中不在有运行级别概念,但是这里权当这么理解。  

If there are multiple items, separate them with commas.

Published 59 original articles · 69 praises · 270,000+ views

Guess you like

Origin blog.csdn.net/pansaky/article/details/105678390