📜  Unix 中的 systemctl

📅  最后修改于: 2022-05-13 01:57:28.385000             🧑  作者: Mango

Unix 中的 systemctl

systemctl用于检查和控制“systemd”系统和服务管理器的状态。

systemd是类 Unix 操作系统(大多数发行版,不是全部)的系统和服务管理器。当系统启动时,创建的第一个进程,即PID=1的init进程,是启动用户空间服务的systemd系统。

句法 :

systemctl [OPTIONS...] COMMAND [NAME...]

systemctl 命令的部分输出

systemctl


输出:



snap-core-3887.mount                                        loaded active mounted   Mount unit for core
snap-simplescreenrecorder-1.mount                           loaded active mounted   Mount unit for simplescreenrecorder
sys-fs-fuse-connections.mount                               loaded active mounted   FUSE Control File System
sys-kernel-config.mount                                     loaded active mounted   Configuration File System
sys-kernel-debug.mount                                      loaded active mounted   Debug File System
acpid.path                                                  loaded active running   ACPI Events Check
cups.path                                                   loaded active running   CUPS Scheduler
systemd-ask-password-plymouth.path                          loaded active waiting   Forward Password Requests to Plymouth Directory Watch
systemd-ask-password-wall.path                              loaded active waiting   Forward Password Requests to Wall Directory Watch
systemd-networkd-resolvconf-update.path                     loaded active waiting   Trigger resolvconf update for networkd DNS
init.scope                                                  loaded active running   System and Service Manager
session-1.scope                                             loaded active running   Session 1 of user mandeep

另一个例子,如果你想挂起你的系统,使用下面的命令

systemctl suspend
or 
systemctl -i suspend

systemctl 选项:

-t, --type= : the argument in this case should be comma-separated list of unit types such as service and socket.
--state= :  this is used to show only specified states, for example --state=failed, will show only failed units
-a, --all:  show all units, regardless of their states
-r, --recursive:  while listing units, show units of local containers also
--show-types:  it shows types of sockets along with showing sockets.
--job-mode : this option controls how to deal with already queued jobs in case of queuing a new job.
--fail : similar to --job-mode=fail
-i, --ignore-inhibitors:  When you request system shutdown or sleep state, it ignores inhibitor locks
-q, --quite :  it suppresses printing of results of various commands and the hints about the truncated lines
--no-block : don't wait synchronously for process to finish.
--user : talk to service manager of calling user, instead of system.
--no-wall : don't send wall message before power-off, halt or reboot.
-f, --force : when used with enable override any conflicting symlinks.
--message= : When used with halt, poweroff, reboot, kexec, you can display a short message specifying reason of operation.
-n, --lines : When used with status, controls the no. of journal lines to show.
-H, --host= : execute operation remotely. 

例子 :
1)

systemctl --fail

它将显示处于失败状态的作业,类似于 –job-mode=fail

输出:
这是完整输出的一部分

UNIT                                                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                           loaded active waiting   Arbitrary Executable File Formats File System Automount Poi
sys-devices-pci0000:00-0000:00:02.0-drm-card0-card0\x2deDP\x2d1-intel_backlight.device loaded active plugged   /sys/devices/pci0000:00/0000:00:
sys-devices-pci0000:00-0000:00:03.0-sound-card0.device      loaded active plugged   Haswell-ULT HD Audio Controller
sys-devices-pci0000:00-0000:00:1b.0-sound-card1.device      loaded active plugged   8 Series HD Audio Controller
sys-devices-pci0000:00-0000:00:1c.0-0000:01:00.0-net-enp1s0.device loaded active plugged   RTL8101/2/6E PCI Express Fast/Gigabit Ethernet contr
sys-devices-pci0000:00-0000:00:1c.3-0000:02:00.0-net-wlp2s0.device loaded active plugged   Wireless 3160 (Dual Band Wireless AC 3160)
sys-devices-pci0000:00-0000:00:1d.0-usb1-1\x2d1-1\x2d1.5-1\x2d1.5:1.0-bluetooth-hci0.device loaded active plugged   /sys/devices/pci0000:00/000
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged   ST500LT012-1DG142 ESP
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda10.device loaded active plugged   ST500LT012-1DG142 10
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda11.device loaded active plugged   ST500LT012-1DG142 PBR_Image

