Debug a systemd service that failed or keeps restarting
Read-onlyPaste 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
Statusactive / failed / activating (auto-restart), main PID and the last log linesState summaryResult (exit-code, signal, timeout, core-dump), exit status, restart count, enabled or not, unit file pathWarnings and errorspriority warning and worse from the service's journal in the last hourLast 50 log lineseverything the service logged most recentlyUnit file and overridesExecStart, User, Environment, Restart settings, including drop-ins in /etc/systemd/system/<name>.service.d/Program checkthe ExecStart binary exists, is executable, and the right typePortswhat the running process listens on (Address already in use means another process has the port)Memory, CPU and tasksusage against MemoryMax / TasksMax limitsFailed unitsother services that failed (dependencies)OOM killswhether the kernel killed it for lack of memory