Automation, from a shell script to a workflow · Lesson 5 of 8 · about 35 minutes

From runbook to script: automating the manual fix

A runbook is the list of steps someone follows by hand when something breaks, often at 3am. This lesson follows a real one for a full disk, watches one of its steps do nothing at all, works out why, and turns the runbook into a script that looks before it acts, asks a person to confirm, checks its own fix, and keeps a record.

By the end you'll know

  • What a runbook is, and why following one by hand under pressure goes wrong
  • Why deleting a big log file can free no space at all
  • How to turn a runbook into a script that checks first, asks before acting, and checks its own work
  • How to make a person confirm a dangerous step, and when a tool should be allowed to skip that
  • When a runbook is worth automating, and when it isn't

Before you start

  • You need: Vagrant and VMware Fusion or VirtualBox, as in Linux Lesson 7, and the practice folder: git clone https://github.com/Kwamib/automation-practice.git, or git pull in it.
  • This lesson uses df and du from Linux Lesson 10, and processes and signals from Linux Lesson 6.
  • Where commands run: on your own computer to build the server, then on the server, called bookshop-1.
  • The output below is from a real run on November 18, 2026, at about 1:20pm in New York (18:20 UTC).

The problem: steps followed by hand, under pressure

A runbook is a written list of steps for a known problem: "when this alert fires, do this". Every operations team has them. They're a good thing: the person on call at 3am doesn't have to work it out from scratch.

They also go wrong in predictable ways:

  • A tired person skips a step, or runs it on the wrong server.
  • A step is copied wrong, a path or a name mistyped.
  • A step is simply wrong, and nobody noticed, because it usually seemed to work.
  • Nobody writes down what they did, so the next person, and the incident review, have to guess.

Work like this, done by hand, over and over, the same way each time, is what engineers call toil. Turning a runbook into a script is one of the most common ways to reduce it, and job listings ask for it by name: "runbook automation", "reducing toil", "self-healing".

This lesson takes one runbook, for a full disk, follows it by hand, and turns it into a script.

The server

On your own computer:

BASH
cd ~/Apps/automation-practice/runbook
vagrant up
vagrant ssh
  • cd .../runbook: Vagrant uses the Vagrantfile in this folder, which builds a server called bookshop-1.
  • vagrant up: builds it and runs setup.sh. That makes a small disk for the bookshop's logs at /srv/bookshop-logs, starts the app writing a log line every second, and installs the runbook and the script.
  • vagrant ssh: logs you in.

(On the server)

BASH
systemctl status bookshop --no-pager | head -3
df -h /srv/bookshop-logs
tail -n 3 /srv/bookshop-logs/app.log
cat ~/RUNBOOK.md
OUTPUT
● bookshop.service - The bookshop app (a stand-in that writes a log line every second)
     Loaded: loaded (/etc/systemd/system/bookshop.service; enabled; preset: enabled)
     Active: active (running) since Wed 2026-11-18 17:41:02 UTC; 39min ago
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M  128K  168M   1% /srv/bookshop-logs
2026-11-18T18:20:32Z INFO GET /books 200 12ms
2026-11-18T18:20:33Z INFO GET /books 200 12ms
2026-11-18T18:20:34Z INFO GET /books 200 12ms
  • systemctl status ... | head -3: the app is running. --no-pager prints it all at once, and head -3 keeps the first 3 lines.
  • df -h DISK: how full a disk is. -h shows sizes like 172M instead of raw numbers. The 200 MB disk shows as 172 MB, because the file system uses some space for itself.
  • tail -n 3 FILE: the last 3 lines of the log: one line a second, as a web app logs its requests.

The runbook, RUNBOOK.md:

OUTPUT
# Runbook: the bookshop logs disk is full

**Alert:** "/srv/bookshop-logs is over 80% full" on the bookshop server.
**Impact:** when it reaches 100%, the app can't write its log, and errors go unrecorded.

1. Log in to the server, and check how full the disk is:

       df -h /srv/bookshop-logs

2. Find what's using the space:

       sudo du -ah /srv/bookshop-logs | sort -rh | head

3. If `app.log` is huge, someone has probably left debug logging on. Switch it off:

       sudo systemctl kill --kill-whom=main -s USR2 bookshop

