Linux系统 自启稳定性测试 在 Linux 系统的运维和开发工作中,功能迭代、硬件更新或系统配置调整后,都可能藏着一个风险点,就是有时候会出现开机启动异常。最头疼的是,在排查解决后,这种问题通常不会每次都出现。
可能手动重启个三五次都正常,但跑现场时,就起不来了。如果靠人手一遍遍重启记录,不仅累,还测不出那种”100次里失败1次”的隐藏bug。
本文分享一套自动化测试方案:通过脚本让机器自主完成循环重启、状态检测和日志记录。
脚本编写 整套方案由核心执行脚本、服务配置、参数配置、结果分析、安装、卸载脚本组成。如果仅测试自启,可以直接挪用。如果需要针对特定功能测试,可自行添加测试命令和参数配置。
核心脚本 reboot_test.sh 负责实现 “重启计数 - 状态检测 - 日志记录 - 循环重启” 的完整流程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #!/bin/bash BASE_DIR="/root/reboot_stress" STATE_DIR="$BASE_DIR /state" LOG_DIR="$BASE_DIR /logs" COUNT_FILE="$STATE_DIR /count" LOG_FILE="$LOG_DIR /reboot.log" source "$BASE_DIR /config.conf" mkdir -p "$STATE_DIR " "$LOG_DIR " if [ ! -f "$COUNT_FILE " ]; then echo 0 > "$COUNT_FILE " echo "===== Reboot Stress Test START $(date) =====" >> "$LOG_FILE " fi COUNT=$(cat "$COUNT_FILE " ) if [ "$COUNT " -ge "$MAX_REBOOT " ]; then echo "===== Reboot Stress Test FINISH $(date) =====" >> "$LOG_FILE " bash "$BASE_DIR /analyze.sh" systemctl disable reboot-test.service exit 0 fi COUNT=$((COUNT + 1 )) echo "$COUNT " > "$COUNT_FILE " TEST_STATUS=$($CHECK_CMD 2>/dev/null) if echo "$TEST_STATUS " | grep -q "$OK_KEYWORD " ; then TEST_RESULT="OK" else TEST_RESULT="ERROR" fi echo "[$(date) ] BOOT OK | Round=$COUNT | TEST=$TEST_RESULT " >> "$LOG_FILE " sleep "$BOOT_DELAY " reboot
系统服务配置 reboot-test.service 通过 systemd 服务配置实现核心脚本的 “开机自启”。
1 2 3 4 5 6 7 8 9 10 11 12 [Unit] Description=Reboot Stress Test After=multi-user.target [Service] Type=simple ExecStart=/root/reboot_stress/reboot_test.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target
配置文件 config.conf 配置重启次数、测试命令和结果判断关键字。我提供了很多示例,可自行选择。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 # 重启次数 MAX_REBOOT=100 # 每次启动后等待秒数 BOOT_DELAY=15 # 显卡状态检测命令(按你实际工具改) # 示例:hwmc_cli show array # 网络连通性测试 # CHECK_CMD="ping -c 3 8.8.8.8" # PCIe设备检测 # CHECK_CMD="lspci | grep -E 'VGA compatible controller|Ethernet controller|USB controller'" # 显示设备检测 # CHECK_CMD="xrandr --query | grep -q ' connected' && echo 'Display OK' || echo 'Display FAIL'" # 触摸屏检测 # CHECK_CMD="ls /dev/input/event* | grep -i touch" # 存储设备检测 # CHECK_CMD="lsblk -d | grep -q disk && echo 'Storage OK' || echo 'Storage FAIL'" # USB设备检测 # CHECK_CMD="lsusb | wc -l && echo 'USB devices detected'" # 内存检测 # CHECK_CMD="free -m | grep -q Mem && echo 'Memory OK'" # CPU检测 # CHECK_CMD="nproc && lscpu | grep -q 'CPU(s):'" # 组合测试命令 # CHECK_CMD="ping -c 1 8.8.8.8 > /dev/null && echo 'Network: OK'; lspci | grep -q 'VGA' && echo 'GPU: OK'; ls /dev/input/event* | grep -q touch && echo 'Touchscreen: OK'" # 当前使用的测试命令 CHECK_CMD="echo OK" # 状态正常判断关键字(按实际修改改) OK_KEYWORD="OK"
分析脚本 analyze.sh 测试完成后自动统计并生成可视化的测试报告。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/bin/bash BASE_DIR="/root/reboot_stress" LOG_FILE="$BASE_DIR /logs/reboot.log" REPORT="$BASE_DIR /logs/summary.txt" TOTAL=$(grep -c "BOOT OK" "$LOG_FILE " ) TEST_ERR=$(grep -c "ERROR" "$LOG_FILE " ) { echo "========== Reboot Stress Test Summary ==========" echo "Test End Time : $(date) " echo "Total Boots : $TOTAL " echo "TEST Errors : $TEST_ERR " echo if [ "$TOTAL " -eq 0 ]; then echo "RESULT: Test Invalid (No successful boots)" elif [ "$TEST_ERR " -eq 0 ]; then echo "RESULT: PASS" else echo "RESULT: FAIL" fi echo "===============================================" } | tee "$REPORT "
安装部署脚本 install.sh 一键完成测试环境的部署。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/bin/bash BASE_DIR="/root/reboot_stress" if [ ! -d "$BASE_DIR " ]; then mkdir -p "$BASE_DIR " cp -r ./* "$BASE_DIR /" fi chmod +x "$BASE_DIR " /*.shcp "$BASE_DIR /reboot-test.service" /etc/systemd/system/systemctl daemon-reexec systemctl daemon-reload systemctl enable reboot-test.service echo "Deploy finished." echo "Run: /root/reboot_stress/reboot_test.sh"
卸载清理脚本 uninstall.sh 一键清理所有测试相关文件和服务配置,避免残留文件影响系统环境。
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash systemctl disable reboot-test.service systemctl stop reboot-test.service rm -f /etc/systemd/system/reboot-test.servicerm -rf /root/reboot_stresssystemctl daemon-reload echo "Reboot stress test removed."
整套方案已测试通过,可自行修改参数和测试工具,实现各种测试需求。将所有文件放在一个目录下,并执行install.sh脚本即可完成安装。