Linux Admin 简明教程

Systemd Services Start and Stop

systemd 是 Linux 中运行服务的新方法。systemd 已经取代 sysvinit。systemd 为 Linux 带来了更快的启动时间,并且现已成为管理 Linux 服务的标准方式。尽管稳定,systemd 仍在不断发展。

systemd is the new way of running services on Linux. systemd has a superceded sysvinit. systemd brings faster boot-times to Linux and is now, a standard way to manage Linux services. While stable, systemd is still evolving.

systemd 作为一个 init 系统,用于管理在 Linux 内核启动后需要更改状态的服务和守护进程。通过状态更改启动、停止、重新加载和调整服务的状态。

systemd as an init system, is used to manage both services and daemons that need status changes after the Linux kernel has been booted. By status change starting, stopping, reloading, and adjusting service state is applied.

首先,让我们检查一下服务器当前运行的 systemd 版本。

First, let’s check the version of systemd currently running on our server.

[centos@localhost ~]$ systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP
+GCRYPT +GNUTLS +ACL     +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

[centos@localhost ~]$

在撰写本文时,CentOS 7 版本已完全更新,systemd 版本 219 是当前稳定版本。

As of CentOS version 7, fully updated at the time of this writing systemd version 219 is the current stable version.

我们还可以使用 systemd-analyze 分析服务器的最后一次启动时间

We can also analyze the last server boot time with systemd-analyze

[centos@localhost ~]$ systemd-analyze
Startup finished in 1.580s (kernel) + 908ms (initrd) + 53.225s (userspace) = 55.713s
[centos@localhost ~]$

当系统启动速度较慢时,可以使用 systemd-analyze blame 命令。

When the system boot times are slower, we can use the systemd-analyze blame command.

[centos@localhost ~]$ systemd-analyze blame
   40.882s kdump.service
   5.775s NetworkManager-wait-online.service
   4.701s plymouth-quit-wait.service
   3.586s postfix.service
   3.121s systemd-udev-settle.service
   2.649s tuned.service
   1.848s libvirtd.service
   1.437s network.service
   875ms packagekit.service
   855ms gdm.service
   514ms firewalld.service
   438ms rsyslog.service
   436ms udisks2.service
   398ms sshd.service
   360ms boot.mount
   336ms polkit.service
   321ms accounts-daemon.service

在使用 systemd 时,了解单元的概念非常重要。 Units 是 systemd 了解如何解释的资源。单元被分为 12 类,如下所示 −

When working with systemd, it is important to understand the concept of units. Units are the resources systemd knows how to interpret. Units are categorized into 12 types as follows −

  1. .service

  2. .socket

  3. .device

  4. .mount

  5. .automount

  6. .swap

  7. .target

  8. .path

  9. .timer

  10. .snapshot

  11. .slice

  12. .scope

在大多数情况下,我们将把 .service 作为单元目标。建议对其他类型进行进一步的研究。因为只有 .service 单元适用于启动和停止 systemd 服务。

For the most part, we will be working with .service as unit targets. It is recommended to do further research on the other types. As only .service units will apply to starting and stopping systemd services.

每个单元都在位于以下位置的文件中定义 −

Each unit is defined in a file located in either −

  1. /lib/systemd/system − base unit files

  2. /etc/systemd/system − modified unit files started at run-time

Manage Services with systemctl

要使用 systemd,我们需要非常熟悉 systemctl 命令。以下是 systemctl 最常用的命令行开关。

To work with systemd, we will need to get very familiar with the systemctl command. Following are the most common command line switches for systemctl.

Switch

Action

-t

Comma separated value of unit types such as service or socket

-a

Shows all loaded units

--state

Shows all units in a defined state, either: load, sub, active, inactive, etc..

-H

Executes operation remotely. Specify Host name or host and user separated by @.

Basic systemctl Usage

systemctl [operation]
example: systemctl --state [servicename.service]

快速查看在我们主机上运行的所有服务。

For a quick look at all the services running on our box.

[root@localhost rdc]# systemctl -t service
UNIT                       LOAD     ACTIVE      SUB     DESCRIPTION

