Skip to content

Debug scripts: Linux services & servers

3 paste-and-run bash scripts that collect what you need to find the error.

Index
3 items
  1. Paste the container ID, pod, service or URL into the box.
  2. Copy the script and paste it into a terminal on the machine (or where kubectl works).
  3. Read the report. It is also saved as debug-*.txt in the current folder to share.

Debug a systemd service that failed or keeps restarting

Read-only

Paste the service name (nginx, docker, myapp). Shows status, exit result and restart count, warnings and errors from the last hour, the unit file with overrides, the program path, its ports, failed dependencies and OOM kills.

(
S="<service>"
h() { printf '\n=== %s ===\n' "$*"; }

h "Status"
systemctl status "$S" --no-pager -l 2>&1 | head -25

h "State summary"
systemctl show "$S" --no-pager -p LoadState,ActiveState,SubState,Result,ExecMainStatus,ExecMainCode,NRestarts,MainPID,UnitFileState,ActiveEnterTimestamp,InactiveEnterTimestamp,FragmentPath

h "Warnings and errors in the last hour"
journalctl -u "$S" --since "1 hour ago" -p warning --no-pager 2>&1 | tail -40

h "Last 50 log lines"
journalctl -u "$S" -n 50 --no-pager 2>&1

h "Unit file and drop-in overrides"
systemctl cat "$S" --no-pager 2>&1

h "Does the program exist and can it run?"
BIN=$(systemctl show "$S" -p ExecStart --value | sed -n 's/.*path=\([^ ;]*\).*/\1/p' | head -1)
if [ -n "$BIN" ]; then ls -l "$BIN"; file "$BIN" 2>/dev/null; else echo "no ExecStart found"; fi

h "Ports the service listens on"
PID=$(systemctl show "$S" -p MainPID --value)
if [ "${PID:-0}" != 0 ]; then sudo ss -tulpn | grep "pid=$PID," || echo "PID $PID listens on no ports"; else echo "not running"; fi

h "Memory, CPU and tasks"
systemctl show "$S" --no-pager -p MemoryCurrent,MemoryMax,CPUUsageNSec,TasksCurrent,TasksMax

h "Failed units on this machine"
systemctl --failed --no-pager

h "OOM kills in the last day"
journalctl -k --since "1 day ago" --no-pager 2>/dev/null | grep -iE 'out of memory|killed process' | tail -10 || echo "none"
) 2>&1 | tee "debug-service-$(date +%Y%m%d-%H%M%S).txt"
What it checks
  1. Statusactive / failed / activating (auto-restart), main PID and the last log lines
  2. State summaryResult (exit-code, signal, timeout, core-dump), exit status, restart count, enabled or not, unit file path
  3. Warnings and errorspriority warning and worse from the service's journal in the last hour
  4. Last 50 log lineseverything the service logged most recently
  5. Unit file and overridesExecStart, User, Environment, Restart settings, including drop-ins in /etc/systemd/system/<name>.service.d/
  6. Program checkthe ExecStart binary exists, is executable, and the right type
  7. Portswhat the running process listens on (Address already in use means another process has the port)
  8. Memory, CPU and tasksusage against MemoryMax / TasksMax limits
  9. Failed unitsother services that failed (dependencies)
  10. OOM killswhether the kernel killed it for lack of memory

Server health check (why is this box slow or broken?)

Read-only

No input needed. One report with load vs CPUs, memory and swap, disks and inodes over 85%, top CPU and memory processes, failed services, recent errors, OOM kills, stuck processes and time sync.

