From 1d20d95b266400677c1c50424501170b2a447e79 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 14 Sep 2026 09:45:47 -0400 Subject: [PATCH] More updates to the watchdog. --- scripts/i3_watchdog.sh | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/scripts/i3_watchdog.sh b/scripts/i3_watchdog.sh index d03c7f8..c094040 100755 --- a/scripts/i3_watchdog.sh +++ b/scripts/i3_watchdog.sh @@ -19,6 +19,7 @@ timeoutSeconds=2 # Consider i3 locked if it doesn't respond within 2 seconds pidFile="${HOME}/.config/i3/i3_watchdog.pid" logFile="/tmp/i38-i3-watchdog-${UID}.log" maxLogBytes=262144 +displayFailureLimit=3 rotate_log() { local logSize @@ -53,6 +54,20 @@ check_health() { fi } +check_x_display() { + timeout 1 xdotool getmouselocation > /dev/null 2>&1 +} + +cleanup_pid_file() { + local recordedPid + + [[ -r "$pidFile" ]] || return 0 + read -r recordedPid < "$pidFile" + if [[ "$recordedPid" == "$$" ]]; then + rm -f -- "$pidFile" + fi +} + # Kill any existing watchdog instances if [[ -f "$pidFile" ]]; then oldPid=$(cat "$pidFile") @@ -64,20 +79,28 @@ fi # Write our PID echo "$$" > "$pidFile" +trap cleanup_pid_file EXIT log_message "I38 Watchdog started (PID: $$)" lastSuccessTime=$(date +%s) consecutiveFailures=0 +consecutiveDisplayFailures=0 while true; do sleep "$checkInterval" - # Check if i3 is still running - if ! pgrep -x i3 > /dev/null; then - log_message "i3 process not found - exiting watchdog" - rm -f "$pidFile" - exit 0 + # An i3 restart leaves X available and launches a replacement watchdog via + # exec_always. Only exit when the X session itself is no longer available. + if check_x_display; then + consecutiveDisplayFailures=0 + else + ((consecutiveDisplayFailures++)) + if [[ $consecutiveDisplayFailures -ge $displayFailureLimit ]]; then + log_message "X display unavailable for $consecutiveDisplayFailures consecutive checks - exiting watchdog" + exit 0 + fi + continue fi # Try to communicate with i3 IPC AND check X focus