Fix Systemd Services Failing at Boot (PATH & Env)
Why a script runs manually in your shell but fails under systemd at boot—and how to fix PATH, WorkingDirectory, and network ordering.
You'll learn why systemd rejects relative paths, why your shell's .bashrc PATH and env vars are missing at boot, and how to write a rock-solid .service unit using WorkingDirectory, EnvironmentFile, and network-online.target.
1. The big idea
"It works when I run it in my terminal, but systemd crashes with status=203/EXEC or No such file or directory at boot." That happens because systemd does not launch a login shell: there is no ~/.bashrc, no ~/.nvm, no /usr/local/bin guarantee, and no automatic working directory.
2. Why manual runs differ from systemd
When you run a command interactively, bash loads /etc/profile and ~/.bashrc, sets PWD, and inherits your user environment. Under systemd:
ExecStart=requires an absolute path to the binary (e.g.,/usr/local/bin/node, notnode).PATHdefaults to a minimal system path (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin).network.targetonly means the network stack is initialized—not that an IP address or DNS is ready yet.
# Inspect exact failure reason and full service logs
sudo systemctl status my-app.service
sudo journalctl -u my-app.service -b --no-pager -n 503. Writing a boot-safe systemd unit file
Always specify absolute paths, explicit WorkingDirectory, an EnvironmentFile, and network-online.target if your app connects to external databases or APIs on startup:
[Unit]
Description=CodeHeart Worker Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/opt/codeheart-worker
EnvironmentFile=/etc/codeheart/worker.env
ExecStart=/usr/local/bin/worker --config /etc/codeheart/config.yaml
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.targetAfter saving /etc/systemd/system/codeheart-worker.service, reload systemd and test it immediately:
sudo systemctl daemon-reload
sudo systemctl enable --now codeheart-worker.serviceVerify your unit uses an absolute binary path in ExecStart, sets WorkingDirectory, loads secrets via EnvironmentFile, and uses After=network-online.target + Wants=network-online.target when network connectivity is required.
4. Common pitfalls
| Mistake | Why it fails | Better fix |
|---|---|---|
ExecStart=python app.py | Systemd requires an absolute executable path | Use /opt/app/.venv/bin/python /opt/app/app.py |
Relying on After=network.target | Fires before DHCP/DNS is ready | Pair After=network-online.target with Wants=network-online.target |
| Editing unit file without reload | Systemd keeps running the cached unit definition | Run sudo systemctl daemon-reload before restart |
Putting export KEY=val in EnvironmentFile | Systemd parses KEY=val literally, not as bash | Remove export prefixes in .env files |
5. What you learned
You now know why systemd services behave differently from interactive shells and how to eliminate 203/EXEC and boot-race failures with explicit paths, WorkingDirectory, EnvironmentFile, and network-online.target.