Overview
systemd is the standard init system for most modern Linux distributions (Ubuntu, Debian, RHEL, CentOS). Generating a proper systemd unit file is critical for ensuring your applications restart automatically on failure, start at boot, and are properly sandboxed from the rest of the system.
Process Management
Automatic restarts, clean shutdowns, and dependency ordering.
Sandboxing
Limit filesystem access, drop capabilities, and isolate networking.
Resource Control
Set MemoryMax, CPUQuota, and file descriptor limits (LimitNOFILE).
Best Practices for Production
- Never run as root if the service doesn't strictly need it. Use `User=` or `DynamicUser=yes`.
- Always set a restart policy (e.g., `Restart=on-failure`) but pair it with `StartLimitBurst` to avoid CPU spin loops.
- Harden your unit using `ProtectSystem=strict`, `ProtectHome=yes`, and `NoNewPrivileges=yes`.
- Use `EnvironmentFile` for secrets rather than hardcoding them in `Environment=` directives.
Troubleshooting Guide
Service fails to start (code=exited, status=203/EXEC): Check that the executable path in `ExecStart` is absolute and correct. Ensure the file has execute permissions (`chmod +x`).
Service starts but immediately exits: If using `Type=simple`, ensure the application runs in the foreground (do not let it daemonize itself). If it daemonizes, change `Type=forking`.
Logs aren't showing up: By default, stdout/stderr go to the journal. Check logs using `journalctl -u myapp.service -f`.