4. Delete the big log:

       sudo rm /srv/bookshop-logs/app.log

5. Check the disk again with `df -h /srv/bookshop-logs`. It should be nearly empty.

6. Post in the incident channel that it's fixed.

It looks reasonable. One of its steps is wrong.

The incident

Someone switches on debug logging to chase a problem, and forgets to switch it off. Debug logging records everything, and here it writes about 10 MB a second:

BASH
sudo systemctl kill --kill-whom=main -s USR1 bookshop
sleep 30
df -h /srv/bookshop-logs
journalctl -u bookshop --no-pager | tail -3
OUTPUT
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M  168M     0 100% /srv/bookshop-logs
Nov 18 18:21:15 bookshop-1 bookshop-app[2508]: bookshop-app: can't write to /srv/bookshop-logs/app.log
Nov 18 18:21:15 bookshop-1 bookshop-app[7286]: head: error writing 'standard output': No space left on device
Nov 18 18:21:15 bookshop-1 bookshop-app[7285]: yes: standard output: Broken pipe
  • systemctl kill -s USR1 bookshop: sends the app a signal, a short message a program can react to. -s USR1 picks which one; this app treats USR1 as "switch on debug logging", and USR2 as "switch it off". Despite the name, kill here only sends a signal. --kill-whom=main sends it to the app itself, not the small programs it starts.
  • journalctl -u bookshop: the app's own messages.

In under 30 seconds, the disk is full, and the app can no longer write its log: "No space left on device". Anything that goes wrong now goes unrecorded.

Follow the runbook

Steps 2, 3 and 4, exactly as written:

BASH
sudo du -ah /srv/bookshop-logs | sort -rh | head
sudo systemctl kill --kill-whom=main -s USR2 bookshop
sudo rm /srv/bookshop-logs/app.log
df -h /srv/bookshop-logs
ls -la /srv/bookshop-logs
OUTPUT
168M	/srv/bookshop-logs/app.log
168M	/srv/bookshop-logs
16K	/srv/bookshop-logs/lost+found
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M  168M     0 100% /srv/bookshop-logs
total 24
drwxr-xr-x 3 bookshop bookshop  4096 Nov 18 18:21 .
drwxr-xr-x 3 root     root      4096 Nov 18 17:41 ..
drwx------ 2 root     root     16384 Nov 18 17:41 lost+found
  • du -ah FOLDER: how much space each file uses. -a lists files as well as folders, and -h gives readable sizes.
  • sort -rh: biggest first. -h understands sizes like 168M, and -r reverses the order. head: the first 10 lines.
  • rm FILE: the runbook's step 4.
  • ls -la: everything in the folder, hidden files too (-a), in long format (-l).

The log is gone, and the disk is still 100% full. du found a 168 MB app.log; ls shows it's deleted; df says nothing changed. The runbook's step 5, "it should be nearly empty", is simply wrong. A person following it at 3am would be stuck, or would start deleting other things.

Where did the space go?

On Linux, deleting a file removes its name, not its contents. The space is only freed when no program has the file open any more. The app opened its log when it started, and still has it open: it's writing into a file with no name, which nobody can see, and which still fills the disk.

Linux keeps a list of every file each running program has open, under /proc. Look for files on our disk marked as deleted:

BASH
sudo find /proc/[0-9]*/fd -lname '/srv/bookshop-logs/*(deleted)' -printf '%p -> %l\n'
OUTPUT
/proc/2508/fd/1 -> /srv/bookshop-logs/app.log (deleted)
/proc/7465/fd/1 -> /srv/bookshop-logs/app.log (deleted)
  • /proc/[0-9]*/fd: each running program has a numbered folder in /proc, and its fd folder holds a link for every file it has open. [0-9]* matches every program's number.
  • -lname 'PATTERN': keep links whose target matches; here, files on our disk ending (deleted).
  • -printf '%p -> %l\n': print each one as "the link -> what it points to". \n ends the line.

Two programs are holding the deleted log open: the app (process 2508), and a sleep it started, which got a copy of its open files. Until they let go, the space stays used. Two things free it:

  • Restart the program, so it closes the file, then opens a new one.
  • Don't delete the log in the first place. Empty it instead, with truncate -s 0 FILE, which sets its size to 0. The program keeps the same file open and carries on writing, from the start.
