Systemd 服务启动和停止

systemd 是在 Linux 上运行服务的新方式。systemd 取代了 sysvinitsystemd 为 Linux 带来了更快的启动时间,现在已成为管理 Linux 服务的标准方式。systemd 虽然稳定,但仍在不断发展。

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

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

[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 是当前稳定版本。

我们还可以使用 systemd-analyze 分析上次服务器启动时间>

[centos@localhost ~]$ systemd-analyze
启动完成于 1.580s (内核) + 908ms (initrd) + 53.225s (用户空间) = 55.713s
[centos@localhost ~]$

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

[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 的概念非常重要。Unitssystemd 知道如何解释的资源。Units 分为以下 12 种类型 −

  • .service
  • .socket
  • .device
  • .mount
  • .automount
  • .swap
  • .target
  • .path
  • .timer
  • .snapshot
  • .slice
  • .scope

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

每个 unit 都在位于 −

的文件中定义
  • /lib/systemd/system − 基本单元文件

  • /etc/systemd/system − 在运行时启动的修改后的单元文件

使用 systemctl 管理服务

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

开关 操作
-t 以逗号分隔的单元类型值,例如服务或套接字
-a 显示所有已加载的单元
--state 显示处于定义状态的所有单元,可以是:加载、子、活动、非活动等。
-H 远程执行操作。指定主机名或主机和用户,以 @ 分隔。

systemctl 基本用法

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

快速查看我们盒子上运行的所有服务。

[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

停止服务

首先,我们来停止蓝牙服务。

[root@localhost]# systemctl stop bluetooth

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

[root@localhost]#

我们可以看到,bluetooth 蓝牙服务现在处于非活动状态。

再次启动蓝牙服务。

[root@localhost]# systemctl start bluetooth

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

[root@localhost]#

注意 −我们没有指定 bluetooth.service,因为 .service 是隐含的。考虑将 单元类型 附加到我们正在处理的服务上是一种很好的做法。因此,从现在开始,我们将使用 .service 扩展来阐明我们正在处理服务单元操作。

可以对服务执行的主要操作是 −

启动 启动服务
停止 停止服务
重新加载 重新加载服务的活动配置而不停止它(如 system v init 中的 kill -HUP)
重新启动 停止,然后启动服务
启用 在启动时启动服务
禁用 阻止服务在运行时自动启动

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

启动 启动已处于停止状态的服务。
停止 暂时关闭服务(例如,当必须停止服务才能访问服务锁定的文件时,如升级服务时)
重新加载 当配置文件已被编辑并且我们想要在不停止服务的情况下应用新的更改时。
重新启动 与重新加载相同的场景,但服务不支持重新加载
启用 当我们希望已禁用的服务在启动时运行时。
禁用 主要用于需要停止服务但服务在启动时启动的情况。

检查服务状态 −

[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]#

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

[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 中启动和停止守护进程服务的新方法。