In this lesson, you will learn how to create a resilient shell wrapper that detects when a program crashes and automatically restarts it to maintain system continuity.
The Problem: Unexpected Exits
Processes in Linux often terminate prematurely due to segmentation faults, memory exhaustion, or transient errors. If you are running a long-term service or a critical background job, a single crash could halt your workflow entirely. Because the shell acts as the parent process, it receives an exit status code when its child dies. We can harness this to build a "supervisor" loop.
Building a Resilient Wrapper
To ensure a process keeps running, we use a while loop that monitors the exit status of our target command. In shell scripting, the $? variable stores the exit code of the most recently executed command. If the code is non-zero, it indicates a crash or failure.
Here is a simple pattern to keep a process alive:
#!/bin/bash
while true; do
echo "Starting application..."
./my_critical_app
# Check the exit status
if [ $? -ne 0 ]; then
echo "Application crashed! Restarting in 2 seconds..."
sleep 2
else
echo "Application exited normally. Stopping."
break
fi
done
How it Works
- The Loop: The
while trueconstruct keeps the script running indefinitely. - The Execution: We run
./my_critical_appin the foreground. The shell pauses execution of the script until the application finishes. - Status Check: Immediately after the application terminates, we inspect
$?. A value of0is standard for "success." Any other number suggests an error occurred. - Recovery: By adding a short
sleepcommand, we prevent the system from entering a "tight loop" (spinning uncontrollably if the application crashes instantly every time), which saves CPU cycles and allows for manual intervention if needed.
This simple wrapper architecture transforms a fragile process into a self-healing service, ensuring your task remains active even when the underlying software encounters issues.
Key Takeaway: By wrapping a process in a conditional while loop, you can detect non-zero exit codes and force a restart, effectively creating a basic automated recovery mechanism.