首页 帮助中心 美国云服务器 Linux中systemd服务和单元的解读
Linux中systemd服务和单元的解读
时间 : 2024-12-13 12:21:53 编辑 : 华纳云 阅读量 : 227

systemctl是systemd的一个实用程序,可以控制systemd系统和服务管理器,systemd是系统管理守护进程、实用程序和库的集合,能替代system v init守护进程。systemd可作为类UNIX系统的中央管理和配置平台。下文华纳云和大家分享关于systemd控制系统和服务的方法。

先检查系统上是否安装systemd,如已安装查询出当前安装版本:

#systemctl --version 
systemd 215
+ PAM + AUDIT + SELINUX + IMA + SYSVINIT + LIBCRYPTSETUP + GCRYPT + ACL + XZ -SECCOMP -APPARMOR

输出可看到已经安装了systemd 215版。然后继续检查systemd和systemctl二进制文件和库的安装位置:

# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

检查systemd是否正在运行:

# ps -eaf | grep [s]ystemd 
root 1 0 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root 444 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 469 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-udevd
root 555 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-logind
dbus 556 1 0 16:27 ? 00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

Systemd 是 Linux 系统中的一个系统和服务管理器,它作为 PID(进程ID)为1 的父守护进程运行,负责启动系统上的其他所有进程。在 Linux 中,你可以通过 ps 命令查看进程信息。以下是 ps 命令的一些选项:

-e:显示所有进程。

-a:显示所有终端的进程,不包括会话领导者。

-f:显示完整格式的列表,包括进程的更多详细信息。

当你在 ps 命令中同时使用 -e、-a 和 -f 选项时(即 ps -eaf),你会得到一个包含所有进程详细信息的完整列表。

至于方括号,它们在 grep 命令中用于定义字符类,以便进行模式匹配。例如,[abc] 会匹配任何一个在方括号中的字符(a、b 或 c)。在处理文本搜索时,方括号可以帮助你指定一个字符集合,grep 会搜索包含这个集合中任何一个字符的行。

分析systemd的启动过程:

# systemd-analyze
Startup finished in 487ms (kernel) + 2.776s (initrd) + 20.229s (userspace) = 23.493s

分析启动时各个进程所花费的时间。

# systemd-analyze blame 
8.565s mariadb.service
7.991s webmin.service
6.095s postfix.service
4.311s httpd.service
3.926s firewalld.service
3.780s kdump.service
3.238s tuned.service
1.712s network.service
1.394s lvm2-monitor.service
1.126s systemd-logind.service
....

分析启动时的关键链:

# systemd-analyze critical-chain
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.
multi-user.target @20.222s
└─mariadb.service @11.657s +8.565s
└─network.target @11.168s
└─network.service @9.456s +1.712s
└─NetworkManager.service @8.858s +596ms
└─firewalld.service @4.931s +3.926s
└─basic.target @4.916s
└─sockets.target @4.916s
└─dbus.socket @4.916s
└─sysinit.target @4.905s
└─systemd-update-utmp.service @4.864s +39ms
└─auditd.service @4.563s +301ms
└─systemd-tmpfiles-setup.service @4.485s +69ms
└─rhel-import-state.service @4.342s +142ms
└─local-fs.target @4.324s
└─boot.mount @4.286s +31ms
└─systemd-fsck@dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\x2d85d19608096
└─dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\x2d85d196080964.device @4

Systemctl 接受服务(.service)、挂载点(.mount)、套接字(.socket)和设备(.device)作为任务。

列出全部可用任务:

# systemctl list-unit-files
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-sys-fs-binfmt_misc.mount               static  
sys-fs-fuse-connections.mount               static  
sys-kernel-config.mount                     static  
sys-kernel-debug.mount                      static  
tmp.mount                                   disabled
brandbot.path                               disabled
.....

列出所有正在运行的任务:

# systemctl list-units
UNIT                                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount           loaded active waiting   Arbitrary Executable File Formats File Syste
sys-devices-pc...0-1:0:0:0-block-sr0.device loaded active plugged   VBOX_CD-ROM
sys-devices-pc...:00:03.0-net-enp0s3.device loaded active plugged   PRO/1000 MT Desktop Adapter
sys-devices-pc...00:05.0-sound-card0.device loaded active plugged   82801AA AC'97 Audio Controller
sys-devices-pc...:0:0-block-sda-sda1.device loaded active plugged   VBOX_HARDDISK
sys-devices-pc...:0:0-block-sda-sda2.device loaded active plugged   LVM PV Qzyo3l-qYaL-uRUa-Cjuk-pljo-qKtX-VgBQ8
sys-devices-pc...0-2:0:0:0-block-sda.device loaded active plugged   VBOX_HARDDISK
sys-devices-pl...erial8250-tty-ttyS0.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-pl...erial8250-tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-pl...erial8250-tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-pl...erial8250-tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
sys-devices-virtual-block-dm\x2d0.device    loaded active plugged   /sys/devices/virtual/block/dm-0
sys-devices-virtual-block-dm\x2d1.device    loaded active plugged   /sys/devices/virtual/block/dm-1
sys-module-configfs.device                  loaded active plugged   /sys/module/configfs
...

列出所有失败的任务:

# systemctl --failed
UNIT          LOAD   ACTIVE SUB    DESCRIPTION
kdump.service loaded failed failed Crash recovery kernel arming
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

检查某个单元(cron.service)是否已启用?

# systemctl is-enabled crond.service
enabled

检查单元或服务是否正在运行?

# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
Active: active (running) since Tue 2015-04-28 16:27:55 IST; 34min ago
Main PID: 549 (firewalld)
CGroup: /system.slice/firewalld.service
└─549 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Apr 28 16:27:51 tecmint systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 28 16:27:55 tecmint systemd[1]: Started firewalld - dynamic firewall daemon.
华纳云 推荐文章
美国Linux云服务器取消fstab开机挂载配置的步骤 Linux系统交换空间监控的方法 Linux bmon命令常用方法的分享 宝塔Linux面板的功能、支持系统及安装指南 Linux中ifconfig命令详解与实战指南 Linux mknod 命令的使用示例与详解 2025年最适合学生使用的Linux发行版推荐 Linux系统磁盘和文件系统管理 教你如何用特定命令轻松管理Linux进程? Linux/Ubuntu中的CPU使用率或利用率的查看方法
客服咨询
7*24小时技术支持
技术支持
渠道支持