Linux Admin 简明教程

Resource Mgmt with systemctl

systemctl 是用于控制 systemd 的实用程序。systemctl 为 CentOS 管理员提供了在 systemd 上执行多项操作的能力,包括:

systemctl is the utility used to control systemd. systemctl provides CentOS administrators with the ability to perform a multitude of operations on systemd including −

  1. Configure systemd units

  2. Get status of systemd untis

  3. Start and stop services

  4. Enable / disable systemd services for runtime, etc.

systemctl 的命令语法相当简单,但可能会混淆选项和开关。我们将介绍最基本的 systemd 功能,适用于 CentOS Linux 的管理。

The command syntax for systemctl is pretty basic, but can tangle with switches and options. We will present the most essential functions of systemctl needed for administering CentOS Linux.

Basic systemctl syntax:
systemctl [OPTIONS] COMMAND [NAME]

以下是 systemctl 常用的命令:

Following are the common commands used with systemctl −

  1. start

  2. stop

  3. restart

  4. reload

  5. status

  6. is-active

  7. list-units

  8. enable

  9. disable

  10. cat

  11. show

以上我们已经通过 systemctl 讨论了 start、stop、reload、restart、enable 和 disable 命令,现在让我们进一步了解剩余的常用命令。

We have already discussed start, stop, reload, restart, enable and disable with systemctl. So let’s go over the remaining commonly used commands.

status

在最简单的形式中,status 命令可以用来查看整体系统状态,就像下例所示:

In its most simple form, the status command can be used to see the system status as a whole −

[root@localhost rdc]# systemctl status
 ● localhost.localdomain
  State: running
  Jobs: 0 queued
  Failed: 0 units
  Since: Thu 2017-01-19 19:14:37 EST; 4h 5min ago
CGroup: /
       ├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
       ├─user.slice
       │ └─user-1002.slice
       │   └─session-1.scope
       │     ├─2869 gdm-session-worker [pam/gdm-password]
       │     ├─2881 /usr/bin/gnome-keyring-daemon --daemonize --login
       │     ├─2888 gnome-session --session gnome-classic
       │     ├─2895 dbus-launch --sh-syntax --exit-with-session

上面的输出已经简化。在实际中,systemctl status 将输出大约 100 行树状进程状态。

The above output has been condensed. In the real-world systemctl status will output about 100 lines of treed process statuses.

假设我们要检查防火墙服务的 – 状态

Let’s say we want to check the status of our firewall service −

[root@localhost rdc]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-01-19 19:14:55 EST; 4h 12min ago
 Docs: man:firewalld(1)
Main PID: 825 (firewalld)
CGroup: /system.slice/firewalld.service
       └─825 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

如你所见,我们的防火墙服务目前处于活动状态,且已经运行超过 4 小时。

As you see, our firewall service is currently active and has been for over 4 hours.

list-units

list-units 命令允许我们列出所有特定类型单元。让我们检查 systemctl 管理的套接字:

The list-units command allows us to list all the units of a certain type. Let’s check for sockets managed by systemd −

[root@localhost]# systemctl list-units --type=socket
UNIT                         LOAD     ACTIVE     SUB     DESCRIPTION
avahi-daemon.socket          loaded   active  running    Avahi mDNS/DNS-SD Stack Activation Socket
cups.socket                  loaded   active  running    CUPS Printing Service Sockets
dbus.socket                  loaded   active  running    D-Bus System Message Bus Socket
dm-event.socket              loaded   active  listening  Device-mapper event daemon FIFOs
iscsid.socket                loaded   active  listening  Open-iSCSI iscsid Socket
iscsiuio.socket              loaded   active  listening  Open-iSCSI iscsiuio Socket
lvm2-lvmetad.socket          loaded   active  running    LVM2 metadata daemon socket
lvm2-lvmpolld.socket         loaded   active  listening  LVM2 poll daemon socket
rpcbind.socket               loaded   active  listening  RPCbind Server Activation Socket
systemd-initctl.socket       loaded   active  listening  /dev/initctl Compatibility Named Pipe
systemd-journald.socket      loaded   active  running    Journal Socket
systemd-shutdownd.socket     loaded   active  listening  Delayed Shutdown Socket
systemd-udevd-control.socket loaded   active  running    udev Control Socket
systemd-udevd-kernel.socket  loaded   active  running    udev Kernel Socket
virtlockd.socket             loaded   active  listening  Virtual machine lock manager socket
virtlogd.socket              loaded   active  listening  Virtual machine log manager socket

现在让我们检查当前运行中的服务:

Now let’s check the current running services −

[root@localhost rdc]# systemctl list-units --type=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

is-active

is-active 命令是 systemctl 命令的一个示例,它旨在返回某个单元的状态信息。

The is-active command is an example of systemctl commands designed to return the status information of a unit.

[root@localhost rdc]# systemctl is-active ksm.service
active

cat

cat 是一个很少使用的命令。与其在 shell 中使用 cat 并键入单元文件的路径,不如直接使用 systemctl cat。

cat is one of the seldomly used command. Instead of using cat at the shell and typing the path to a unit file, simply use systemctl cat.

[root@localhost]# systemctl cat firewalld
# /usr/lib/systemd/system/firewalld.service
[Unit]
Description=firewalld - dynamic firewall daemon
Before=network.target
Before=libvirtd.service
Before = NetworkManager.service
After=dbus.service
After=polkit.service
Conflicts=iptables.service ip6tables.service ebtables.service ipset.service
Documentation=man:firewalld(1)

[Service]
EnvironmentFile = -/etc/sysconfig/firewalld
ExecStart = /usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
ExecReload = /bin/kill -HUP $MAINPID
# supress to log debug and error output also to /var/log/messages
StandardOutput = null
StandardError = null

Type = dbus
BusName = org.fedoraproject.FirewallD1

[Install]
WantedBy = basic.target
Alias = dbus-org.fedoraproject.FirewallD1.service

[root@localhost]#

现在我们已经更详细地了解了 systemctl 和 systemd,让我们使用它们来管理 cgroups 或控制组中的资源。

Now that we have explored both systemd and systemctl in more detail, let’s use them to manage the resources in cgroups or control groups.