2) -i 选项,有时在您尝试执行命令时发生,例如systemctl suspend ,它不会被执行,当某些操作(例如复制项目或刻录 CD)时,在该系统中使用抑制锁来执行此操作时可能会发生确保这些操作成功,您可以使用 -i 选项覆盖这些锁

例如:

systemctl -i suspend

systemctl 命令:



1) list-units :列出所有的单位

list-units [PATTERN...]

例子:

systemctl list-units

部分输出:

sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda9.device loaded active plugged   ST500LT012-1DG142 9
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged   ST500LT012-1DG142
sys-devices-platform-coretemp.0-hwmon-hwmon1.device         loaded active plugged   /sys/devices/platform/coretemp.0/hwmon/hwmon1
sys-devices-platform-dell\x2dlaptop-leds-dell::kbd_backlight.device loaded active plugged   /sys/devices/platform/dell-laptop/leds/dell::kbd_ba
sys-devices-platform-serial8250-tty-ttyS0.device            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-platform-serial8250-tty-ttyS1.device            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS10.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS10
sys-devices-platform-serial8250-tty-ttyS11.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS11
sys-devices-platform-serial8250-tty-ttyS12.device           loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS12

2) list-sockets :它列出了按监听地址排序的套接字地址。

systemctl list-sockets

输出:

LISTEN                          UNIT                            ACTIVATES
/dev/rfkill                     systemd-rfkill.socket           systemd-rfkill.service
/run/acpid.socket               acpid.socket                    acpid.service
/run/snapd-snap.socket          snapd.socket                    snapd.service
/run/snapd.socket               snapd.socket                    snapd.service
/run/systemd/fsck.progress      systemd-fsckd.socket            systemd-fsckd.service
/run/systemd/initctl/fifo       systemd-initctl.socket          systemd-initctl.service
/run/systemd/journal/dev-log    systemd-journald-dev-log.socket systemd-journald.service
/run/systemd/journal/socket     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/stdout     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/syslog     syslog.socket                   rsyslog.service
/run/udev/control               systemd-udevd-control.socket    systemd-udevd.service
/run/uuidd/request              uuidd.socket                    uuidd.service
/var/run/avahi-daemon/socket    avahi-daemon.socket             avahi-daemon.service
/var/run/cups/cups.sock         cups.socket                     cups.service
/var/run/dbus/system_bus_socket dbus.socket                     dbus.service
audit 1                         systemd-journald-audit.socket   systemd-journald.service
kobject-uevent 1                systemd-udevd-kernel.socket     systemd-udevd.service

同样,您可以使用list-timers列出按它们接下来经过的时间排序的计时器单元。

3) start, stop, reload, restart :顾名思义,它们分别用于启动、停止、重新加载或重新启动服务

start PATTERN...
reload PATTERN...
stop PATTERN...
restart PATTERN...

4) is-active PATTERN... :检查是否有任何指定的服务处于活动状态,类似的 is-failed 可用于确定处于失败状态的单元。

5) status [PATTERN…|PID…]] :显示关于一个或多个单元的简洁的运行时状态信息

systemctl status

对于输出,请参见下图

systemctl 状态输出



6) show [PATTERN…|JOB…] :显示一个或多个单位、工作或经理本身的属性。

systemctl show

输出:

Version=229
Features=+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMO
Architecture=x86-64
FirmwareTimestampMonotonic=0
LoaderTimestampMonotonic=0
KernelTimestamp=Tue 2018-02-13 08:07:08 IST
KernelTimestampMonotonic=0
InitRDTimestampMonotonic=0
UserspaceTimestamp=Tue 2018-02-13 08:07:13 IST
UserspaceTimestampMonotonic=5502424
FinishTimestamp=Tue 2018-02-13 08:07:53 IST
FinishTimestampMonotonic=45214960
SecurityStartTimestamp=Tue 2018-02-13 08:07:13 IST

7) enable NAME...:启用一个或多个单元文件或其实例,同理disable可用于禁用。

8)暂停

systemctl supend

它可以用来悬挂系统,同样存在对休眠混合睡眠重启关机停机选项

9) kexec :它关闭系统并通过 kexec 重新启动它。

我在本文中提到了最常用的命令和选项,更多你可以访问 systemctl 的 linux 手册页。

– 曼迪普·辛格