abrt-ccpp.service          loaded   active   exited     Install ABRT coredump   hook
abrt-oops.service          loaded   active   running    ABRT kernel log watcher
abrt-xorg.service          loaded   active   running    ABRT Xorg log watcher
abrtd.service              loaded   active   running    ABRT Automated Bug  Reporting Tool
accounts-daemon.service    loaded   active   running    Accounts Service
alsa-state.service         loaded   active   running    Manage Sound Card State (restore and store)
atd.service                loaded   active   running    Job spooling tools
auditd.service             loaded   active   running    Security Auditing Service
avahi-daemon.service       loaded   active   running    Avahi mDNS/DNS-SD Stack
blk-availability.service   loaded   active   exited     Availability of block devices
bluetooth.service          loaded   active   running    Bluetooth service
chronyd.service            loaded   active   running    NTP client/server

Stopping a Service

首先,停止蓝牙服务。

Let’s first, stop the bluetooth service.

[root@localhost]# systemctl stop bluetooth

[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service   loaded    inactive dead    Bluetooth service

[root@localhost]#

如我们所见,蓝牙服务现在处于非激活状态。

As we can see, the bluetooth service is now inactive.

再次启动蓝牙服务。

To start the bluetooth service again.

[root@localhost]# systemctl start bluetooth

[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service  loaded    active   running Bluetooth     service

[root@localhost]#

Note − 我们没有指定 bluetooth.service,因为 .service 是隐含的。将处理服务追加到单元类型中是一种好习惯。因此,从这里开始,我们将使用 .service 扩展名来明确我们正在处理服务单元操作。

Note − We didn’t specify bluetooth.service, since the .service is implied. It is a good practice to think of the unit type appending the service we are dealing with. So, from here on, we will use the .service extension to clarify we are working on service unit operations.

可以在服务上执行的主要操作包括 −

The primary actions that can be performed on a service are −

Start

Starts the service

Stop

Stops a service

Reload

Reloads the active configuration of a service w/o stopping it (like kill -HUP in system v init)

Restart

Stops, then starts a service

Enable

Starts a service at boot time

Disable

Stops a service from automatically starting at run time

上述操作主要用于以下场景 −

The above actions are primarily used in the following scenarios −

Start

To bring a service up that has been put in the stopped state.

Stop

To temporarily shut down a service (for example when a service must be stopped to access files locked by the service, as when upgrading the service)

Reload

When a configuration file has been edited and we want to apply the new changes while not stopping the service.

Restart

In the same scenario as reload, but the service does not support reload.

Enable

When we want a disabled service to run at boot time.

Disable

Used primarily when there is a need to stop a service, but it starts on boot.

检查服务的当前状态 −

To check the status of a service −

[root@localhost]# systemctl status network.service
network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: active (exited) since Sat 2017-01-14 04:43:48 EST; 1min 31s ago
Docs: man:systemd-sysv-generator(8)

Process: 923 ExecStart = /etc/rc.d/init.d/network start (code=exited, status = 0/SUCCESS)

localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking...
localhost.localdomain network[923]: Bringing up loopback interface:  [  OK  ]
localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.

[root@localhost]#

向我们展示网络服务的当前状态。如果我们想看到所有与网络相关的服务,我们可以使用 −

Show us the current status of the networking service. If we want to see all the services related to networking, we can use −

[root@localhost]# systemctl --all -t service | grep -i network
network.service                       loaded    active    exited    LSB: Bring up/
NetworkManager-wait-online.service    loaded    active    exited    Network Manager
NetworkManager.service                loaded    active    running   Network Manager
ntpd.service                          loaded    inactive  dead      Network Time
rhel-import-state.service             loaded    active    exited    Import network

[root@localhost]#

对于熟悉 sysinit 管理服务方法的人来说,向 systemd 过渡很重要。systemd 是在 Linux 中启动和停止守护进程服务的新方式。

For those familiar with the sysinit method of managing services, it is important to make the transition to systemd. systemd is the new way starting and stopping daemon services in Linux.