Deleting an open file is taking the name off a room that someone's still working in. The room is still there, still full of their boxes, and they're still adding more. It just doesn't appear on the floor plan any more, so nobody can find it to clear it. The room is only freed when the last person leaves.

The script

fix-disk-full, the runbook as a script, installed at /usr/local/sbin/fix-disk-full. Also on GitHub (opens in a new tab):

BASH
#!/usr/bin/env bash
# fix-disk-full: the "bookshop logs disk is full" runbook, as a script.
#
#   fix-disk-full                  look, and show what it would do (changes nothing)
#   fix-disk-full --apply          fix it, after you confirm by typing the server's name
#   fix-disk-full --apply --yes    fix it without asking (for use by other tools)
#
# Exit codes: 0 fixed, or nothing to fix; 1 it tried and the fix didn't work;
# 2 called wrongly, not confirmed, or refused.
set -euo pipefail

MOUNT=${MOUNT:-/srv/bookshop-logs}
APP_LOG=${APP_LOG:-$MOUNT/app.log}
SERVICE=${SERVICE:-bookshop}
THRESHOLD=${THRESHOLD:-80}
INCIDENTS=${INCIDENTS:-/var/lib/incidents}
RECORD=${RECORD:-/var/log/runbook.log}
who_ran=${SUDO_USER:-$(id -un)}

say() { printf '%s fix-disk-full: %s\n' "$(date -u +%FT%TZ)" "$*" >&2; }
usage() { sed -n '4,6p' "$0" | sed -E 's/^# ?//' >&2; exit 2; }

apply=0 yes=0
for a in "$@"; do
	case "$a" in
		--apply) apply=1 ;;
		--yes) yes=1 ;;
		*) usage ;;
	esac
done
[ "$(id -u)" -eq 0 ] || { say "run me with sudo"; exit 2; }
mountpoint -q "$MOUNT" || { say "refusing: $MOUNT isn't a mounted disk; is this the right server?"; exit 2; }

used() { df --output=pcent "$MOUNT" | tail -n 1 | tr -dc '0-9'; }
# Files that were deleted while a program still had them open: their space
# isn't freed until the program closes them.
held_open() { find /proc/[0-9]*/fd -lname "$MOUNT/*(deleted)" 2>/dev/null || true; }

