Systemd 启动停止
systemd 是在 Linux 上运行服务的新方式。
systemd 有一个被取代的
sysvinit。
systemd 为 Linux 带来更快的启动时间,现在是管理的标准方式e Linux 服务。
systemd 虽然稳定,但仍在不断发展。
systemd 作为一个初始化系统,用于管理在 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
Startup finished in 1.580s (kernel) + 908ms (initrd) + 53.225s (userspace) = 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 是
systemd 知道如何解释的资源。单位分为以下 12 种类型-
.service
.socket
.device
.mount
.automount
.swap
.target
.path
.timer
.snapshot
.slice
.scope
在大多数情况下,我们将使用 .service 作为单位目标。建议对其他类型做进一步研究。因为只有
.service 单元适用于启动和停止
systemd 服务。
每个
单元都在位于以下任一位置的文件中定义-
/lib/systemd/system-基本单元文件
/etc/systemd/system-运行时启动的修改单元文件
使用 systemctl 管理服务
要使用
systemd,我们需要非常熟悉
systemctl 命令。以下是
systemctl 最常用的命令行Switch。
Switch |
Action |
-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]#
如我们所见,蓝牙服务现在处于非活动状态。
再次启动蓝牙服务。
[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 扩展名来说明我们正在处理服务单元操作。
可以对服务执行的主要操作是-
Start |
启动服务 |
Stop |
停止服务 |
Reload |
重新加载服务的活动配置而无需停止它(如 system v init 中的 kill-HUP) |
Restart |
启动,然后停止服务 |
Enable |
在启动时启动服务 |
Disable |
停止服务在运行时自动启动 |
上述操作主要用于以下场景-
Start |
启动已处于停止状态的服务。 |
Stop |
暂时关闭服务(例如,当必须停止服务以访问被服务锁定的文件时,如升级服务时) |
Reload |
当配置文件已被编辑并且我们希望在不停止服务的情况下应用新更改时。 |
Restart |
和reload一样的场景,但是服务不支持reload。 |
Enable |
当我们希望禁用的服务在启动时运行时。 |
Disable |
主要在需要停止服务时使用,但它在启动时启动。 |
检查服务的状态-
[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 中启动和停止守护进程服务的新方式。