Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: systemd-service-crash-loop-fix description: > Diagnose and fix systemd user service crash loops where the service binary can't find its runtime (node, python, etc.) because systemd doesn't inherit the user's shell environment (nvm, pyenv, volta, brew). Use when a service shows status 127 or "No such file or directory" from env, or when journalctl shows repeated restarts every ~5-10s. trigger: > A systemd user service is crash-looping with exit code 127, or journalctl shows errors like "env: 'node': No such file or directory" or "No such file or directory" from a shebang script. The service was installed via nvm, pyenv, volta, or linuxbrew and works when run manually but not under systemd.
Fix Systemd User Service Crash Loops from Missing Runtime PATH
Diagnosis
1. Check service status and restart counter
systemctl --user status <service>.service
Look for:
Result: exit-codewith status 127 (command not found)- High restart counter (thousands = crash looping for hours/days)
activating (auto-restart)state
2. Check journal logs for the root cause
journalctl --user -u <service>.service --since "1 hour ago" --no-pager | tail -20
Common errors:
env: 'node': No such file or directory— nvm-installed node not in PATHenv: 'python3': No such file or directory— pyenv-installed python not in PATHNo such file or directoryon a script that exists — shebang interpreter missing from PATH
3. Verify the runtime exists but isn't in systemd's PATH
# Check where the runtime actually iswhich node python3 # from your shell (works)# Check what PATH systemd seescat ~/.config/systemd/user/<service>.service | grep -i path# Confirm the binary existsls -la ~/.nvm/versions/node/*/bin/nodels -la ~/.pyenv/shims/python3ls -la $(brew --prefix)/bin/*
Fix
Add explicit PATH to the service file
Edit the service file and add an Environment="PATH=..." line in the [Service] section:
cat > ~/.config/systemd/user/<service>.service << 'EOF'[Unit]Description=<description>After=network.target[Service]Type=simpleWorkingDirectory=<app-dir>Environment="PATH=~/.nvm/versions/node/v22.21.1/bin:/usr/local/bin:/usr/bin:/bin"ExecStart=<full-path-to-binary> <args>Restart=on-failureRestartSec=10[Install]WantedBy=default.targetEOF
Then reload and restart:
systemctl --user daemon-reloadsystemctl --user restart <service>.servicesystemctl --user status <service>.service
Common PATH values by runtime manager
| Manager | Runtime | PATH entry | |
|---|---|---|---|
| nvm | node | ~/.nvm/versions/node/vXX.XX.X/bin | |
| pyenv | python | ~/.pyenv/shims:~/.pyenv/bin | |
| volta | node/npm | ~/.volta/bin | |
| linuxbrew | any | $(brew --prefix)/bin |
Pitfalls
- Restart counter in tens of thousands: A service restarting every 10s for 24+ hours accumulates 8,000+ restarts. Fix immediately — it wastes resources and clutters logs.
- Don't just disable the service: If the user wants it running, fix the PATH. Only disable if the service is genuinely unused.
- `serve` and other npx-installed tools: Tools installed via
npm install -gorbunxuse#!/usr/bin/env nodeshebangs. They break under systemd for the same reason — node isn't in PATH. - RestartSec=10 is aggressive: For services that legitimately crash-loop during startup (e.g., waiting for a dependency), consider increasing to 30-60s or adding a
ExecStartPrehealth check. - Hardcoded node version: If the service file references a specific nvm node version (e.g.,
v22.21.1), it will break when node is upgraded. Consider using a symlink or volta instead.
Verification
# Should show "active (running)"systemctl --user is-active <service>.service# Check logs for clean startupjournalctl --user -u <service>.service --since "1 min ago" --no-pager# Verify the service is actually listeningcurl -s http://localhost:<port> | head -5