# 1. Look before touching anything.
before=$(used)
say "step 1: look"
df -h "$MOUNT" >&2
say "the biggest files:"
du -ah "$MOUNT" 2>/dev/null | sort -rh | head -n 4 | sed 's/^/    /' >&2 || true
held=$(held_open)
if [ -n "$held" ]; then
	say "space held by deleted files that are still open:"
	while read -r fd; do
		pid=${fd#/proc/}; pid=${pid%%/*}
		printf '    %s (%s bytes), held open by process %s (%s)\n' "$(readlink "$fd")" "$(stat -L -c %s "$fd" 2>/dev/null || echo '?')" "$pid" "$(cat "/proc/$pid/comm" 2>/dev/null || echo '?')" >&2
	done <<<"$held"
fi

if [ "$before" -lt "$THRESHOLD" ] && [ -z "$held" ]; then
	say "nothing to fix: $MOUNT is $before% full, under the $THRESHOLD% line"
	exit 0
fi

# 2. Say what it's going to do.
say "step 2: the plan, for $MOUNT at $before% full"
say "    a. save the last 200 lines of $APP_LOG, for the incident notes"
say "    b. switch off the app's debug logging"
say "    c. empty $APP_LOG (truncate it; don't delete it)"
[ -z "$held" ] || say "    d. restart $SERVICE, so it lets go of deleted files"
say "    then check: under $THRESHOLD%, $SERVICE running, and still logging"
if [ "$apply" -eq 0 ]; then
	say "dry run: nothing was changed. Add --apply to do it."
	exit 0
fi

# 3. A person confirms, unless told not to ask.
if [ "$yes" -eq 0 ]; then
	if [ ! -t 0 ]; then
		say "refusing: nobody's here to confirm. Run it in a terminal, or add --yes if a tool is running it on purpose."
		exit 2
	fi
	read -r -p "Type this server's name ($(hostname)) to go ahead: " answer
	if [ "$answer" != "$(hostname)" ]; then
		say "not confirmed ('$answer' isn't $(hostname)); nothing was changed"
		exit 2
	fi
fi

# 4. Fix it, one step at a time.
stamp=$(date -u +%Y%m%dT%H%M%SZ)
notes="$INCIDENTS/$stamp-disk-full"
install -d -m 700 "$notes"
say "a. saving the last 200 lines of the log to $notes"
tail -n 200 "$APP_LOG" >"$notes/app-log-tail.txt" 2>/dev/null || true
say "b. switching off debug logging"
systemctl kill --kill-whom=main -s USR2 "$SERVICE"
if [ -e "$APP_LOG" ]; then
	say "c. emptying $APP_LOG"
	truncate -s 0 "$APP_LOG"
else
	say "c. $APP_LOG isn't there (deleted?); restarting $SERVICE will start a new one"
	held=${held:-restart}
fi
if [ -n "$held" ]; then
	say "d. restarting $SERVICE"
	systemctl restart "$SERVICE"
fi

# 5. Check it worked: free space, the app running, and new log lines arriving.
sleep 3
after=$(used)
ok=1
if [ "$after" -ge "$THRESHOLD" ]; then say "CHECK FAILED: $MOUNT is still $after% full"; ok=0; fi
if ! systemctl is-active --quiet "$SERVICE"; then say "CHECK FAILED: $SERVICE isn't running"; ok=0; fi
if [ ! -s "$APP_LOG" ]; then say "CHECK FAILED: $SERVICE hasn't written to $APP_LOG since it was emptied"; ok=0; fi
df -h "$MOUNT" >&2

# 6. Record what happened, for the incident notes and for whoever's next.
result=$([ "$ok" -eq 1 ] && echo "fixed" || echo "NOT fixed")
summary="$result: $MOUNT went from $before% to $after% full; run by $who_ran on $(hostname)"
printf '%s\n' "$summary" >"$notes/summary.txt"
printf '%s %s\n' "$(date -u +%FT%TZ)" "$summary" >>"$RECORD"
say "$summary"
say "notes saved in $notes"
[ "$ok" -eq 1 ]

It follows the runbook's shape, with what the runbook was missing added at each step.

It checks it's on the right server. mountpoint -q /srv/bookshop-logs is true only if that disk is mounted there. On any other server, the script refuses before doing anything.

It looks before it does anything. It shows df, the biggest files, and files that were deleted but are still open, using the same /proc search as above. If the disk is under the THRESHOLD (80%) and nothing is held open, it says "nothing to fix" and stops. A fix that runs when nothing's wrong can only make things worse.

It says what it will do, and changes nothing without --apply, like every script in this track.

It asks a person to confirm, by typing the server's name. Not y, which people type without reading. Typing the name means reading it, which is also the last chance to notice you're on the wrong server.

BASH
if [ ! -t 0 ]; then

[ -t 0 ] is true when the script's input is a terminal, meaning a person typing. If it isn't, nobody can be asked, so the script refuses, unless it's given --yes, a deliberate "a tool is running me on purpose".

It fixes it the right way. It saves the last 200 lines of the log for the incident notes, switches debug logging off, and empties the log with truncate -s 0 instead of deleting it. Only if something is holding a deleted file open does it restart the app.

It checks its own work. Is the disk under the threshold? Is the app running? Is it writing to the log again? A fix isn't done until it's checked.

It keeps a record. /var/log/runbook.log gets one line per run: what changed, and who ran it (SUDO_USER, the person behind sudo). The incident folder gets the log excerpt and a summary.

Run it after the manual attempt

The manual fix left the disk full, so run the script. The dry run first:

BASH
sudo fix-disk-full
OUTPUT
2026-11-18T18:21:54Z fix-disk-full: step 1: look
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M  168M     0 100% /srv/bookshop-logs
2026-11-18T18:21:54Z fix-disk-full: the biggest files:
    20K	/srv/bookshop-logs
    16K	/srv/bookshop-logs/lost+found
2026-11-18T18:21:54Z fix-disk-full: space held by deleted files that are still open:
    /srv/bookshop-logs/app.log (deleted) (175378432 bytes), held open by process 2508 (bash)
    /srv/bookshop-logs/app.log (deleted) (175378432 bytes), held open by process 7465 (sleep)
2026-11-18T18:21:54Z fix-disk-full: step 2: the plan, for /srv/bookshop-logs at 100% full
2026-11-18T18:21:54Z fix-disk-full:     a. save the last 200 lines of /srv/bookshop-logs/app.log, for the incident notes
2026-11-18T18:21:54Z fix-disk-full:     b. switch off the app's debug logging
2026-11-18T18:21:54Z fix-disk-full:     c. empty /srv/bookshop-logs/app.log (truncate it; don't delete it)
2026-11-18T18:21:54Z fix-disk-full:     d. restart bookshop, so it lets go of deleted files
2026-11-18T18:21:54Z fix-disk-full:     then check: under 80%, bookshop running, and still logging
2026-11-18T18:21:54Z fix-disk-full: dry run: nothing was changed. Add --apply to do it.

Look at "the biggest files": 20K. du can't see a deleted file, so it adds up to almost nothing on a full disk. A person would be baffled. The script found the 175 MB held open by the app (bash, since the app is a Bash script) and its sleep, and added step d, the restart, to its plan.

Confirming

BASH
sudo fix-disk-full --apply

The first try, answering yes:

OUTPUT
Type this server's name (bookshop-1) to go ahead: yes
2026-11-18T18:23:11Z fix-disk-full: not confirmed ('yes' isn't bookshop-1); nothing was changed

The second try, pasted together with the commands that came after it:

OUTPUT
Type this server's name (bookshop-1) to go ahead:
2026-11-18T18:23:57Z fix-disk-full: not confirmed ('' isn't bookshop-1); nothing was changed

The question took the next line of the paste as its answer, a blank one, before anyone could type. It refused, which is right: nobody had confirmed anything. Run anything that asks a question on its own, not in a pasted block. The third try, run on its own and answered by hand:

OUTPUT
Type this server's name (bookshop-1) to go ahead: bookshop-1
2026-11-18T18:24:37Z fix-disk-full: a. saving the last 200 lines of the log to /var/lib/incidents/20261118T182437Z-disk-full
2026-11-18T18:24:37Z fix-disk-full: b. switching off debug logging
2026-11-18T18:24:37Z fix-disk-full: c. /srv/bookshop-logs/app.log isn't there (deleted?); restarting bookshop will start a new one
2026-11-18T18:24:37Z fix-disk-full: d. restarting bookshop
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M   28K  168M   1% /srv/bookshop-logs
2026-11-18T18:24:40Z fix-disk-full: fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
2026-11-18T18:24:40Z fix-disk-full: notes saved in /var/lib/incidents/20261118T182437Z-disk-full

From 100% to 1% in 3 seconds. The script noticed the log had already been deleted, skipped emptying it, and restarted the app, which let go of the deleted file. The app started a new log straight away:

BASH
tail -n 2 /srv/bookshop-logs/app.log
OUTPUT
2026-11-18T18:39:04Z INFO GET /books 200 12ms
2026-11-18T18:39:05Z INFO GET /books 200 12ms

A fresh incident, handled by the script from the start

BASH
sudo systemctl kill --kill-whom=main -s USR1 bookshop
sleep 30
sudo fix-disk-full --apply
OUTPUT
2026-11-18T18:39:49Z fix-disk-full: the biggest files:
    168M	/srv/bookshop-logs/app.log
    168M	/srv/bookshop-logs
    16K	/srv/bookshop-logs/lost+found
...
2026-11-18T18:39:49Z fix-disk-full:     c. empty /srv/bookshop-logs/app.log (truncate it; don't delete it)
2026-11-18T18:39:49Z fix-disk-full:     then check: under 80%, bookshop running, and still logging
Type this server's name (bookshop-1) to go ahead: bookshop-1
2026-11-18T18:39:59Z fix-disk-full: a. saving the last 200 lines of the log to /var/lib/incidents/20261118T183959Z-disk-full
2026-11-18T18:39:59Z fix-disk-full: b. switching off debug logging
2026-11-18T18:39:59Z fix-disk-full: c. emptying /srv/bookshop-logs/app.log
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      172M   28K  168M   1% /srv/bookshop-logs
2026-11-18T18:40:02Z fix-disk-full: fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1

This time there was no restart at all. Nothing was deleted, so nothing was held open: emptying the log freed the space at once, and the app carried on writing into the same file. No downtime.

Now the incident notes from both fixes:

BASH
sudo sh -c 'tail -n 2 /var/lib/incidents/*/app-log-tail.txt; cat /var/lib/incidents/*/summary.txt'
OUTPUT
==> /var/lib/incidents/20261118T182437Z-disk-full/app-log-tail.txt <==

==> /var/lib/incidents/20261118T183959Z-disk-full/app-log-tail.txt <==
2026-11-18T18:39:32Z DEBUG request headers, cache state, query plan and everything else nobody reads
2026-11-18T18:39:32Z DEBUG request headers, cache statefixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
  • sudo sh -c '...': runs the commands in quotes in a new shell, as root. It's needed here because only root can look inside those folders, so the * has to be expanded by root, not by you.

Two things worth noticing:

  • The first incident's notes are empty. The log had been deleted by hand, so there was nothing left to save. Following the runbook destroyed the evidence of what went wrong.
  • The second excerpt's last line stops mid-word, at "cache state". That's where the disk filled up, in the middle of writing a line. (The summary appears on the same line because of it: that cut-off line has no line ending.) It's exactly what you'd want to see when working out what happened.

When no person is there

Scripts like this end up being run by other tools: a monitoring alert, a chat bot, a scheduled job. Three runs, on another fresh incident:

BASH
echo bookshop-1 | sudo fix-disk-full --apply; echo "exit $?"
sudo fix-disk-full --apply --yes; echo "exit $?"
sudo fix-disk-full --apply --yes; echo "exit $?"
sudo cat /var/log/runbook.log
OUTPUT
...
2026-11-18T18:40:58Z fix-disk-full: refusing: nobody's here to confirm. Run it in a terminal, or add --yes if a tool is running it on purpose.
exit 2
...
2026-11-18T18:41:01Z fix-disk-full: fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
exit 0
...
2026-11-18T18:41:01Z fix-disk-full: nothing to fix: /srv/bookshop-logs is 1% full, under the 80% line
exit 0
2026-11-18T18:24:40Z fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
2026-11-18T18:40:02Z fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
2026-11-18T18:41:01Z fixed: /srv/bookshop-logs went from 100% to 1% full; run by vagrant on bookshop-1
  • echo bookshop-1 | ...: feeds the answer in through a pipe, as a careless automation might, to get past the question. The input isn't a terminal, so the script refused. Exit 2.
  • --yes: the honest way for a tool to say "I'm running this on purpose". It fixed it. Exit 0.
  • The same command again, with the disk already fine: "nothing to fix". Safe to run twice.
  • echo "exit $?": prints the exit code of the command before it.
  • runbook.log: one line per fix, three fixes, each with who ran it.

The runbook, rewritten

The script doesn't replace the runbook; it shrinks it. What the runbook says now:

OUTPUT
# Runbook: the bookshop logs disk is full

1. Log in to the server, and look:   sudo fix-disk-full
2. If the plan looks right, fix it:  sudo fix-disk-full --apply
   (run it on its own, and type the server's name when asked)
3. If it says "CHECK FAILED", or anything surprising: stop, and escalate.
4. Post its summary line in the incident channel.
5. Find out why debug logging was on. The script fixes the disk, not the cause.

Fewer steps, none of them dangerous, and the knowledge of how to fix it safely lives in the script, where it's tested every time it runs.

Which runbooks to automate

Not every runbook should become a script. A rough guide:

If the runbook isThen
Followed often, the same way each timeScript it. This is toil.
Urgent, when mistakes are likelyScript it, with a confirmation, so a tired person can't skip a step
Rare, and mostly judgment ("decide whether to fail over")Keep it written, but script the looking: a command that gathers everything a person needs to decide
Dangerous and hard to undoScript the checks and the dry run first. Add --apply later, once the dry runs have been right for a while.

Automate in stages. Start with the looking (step 1, which is never dangerous), then the fix with a person confirming, and only then let a tool run it with --yes. Each stage earns trust for the next.

Clean up

BASH
exit

(On your own computer)

BASH
vagrant destroy -f

-f deletes the server without asking.

On a real server

  • Fix the cause, not just the symptom. Rotate logs with logrotate, which starts a new log file at a set size or age and keeps a few old ones, so one log can never fill a disk. Alert at 80%, long before 100%.
  • Keep the runbook and the script together, in Git, and change them together. A runbook that says one thing while the script does another is worse than either alone.
  • Practice. Break things on purpose, on a test server, and run the runbook. Teams call this a game day. It's how you find steps like "delete the log" before 3am does.
  • Record who ran what, as the script does, so incident reviews start from facts.
  • Be careful letting alerts run fixes with --yes. Limit how often, and alert a person when it's happened more than a couple of times. Spot the problem shows why.
  • For the bigger picture, alerting and routing are in the Monitoring and alerting track.

Spot the problem

Work out what's wrong before you read the explanation.

"The disk never fills up any more. Why are the logs useless?" A team wires fix-disk-full --apply --yes to their disk alert, so it runs automatically whenever the disk passes 80%. It works: nobody's been paged about the disk in weeks. Then an incident review needs yesterday's app logs, and there are only ever a few minutes of them. What's wrong? Debug logging was left on weeks ago, and the script has been emptying the log every 20 minutes ever since. It fixed the symptom so well that nobody saw the cause, and every log line older than a few minutes was thrown away. The fix: count the automatic runs, and alert a person when there have been more than one or two in a day. An automatic fix that keeps being needed is telling you something's wrong. And fix the cause: switch debug off, and rotate logs instead of emptying them.

Keep this

ToUse
How full is a disk?df -h PATH
What's using the space?du -ah PATH | sort -rh | head
Deleted files still held opensudo find /proc/[0-9]*/fd -lname '/PATH/*(deleted)'
Empty a file a program has opentruncate -s 0 FILE, not rm
Send a service's main process a signalsystemctl kill --kill-whom=main -s SIGNAL NAME
Is a disk mounted here?mountpoint -q PATH
Is a person typing (a terminal)?[ -t 0 ]
Ask a question, and read the answerread -r -p "Question: " answer
Who ran this, behind sudo?$SUDO_USER
Run commands as root, with root expanding the *sudo sh -c 'COMMANDS'

Where you are now

You followed a real-looking runbook by hand and watched its key step do nothing: the log was gone and the disk was still full, because the app still had it open. You found the hidden space under /proc, and turned the runbook into a script that checks it's on the right server, looks before it acts, asks a person to type the server's name, refuses when nobody's there to answer, fixes it without deleting anything, checks its own work, and keeps a record. Lesson 6 moves off servers altogether: jobs that run on a schedule on GitHub's computers, with no server of your own.

Check yourself

  1. The runbook deleted app.log, but the disk stayed 100% full. Why?

  2. How does the script free the space without restarting the app?

  3. After the manual delete, du said the folder used 20K on a 100% full disk. Why?

  4. Why does the script ask you to type the server's name rather than 'y'?

  5. echo bookshop-1 | sudo fix-disk-full --apply was refused. Why?

  6. Following the runbook by hand left the incident notes empty. Why?

Key terms

  • Runbook: a written list of steps for handling a known problem.
  • Toil: manual, repetitive work that a script could do, and that grows as the system grows.
  • Signal: a short message sent to a running program, which it can react to; USR1 and USR2 are free for programs to use as they like.
  • Deleted but open: a file whose name is gone while a program still has it open. Its space isn't freed until the program closes it.
  • Truncate: cut a file down to a size, here zero, without deleting it.
  • Confirmation: a question a person must answer before a risky step runs.
  • Game day: breaking something on purpose, in a safe place, to practice the response.

Go deeper

  • Earlier lessons: Disks, storage and memory for df, du and mounting, and Processes and signals for signals and /proc.
  • Eliminating Toil (opens in a new tab), a chapter of Google's Site Reliability Engineering book, on what toil is and why it's worth removing.
  • The Monitoring and alerting track, for the alerts that would start this runbook.
  • Manuals: man truncate, man logrotate, and man proc (look for /proc/pid/fd).
  • Try: delete the log by hand again, then, instead of the script, restart the app yourself with sudo systemctl restart bookshop, and watch df. Then write a logrotate rule for /srv/bookshop-logs/app.log that keeps it under 20 MB, and work out whether this app needs copytruncate, and why.