Course contentsShow
← Linux
Lesson 199 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Self-Healing Loop Design

Create scripts that monitor their own health and restart on failure.

In this lesson, you will learn how to design "self-healing" scripts that detect when their primary tasks fail and automatically trigger a recovery process to maintain continuous uptime.

The "Watchdog" Pattern

In Linux, scripts often run as background processes. If an external factor—like a network flicker or a memory limit—causes the script to crash, your system stops functioning. A self-healing loop design uses a "Watchdog" pattern. Instead of running your logic directly, you create a parent script that spawns your worker process and monitors its status using the exit codes we discussed in earlier modules.

Putting it into a Loop

The core concept is to wrap your worker logic in an infinite while loop that checks the process status. If the process terminates, the loop restarts it.

Consider this simplified structure:

#!/bin/bash

# The service to keep alive
SERVICE="./my_critical_task.sh"

while true; do
    echo "Starting critical task..."
    $SERVICE
    
    # If the script above crashes, execution resumes here
    echo "Task failed! Waiting 5 seconds before restart..."
    sleep 5
done

Why This Works

The while true loop ensures that the execution environment never terminates. By including a sleep command, you prevent a "crash loop" where a script that fails instantly consumes 100% of your CPU by restarting thousands of times per second.

In a production environment, you might combine this with your knowledge of Systemd Service Integration. While Systemd has built-in Restart=always directives, understanding the manual loop design is vital for situations where you lack root access or need to manage ephemeral sub-processes within a shell environment.

By trapping the failure of your primary script within this loop, you effectively turn an unstable execution into a resilient service that recovers from faults without human intervention. This is the foundation of high-availability scripting: never assume a process will stay alive, and always provide a mechanism for it to resurrect itself.

Key Takeaway: Self-healing design relies on wrapping your logic in a persistent loop that detects process exit codes and triggers a recovery wait-and-restart cycle.