[Linux] [systemd] Linux服务启动顺序查询(systemd-analyze)

ChatGPT生成(时间)

🧩 一、systemd-analyze —— 查看启动耗时分析(最常用)

1️⃣ 查看整体启动时间

systemd-analyze

输出示例:

Startup finished in 3.424s (kernel) + 5.813s (userspace) = 9.237s 
graphical.target reached after 5.812s in userspace.

含义:

  • kernel:内核加载耗时
  • userspace:用户空间启动耗时(systemd 启动服务等)
  • total:总耗时

2️⃣ 查看各个服务的启动耗时

systemd-analyze blame

输出示例:

3.456s NetworkManager.service
2.987s systemd-udevd.service
1.345s snapd.service
...

按耗时排序,可以看到哪个服务最慢。


3️⃣ 查看启动关键路径(Critical Chain)

systemd-analyze critical-chain

输出类似:

graphical.target @5.812s
└─multi-user.target @5.811s
  └─network.target @5.342s
    └─NetworkManager.service @1.876s +3.456s

显示依赖关系和每个阶段的时间。


4️⃣ 生成图形化启动分析报告(可导出 HTML)

systemd-analyze plot > boot.svg
  • 生成一个 SVG 文件,可以用浏览器打开。
  • 里面会有时间轴、每个服务启动时间、依赖关系等,非常直观。

5️⃣ 生成详细 JSON 报告

systemd-analyze dump > boot_dump.txt

或者:

systemd-analyze --json=pretty > boot.json

这个包含了所有启动单元(unit)的详细信息,适合做脚本分析或监控。


🧩 二、journalctl -b —— 查看开机日志

想看启动时的日志(包括错误、启动顺序):

journalctl -b

如果你想看上一次启动:

journalctl -b -1

如果要导出:

journalctl -b > boot_log.txt

🧩 三、dmesg —— 查看内核启动信息

只想看内核初始化阶段的时间:

dmesg --ctime > dmesg_boot.log

✅ 总结对比表

目的 命令 输出格式 可导出
查看启动总耗时 systemd-analyze 纯文本
查看服务启动耗时排行 systemd-analyze blame 纯文本
查看依赖链与关键路径 systemd-analyze critical-chain 纯文本
图形化启动时序图 systemd-analyze plot > boot.svg SVG 图 ✅(HTML可视化)
日志级别启动信息 journalctl -b 日志
查看内核启动时间 dmesg 日志
posted on 2025-10-31 13:36  风惊庭前叶  阅读(3)  评论(0)    收藏  举报