You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RedHat系统中init 6前预检测脚本失效问题排查

Issues with Your Custom init Function

Let's break down the problems in your script that are preventing init 6 from working correctly:

  • Broken conditional logic precedence
    The line [[ $? –eq 0 ]] && echo "reboot will performed" || echo "reboot will not performed because sanity.sh exit with error" && exit 1 has a critical logic flaw. Since && takes higher precedence than ||, even when sanity.sh succeeds (returns 0), the flow is messy—but more importantly, when the check fails, the echo runs followed immediately by exit 1, which cuts off execution. This "one-liner" condition is brittle; using an explicit if/then/else block is far safer and easier to debug.

  • Infinite recursive loop
    Your function ends with init $@, which calls your custom init function again instead of the system's actual init command. Since you've overridden the init name with your function, this creates an infinite recursion that will crash the shell instead of triggering a reboot. You need to call the system init using its absolute path (usually /sbin/init) to bypass your custom function.

  • Case-sensitive command typo
    Sleep 1 uses a capital "S"—Bash commands are case-sensitive, so Sleep isn't a valid command. This will throw an error and stop execution before the reboot step ever runs. Use lowercase sleep 1 instead.

  • Invalid comparison operator
    The –eq in your conditional uses an en dash (the longer, typographical dash) instead of a regular hyphen -eq. Bash doesn't recognize –eq as a valid numeric comparison operator, so this condition will always evaluate to false, completely breaking your sanity check.

  • Missing actual reboot trigger
    Even if the sanity check passed, your current code only prints a message and recurses—it never actually initiates the reboot with the system's init command.


Fixed Version of the Function

Here's a corrected version that addresses all these issues:

init () {
    # Run the sanity check script and capture its exit status
    /home/sanity.sh
    local exit_status=$?

    if [[ $exit_status -eq 0 ]]; then
        echo "Reboot will be performed"
        sleep 1
        # Call the system's init command directly to avoid recursion
        /sbin/init "$@"
    else
        echo "Reboot will not be performed because sanity.sh exited with error code $exit_status"
        exit 1
    fi
}

This version uses clear if/else logic, captures the exit status explicitly, calls the system init by absolute path, fixes the sleep command, uses the correct comparison operator, and ensures the reboot is triggered when the check passes.

内容的提问来源于stack exchange,提问作者enodmilvado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:29:56