All Snippets
Systemd Service Unit Template
Create a systemd service to run any binary or script as a managed background service on Linux.
Systemd manages services on most modern Linux distros. Use this template to turn any binary, script, or application into a managed service with automatic restarts, logging, and boot-time startup.
/etc/systemd/system/myapp.serviceini
| 1 | [Unit] |
| 2 | Description=My Application |
| 3 | After=network.target |
| 4 | Wants=network-online.target |
| 5 | |
| 6 | [Service] |
| 7 | Type=simple |
| 8 | User=appuser |
| 9 | Group=appuser |
| 10 | WorkingDirectory=/opt/myapp |
| 11 | ExecStart=/opt/myapp/bin/myapp --config /etc/myapp/config.yaml |
| 12 | Restart=on-failure |
| 13 | RestartSec=5 |
| 14 | StandardOutput=journal |
| 15 | StandardError=journal |
| 16 | SyslogIdentifier=myapp |
| 17 | |
| 18 | # Hardening |
| 19 | NoNewPrivileges=true |
| 20 | ProtectSystem=strict |
| 21 | ProtectHome=true |
| 22 | ReadWritePaths=/var/lib/myapp |
| 23 | |
| 24 | [Install] |
| 25 | WantedBy=multi-user.target |
Enable and start the servicebash
| 1 | # Reload systemd after creating/editing the unit file |
| 2 | sudo systemctl daemon-reload |
| 3 | |
| 4 | # Enable (auto-start on boot) and start now |
| 5 | sudo systemctl enable --now myapp |
| 6 | |
| 7 | # Check status |
| 8 | sudo systemctl status myapp |
| 9 | |
| 10 | # View logs |
| 11 | journalctl -u myapp -f |
Use Type=notify if your app supports sd_notify, or Type=forking if it daemonizes itself. Most modern apps work fine with Type=simple.