Linux管理员 - 系统服务启动和停止

systemd 是在Linux上运行服务的新方法. systemd 有一个取代的 sysvinit . systemd 为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,在撰写本文时系统版本219完全更新是当前的稳定版本.

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

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

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

 
 [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 时,了解单位的概念非常重要. 单位是资源 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 最常用的命令行开关.

SwitchAction
-  t服务或套接字等单位类型的逗号分隔值
-  a显示所有已加载单位
-  state显示处于已定义状态的所有单位:加载,子,活动,非活动等.
-  H远程执行操作.指定由@分隔的主机名或主机和用户.

基本systemctl用法

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

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

[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 扩展来阐明我们正在处理服务单元操作.

可以对a执行的主要操作服务是去;

开始启动服务
停止停止服务
重新加载重新加载服务的活动配置而不停止它(如系统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中启动和停止守护程序服务的新方法.