Skip to content

When Things Break

Something is wrong. Someone is in the chat asking “is the server down?” Don’t reboot yet. Rebooting destroys evidence, and then it’ll happen again next Tuesday and you’ll know exactly nothing more than you do now.

Terminal window
uptime # load average and how long since the last "fix"
df -h # disk space
df -i # inodes. The disk can be "full" with plenty of space left.
free -h # memory
dmesg -T | tail -50 # kernel complaints: OOM kills, disk errors, the scary stuff
systemctl --failed

That covers most of it. Most outages are a full disk, memory exhaustion, or a service that died and nobody noticed.

Terminal window
df -h
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail

Keep drilling down into the biggest directory. Or install ncdu and save yourself the typing. Usual suspects, in order of how often they’ve ruined my evening:

  1. Logs. /var/log, especially an app that logs every request at DEBUG.
  2. The journal. journalctl --disk-usage, then sudo journalctl --vacuum-size=500M.
  3. Docker. docker system df. See Docker Storage.
  4. Old kernels / package caches. sudo apt autoremove && sudo apt clean.
  5. Someone’s backup-final-v2.tar.gz in their home directory. It’s always there.

“I deleted the file but the space didn’t come back”

Section titled ““I deleted the file but the space didn’t come back””

A process still has it open. The space isn’t freed until it closes the file.

Terminal window
sudo lsof +L1 # open files with zero links, i.e. deleted but still held

Restart the process that owns it. Or, if you can’t, truncate it through /proc:

Terminal window
sudo truncate -s 0 /proc/<PID>/fd/<FD>

Next time, truncate the log (: > app.log) instead of rm-ing it out from under a running process. And set up logrotate, which exists precisely so you don’t have to do any of this.

df -h shows space, df -i shows 100%. Millions of tiny files somewhere. Session files, mail queue, a cache directory nobody cleans. Find it:

Terminal window
sudo find / -xdev -type d -size +1M 2>/dev/null # directories so big their listing alone is >1MB
Terminal window
free -h
ps aux --sort=-%mem | head
dmesg -T | grep -i -E "killed process|out of memory"
journalctl -k | grep -i oom

If the OOM killer has been visiting, the kernel picked something to shoot. Often your database, because it was the biggest thing in the room.

Load average is the number of processes running or waiting, including waiting on disk. A load of 8 on an 8-core box is busy. A load of 40 is a problem. A load of 40 with idle CPUs means everything is stuck waiting on I/O.

Terminal window
nproc # how many cores, for context
top # check %wa (I/O wait) in the CPU line
iostat -x 2 # from sysstat. Watch %util on your disks.
iotop # who's doing the I/O

High %wa points at disk: a slow NFS mount, a dying drive, or a backup job someone scheduled during business hours.

Terminal window
ip a # addresses
ip r # routes. "No default route" explains a lot.
ss -tulpn # what's listening
curl -v http://localhost:8080 # does it work locally? If yes, it's the network/firewall/proxy.
dig example.com # DNS. It's always DNS. Even when it isn't, check DNS.
resolvectl status # on systemd-resolved systems, which DNS server is actually used

The process for “the website is down”:

  1. Is the process running? (systemctl status)
  2. Is it listening? (ss -tulpn)
  3. Does curl localhost work?
  4. Does it work from another machine? If not: firewall (sudo ufw status, sudo nft list ruleset), security groups, the proxy.
  5. Does the domain resolve to the right IP? Someone changed DNS. Someone always changed DNS.

If you really have to reboot, grab what you can first:

Terminal window
dmesg -T > /root/dmesg-$(date +%F-%H%M).txt
ps auxf > /root/ps-$(date +%F-%H%M).txt

Then reboot, then check systemctl --failed, then write down what happened. Future you won’t remember. Future you is even more tired than you are.