The Problem
At 1:54 in the morning, my home router lost its link to the fiber box for a moment. It logged the hiccup and said that the internet connection was “restored”. Then it passed nothing at all for the next six hours and twenty minutes.
I was on the other side of the country. My monitoring worked perfectly: every alarm went red in the same second. But it could not do anything about the problem. In the end, the fix came when the house woke up. A family member unplugged the router for thirty seconds.
That outage was the second identical one in eight days. After it, my goal for the lab changed. The old goal was “tell me when something breaks”. The new goal is “fix what you know how to fix, and then tell me what you did.”
Monitoring tells you about a problem. Self-healing removes the problem. You want both, but they are two different things.
What “Self-Healing” Means
A self-healing script is a small loop with five parts:
- Detect a real symptom, not a proxy for one.
- Wait long enough to be sure it is not a blip.
- Act, and start with the gentlest fix that could work.
- Escalate only if the gentle fix did not work, and only as far as is safe.
- Report what happened, as soon as a report can get out.
None of these steps is clever. The guardrails around each step make the script trustworthy. I explain them below. First, this is how it looks in my lab today.
The Ladder I Built After That Night
The new router watchdog runs on one of my servers every minute. It has one job: the failure from that night, router alive, internet dead.
Detection uses two vantage points. The router itself tries to reach three well-known addresses, and a server behind it does the same. The internet counts as dead only when both fail and the router still answers on the local network. One failed ping proves nothing. When two independent views fail together, the outage is real.
If the router does not answer on the LAN at all, the script does nothing. It cannot fix that remotely, so it only records the event for later.
The escalation ladder:
| Time dead | Action |
|---|---|
| 3 minutes | restart the router’s WAN connection (gentle) |
| 7 minutes | reboot the router (at most once per 6 hours, 3 per day) |
| after that | assume a provider outage: retry the gentle restart every 30 minutes, no more reboots |
After recovery: a router reboot wipes some custom firewall rules. So the script applies them again immediately and sends me a summary. The summary gives the length of the outage and the steps it took.
It cannot message me during the outage, because there is no internet to send through. That is fine. I do not need a play-by-play. I need a receipt.
With this script, that night would have been a seven-minute outage, not a six-hour one.
Self-Healing Is Already Everywhere (Once You Look)
When I looked, I found that I had built small healers for weeks. I did not call them that.
- A rules guard. The stock router firmware drops my custom rules (IoT isolation, DNS interception) at every reboot. A tiny job checks every five minutes. It applies anything that is missing and then tells me.
- An exit-node watchdog. My household traffic goes out through the fastest of my cloud nodes. If that exit dies, traffic moves to another one. If none work, traffic goes straight out the home connection. The watchdog ignores small speed differences on purpose (more on that below).
- A travel router that fails over. I left a small router at a relative’s house. It sends traffic through my cloud nodes. Every five seconds it checks for real internet. After two failures it moves to the next healthy node. If all of them fail, it goes straight out the local connection. Two minutes after the preferred node recovers, it moves back.
- Media filing that cleans up after itself. A hook files downloads into the right library. A sweep every few hours catches anything the hook missed. A daily sweep removes leftovers that nothing owns any more. It parks anything uncertain for 30 days before it deletes it.
- Storage that gives space back. The containers on one node live on a small 64 GB flash chip. When I deleted files inside a container, the host kept their space until something trimmed them. A weekly trim took that node from 87% full to 54%, and keeps it there.
- Boot order and restart rules. Database and DNS start first. Identity and egress start next. The login portal and the reverse proxy start last. Critical services restart automatically if they crash. Every host runs a self-check after it boots and reports the result.
- Upgrades that roll themselves back. I updated the networking client on the travel router. After the swap, the upgrade script checked itself for 60 seconds. If the client was not running and signed in to my network, the script restored the factory copy by itself. That is important when the device is across the country and is your only way in.
The Guardrails (This Is the Important Part)
A healer with bad judgment is only a new way to cause outages. Every one of mine follows the same rules.
1. Detect the symptom, from more than one place. “The router reports connected” was the lie that cost me six hours. Test what users see (can traffic get out?) from two independent viewpoints.
2. Wait before you act. A one-second blip does not deserve a router reboot. Three minutes of confirmed failure does.
3. Start gentle, escalate slowly. Restart a connection before you reboot a device. Reboot a device before you touch anything else.
4. Cap the blast radius. Use rate limits, cooldowns and daily maximums. The script can reboot my router at most once every six hours. If the problem is my provider, a hundred router reboots will not help. The script knows that it must back off.
5. Choose stability over the “best” choice. My exit-node watchdog used to flap between two cloud nodes when their latency crossed. Every switch meant a new public IP and dropped calls. Now it keeps a working exit until that exit fails two checks in a row. It also switches if another node is at least 40 ms faster for five checks in a row.
Healing that causes churn is no healing.
6. Fail closed when you cannot see. If the data for a healer is missing or stale, the healer waits or stops. It does not guess.
7. Undo is part of the fix. Upgrades keep the previous version and roll back automatically. Cleanups park things for 30 days before they delete them.
8. Always tell a human. Self-healing that hides problems is worse than no self-healing, because you never fix the root cause. Every healer in my lab reports what it did. Two identical outages in eight days is not something a script should quietly absorb forever. It is a cable that I must check.
Test It by Breaking It
The scariest healer is one that nobody has seen work. So I test mine in the only honest way. I break things on purpose, safely.
For the travel router, I blocked its traffic for a short time on the cloud node it used. The block removed itself after 30 to 90 seconds, so a mistake could not leave the router broken. Then I watched what happened. That test found two real bugs:
- Detection took 40 to 80 seconds, not 10. The checks ran in one-minute slots. When one slot ran long, the next one saw a lock and quietly exited. Between them, nobody watched. The fix was a proper always-running service.
- Its “restart me if I die” check could never fire. It looked for its own name in the process list, and it always found itself. The fix was a pattern that cannot match its own command.
After the fixes, the router went from “exit node dead” to “back online through another node” in 21 seconds. A notification came to my phone.
For the router watchdog, I could not safely break my home internet while I was away. So it has a dry-run mode with a simulated clock. I fed it a fake 73-minute outage and checked every decision it would make. It restarted at 3 minutes, did one reboot at 7, did gentle retries after that, and then sent the summary. The router did not change.
A healer that you did not test is only a hope.
The Limits
“Self-healing everything” is my goal. I do not claim it is done. A script cannot fix some things:
- Power. If a server is off, no script on it can turn it on again. That needs hardware. Examples are a UPS, the BIOS setting that powers machines on after an outage, or a remote-controlled plug.
- A frozen device. If the router itself hangs, nothing on the network can talk to it. The only remote fix is an independent way in, like a second network path or a smart plug.
- Your provider. If the outage is upstream, the right move is to back off and wait. Do not keep “fixing”.
A good design knows where healing stops. For those cases, the job changes. Make the problem obvious, and make the fix easy for whoever is home.
A Template You Can Steal
Every healer I write starts from the same skeleton:
#!/bin/bash
# heal-<thing>: run from a timer every minute
STATE=/var/lib/heal-thing; mkdir -p $STATE
exec 9>$STATE/lock; flock -n 9 || exit 0 # one at a time
healthy() { <test the real symptom, from two places>; }
if healthy; then
if [ -f $STATE/down_since ]; then # just recovered: tell a human
notify "Healed after $(( ($(date +%s) - $(cat $STATE/down_since)) / 60 )) min: $(cat $STATE/actions 2>/dev/null)"
rm -f $STATE/down_since $STATE/actions
fi
heartbeat # so you notice if the healer itself dies
exit 0
fi
[ -f $STATE/down_since ] || date +%s > $STATE/down_since
down=$(( $(date +%s) - $(cat $STATE/down_since) ))
if [ $down -ge 180 ] && ! grep -q gentle $STATE/actions 2>/dev/null; then
gentle_fix; echo gentle >> $STATE/actions
elif [ $down -ge 420 ] && ! grep -q strong $STATE/actions 2>/dev/null && rate_limit_ok; then
strong_fix; echo strong >> $STATE/actions
fi # beyond that: back off, don't thrash
Fill in the blanks and add a dry-run mode. Then break the thing on purpose and watch it heal.
Takeaways
- Monitoring tells you that something is broken. Self-healing fixes the boring failures that you already understand. Build both.
- Detect the real symptom from two vantage points. A device that says “connected” gives no proof.
- Wait, then climb a ladder: gentle fix first, stronger fix later, then back off.
- Put hard limits on every fix: cooldowns, daily caps, and a plan for problems that you cannot fix.
- Choose stability over the optimum. A healer that flaps causes outages. It does not cure them.
- Make undo part of every fix: rollbacks, and parking before deletion.
- Always report what the healer fixed. Hidden healing hides root causes.
- Test by breaking things safely, with automatic undo. Or simulate time with a dry run.
- Know the limits. Power, frozen hardware and upstream outages need hardware or humans.
- “Self-healing everything” is the direction. It is not a finish line.