(
h() { printf '\n=== %s ===\n' "$*"; }

h "Host"
hostnamectl 2>/dev/null | grep -E 'hostname|Operating System|Kernel|Virtualization' || uname -a
uptime

h "Load vs CPUs (load above the CPU count means work is queuing)"
echo "CPUs: $(nproc)   load 1/5/15 min: $(cut -d' ' -f1-3 /proc/loadavg)"

h "Memory and swap"
free -h

h "Disks over 85% full"
df -hP -x tmpfs -x devtmpfs -x overlay -x squashfs | awk 'NR == 1 || int($5) >= 85'
h "Inodes over 85% used"
df -iP -x tmpfs -x devtmpfs -x overlay -x squashfs | awk 'NR == 1 || int($5) >= 85'

h "Top 10 CPU processes"
ps -eo pid,user,%cpu,%mem,etime,cmd --sort=-%cpu | head -11 | cut -c1-160
h "Top 10 memory processes"
ps -eo pid,user,%mem,rss,etime,cmd --sort=-rss | head -11 | cut -c1-160

h "Stuck (D state) and zombie processes"
ps -eo pid,stat,etime,cmd | awk 'NR == 1 || $2 ~ /^[DZ]/' | head -20

h "Failed services"
systemctl --failed --no-pager 2>/dev/null

h "Errors since boot (latest 30)"
journalctl -b -p err --no-pager 2>/dev/null | tail -30

h "OOM kills (last day)"
journalctl -k --since "1 day ago" --no-pager 2>/dev/null | grep -iE 'out of memory|killed process' | tail -10 || echo "none"

h "Disk I/O wait and swapping (5 seconds)"
vmstat 1 5

h "Time sync"
timedatectl 2>/dev/null | grep -iE 'synchronized|NTP|Time zone'
) 2>&1 | tee "debug-health-$(hostname)-$(date +%Y%m%d-%H%M%S).txt"
What it checks
  1. HostOS, kernel, virtualization and uptime (a recent reboot is a clue)
  2. Load vs CPUsload average compared with the number of CPUs
  3. Memory and swapavailable memory and swap in use
  4. Disks and inodesonly filesystems 85% or fuller, for space and for file count
  5. Top processesthe biggest CPU and memory users
  6. Stuck and zombie processesD = waiting on disk/NFS (hung storage), Z = zombie (parent not reaping)
  7. Failed servicessystemd units in failed state
  8. Errors since booterror-level messages from every service and the kernel
  9. OOM killsprocesses killed because memory ran out
  10. vmstatwa column = waiting on disk, si/so = swapping, r = run queue
  11. Time syncan unsynchronized clock breaks TLS, tokens and log correlation

Disk full: find what is using the space

Read-only

Paste a path (usually /, /var or the full mount). Shows usage and inodes, the biggest folders and files, deleted files still held open, and the usual suspects: logs, journal, Docker, package caches.

(
P="<path>"
h() { printf '\n=== %s ===\n' "$*"; }

h "Usage and inodes of $P"
df -hT "$P"; df -i "$P"

h "Biggest folders (2 levels, same filesystem)"
sudo du -xh --max-depth=2 "$P" 2>/dev/null | sort -rh | head -25

h "Files over 100 MB"
sudo find "$P" -xdev -type f -size +100M -printf '%s\t%p\n' 2>/dev/null | sort -rn | head -20 | numfmt --field=1 --to=iec

h "Deleted files still held open (space is freed only when the process restarts)"
sudo lsof -nP +L1 2>/dev/null | awk 'NR == 1 || $7 > 10485760' | head -20

h "Logs"
sudo du -sh /var/log/* 2>/dev/null | sort -rh | head -10
journalctl --disk-usage 2>/dev/null

h "Docker"
command -v docker >/dev/null && docker system df 2>/dev/null || echo "docker not installed"

h "Package caches"
sudo du -sh /var/cache/apt /var/cache/dnf /var/cache/yum 2>/dev/null

h "Folders with the most files (inode hogs)"
sudo find "$P" -xdev -type f 2>/dev/null | awk -F/ 'BEGIN{OFS="/"} {NF--; print}' | sort | uniq -c | sort -rn | head -10
) 2>&1 | tee "debug-disk-$(date +%Y%m%d-%H%M%S).txt"
What it checks
  1. Usage and inodesspace and file-count use of the filesystem holding the path
  2. Biggest folderstwo levels deep, staying on the same filesystem (-x)
  3. Files over 100 MBthe 20 largest single files, sizes in K/M/G
  4. Deleted but openfiles removed while a process still writes to them (usual cause when df and du disagree)
  5. Logs/var/log folders by size and the systemd journal size
  6. Dockerspace used by images, containers, volumes and build cache
  7. Package cachesapt / dnf / yum download caches
  8. Inode hogsfolders with the most files (no space left with free space shown = inodes)

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…