需启用healthd服务并配置systemd开机自启脚本,确保等待healthd完成扫描后再执行ksysreport生成报告。

你想让麒麟OS在每次开机时自动执行系统健康自检并生成诊断报告,而不是手动触发——这需要将健康检测能力与开机自启机制联动,且必须确保自检完成后再打包输出,否则会因服务未就绪而生成空报告。
启用healthd服务并配置日志级轮询
这是自检的基础,healthd是麒麟OS预置的守护进程,负责周期性采集硬件传感器、内核告警和驱动状态,不启用它,后续所有自动报告都无数据源。
打开终端,执行:sudo systemctl enable --now healthd.service
确认服务已运行:sudo systemctl is-active healthd,返回active才表示成功激活。若返回inactive或failed,请先检查/sys/class/hwmon/下是否存在硬件监控设备节点,缺失则需加载对应驱动模块。
查看最近一次扫描结果:sudo journalctl -u healthd -n 30 --no-pager | grep -E "(ERROR|WARN|FAIL)",有输出说明采集链路通畅。
创建自检+报告打包脚本
不能直接调用ksysreport,因为它默认不等待healthd完成最新一轮扫描,会抓取陈旧数据。必须写一个带等待逻辑的脚本。
新建文件:nano ~/bin/auto-health-report.sh
填入以下内容(注意替换/home/kylin为你的实际用户名):
#!/bin/bash
export PATH="/usr/local/bin:/usr/bin:/bin"
# 等待healthd完成至少一轮完整扫描
while ! sudo journalctl -u healthd -n 1 --no-pager | grep -q "Scan completed"; do
sleep 5
done
# 触发诊断报告生成,排除网络配置保护隐私
sudo ksysreport --full --exclude-network --output /home/kylin/Reports/system_health_$(date +%Y%m%d_%H%M%S).tar.gz
保存后赋予执行权限:chmod +x ~/bin/auto-health-report.sh
配置systemd系统级服务实现开机自动运行
用户级服务(--user)无法在login前执行,而healthd是系统级服务,必须用root权限启动脚本才能访问全部日志和硬件信息。图形界面添加.desktop方式会因X11未就绪而失败。
第一步:创建服务文件
sudo nano /etc/systemd/system/auto-health-report.service
第二步:填入配置(关键点:After=healthd.service,确保自检完成后再打包)
[Unit]
Description=开机自动健康自检与报告生成
After=healthd.service network.target
[Service]
Type=oneshot
User=root
ExecStart=/home/kylin/bin/auto-health-report.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
第三步:启用服务
sudo systemctl daemon-reload && sudo systemctl enable --now auto-health-report.service
第四步:验证是否注册成功
sudo systemctl list-unit-files | grep auto-health-report,状态应为enabled
验证首次开机执行效果
方法一:模拟重启流程(不需真实重启)
sudo systemctl start auto-health-report.service
方法二:检查报告是否生成
ls -l ~/Reports/system_health_*.tar.gz,文件大小应大于1MB,小于10MB;若为0字节,说明脚本中wait逻辑未生效,需检查journalctl -u healthd输出里是否有“Scan completed”字样。
方法三:查看服务执行日志
sudo journalctl -u auto-health-report.service -n 20 --no-pager,应包含“Scan completed”和“Generating report…”两段连续输出。











