grep -c prints zero and exits one, so every echo-0 fallback produced a two-line count

This commit is contained in:
Gmer4Lfe
2026-08-06 22:31:50 -04:00
parent 7b2f79e3ac
commit 1b46033584
11 changed files with 32 additions and 22 deletions
+13 -3
View File
@@ -320,9 +320,19 @@ purge_old_reboots() {
mv "${SYS_WATCHDOG_REBOOT_LOG}.tmp" "$SYS_WATCHDOG_REBOOT_LOG"
}
# Always emits exactly one number, because the caller compares it against the reboot limit and a
# malformed value there decides whether a looping server reboots again or shuts down.
#
# Two failure paths have to collapse to 0, and the old one-liner got both wrong. An EMPTY log —
# the normal state — made grep -c print 0 and exit 1, so "|| echo 0" fired as well and the
# function returned two lines. A MISSING log makes grep print nothing at all, so "|| true" alone
# would return the empty string. Either way the caller's [[ -ge ]] dies with a syntax error.
count_recent_reboots() {
purge_old_reboots
grep -c "." "$SYS_WATCHDOG_REBOOT_LOG" 2>/dev/null || echo 0
local n
n=$(grep -c "." "$SYS_WATCHDOG_REBOOT_LOG" 2>/dev/null || true)
n="${n//[^0-9]/}"
echo "${n:-0}"
}
log_reboot() {
@@ -584,7 +594,7 @@ echo "━━━ $ICON_REBOOT Stability Watchdog — $(date '+%Y-%m-%d %H:%M:%S')
# ── Kernel oops/BUG — kernel running with corrupted state ────────────────────────────────
if [[ "$SYS_WATCHDOG_CHECK_KERNEL_OOPS" == true ]]; then
PREV_OOPS=$(get_state_val "kernel_oops_count")
CURRENT_OOPS=$(dmesg 2>/dev/null | grep -cE "BUG:|kernel BUG|Oops:" || echo 0)
CURRENT_OOPS=$(dmesg 2>/dev/null | grep -cE "BUG:|kernel BUG|Oops:" || true)
CURRENT_OOPS="${CURRENT_OOPS//[^0-9]/}"; CURRENT_OOPS="${CURRENT_OOPS:-0}"
set_state_val "kernel_oops_count" "$CURRENT_OOPS"
@@ -756,7 +766,7 @@ echo "━━━ $ICON_REBOOT Stability Watchdog — $(date '+%Y-%m-%d %H:%M:%S')
# ── Zombie processes ─────────────────────────────────────────────────────────────────────
if [[ "$SYS_WATCHDOG_CHECK_ZOMBIES" == true ]]; then
ZOMBIE_COUNT=$(ps aux 2>/dev/null | awk '{print $8}' | grep -c "^Z$" || echo 0)
ZOMBIE_COUNT=$(ps aux 2>/dev/null | awk '{print $8}' | grep -c "^Z$" || true)
ZOMBIE_COUNT="${ZOMBIE_COUNT//[^0-9]/}"; ZOMBIE_COUNT="${ZOMBIE_COUNT:-0}"
TRIGGERED=false
[[ "$ZOMBIE_COUNT" -ge "$SYS_WATCHDOG_ZOMBIE_LIMIT" ]] && TRIGGERED=true