使用 systemd-analyze
命令可以查看 Linux 系统在启动过程中每个服务的耗时情况, 方便我们排查是哪个环节导致系统启动缓慢, 以下是整理的常用命令参数和效果. 例子中一下子就可以定位到是 gssproxy.service
服务启动耗时过长.
systemd-analyze blame
Print list of running units ordered by time to init
$ systemd-analyze blame
29.912s gssproxy.service
11.280s update-motd.service
4.250s network.service
426ms cloud-init-local.service
302ms cloud-init.service
276ms dracut-initqueue.service
247ms cloud-config.service
206ms cloud-final.service
201ms systemd-udev-settle.service
183ms initrd-switch-root.service
175ms postfix.service
158ms lvm2-monitor.service
systemd-analyze critical-chain
Print a tree of the time critical chain of units
$ 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.
graphical.target @30.350s
└─multi-user.target @30.350s
└─getty.target @30.350s
└─getty@tty1.service @30.350s
└─systemd-user-sessions.service @30.322s +2ms
└─remote-fs.target @30.322s
└─remote-fs-pre.target @30.322s
└─nfs-client.target @30.322s
└─gssproxy.service @409ms +29.912s
└─basic.target @409ms
└─sockets.target @409ms
└─dbus.socket @409ms
└─sysinit.target @408ms
└─systemd-update-utmp.service @405ms +2ms
└─auditd.service @381ms +24ms
└─systemd-tmpfiles-setup.service @376ms +4ms
└─local-fs.target @375ms
└─local-fs-pre.target @299ms
└─lvm2-monitor.service @140ms +158ms
└─lvm2-lvmetad.service @162ms
└─lvm2-lvmetad.socket @138ms
└─-.slice
systemd-analyze plot
Output SVG graphic showing service initialization
systemd-analyze plot > ~/plot.svg
导出的 svg 文件复制到本地电脑上就可以直接用浏览器打开看了