RedHat系统中init 6前预检测脚本失效问题排查
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 1has a critical logic flaw. Since&&takes higher precedence than||, even whensanity.shsucceeds (returns 0), the flow is messy—but more importantly, when the check fails, theechoruns followed immediately byexit 1, which cuts off execution. This "one-liner" condition is brittle; using an explicitif/then/elseblock is far safer and easier to debug.Infinite recursive loop
Your function ends withinit $@, which calls your custominitfunction again instead of the system's actualinitcommand. Since you've overridden theinitname with your function, this creates an infinite recursion that will crash the shell instead of triggering a reboot. You need to call the systeminitusing its absolute path (usually/sbin/init) to bypass your custom function.Case-sensitive command typo
Sleep 1uses a capital "S"—Bash commands are case-sensitive, soSleepisn't a valid command. This will throw an error and stop execution before the reboot step ever runs. Use lowercasesleep 1instead.Invalid comparison operator
The–eqin your conditional uses an en dash (the longer, typographical dash) instead of a regular hyphen-eq. Bash doesn't recognize–eqas 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'sinitcommand.
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

