Skip to main content
When you close your SSH session, anything you started in that shell dies with it. systemd fixes this: it runs your app as a background service, restarts it if it crashes, and starts it automatically when the server boots. If you want a bot, web app, or game server to just stay up, this is how you do it.

Anatomy of a service file

Systemd service files live in /etc/systemd/system/ and end in .service. Here is a template you can adapt for any long-running app:
Key fields:
  • User — which user the process runs as. Avoid root when possible.
  • WorkingDirectory — the app’s working directory, as if you had cd’d there.
  • ExecStart — the command to run. Use absolute paths.
  • Restart=on-failure — restart if the process exits with an error.

Create and enable a service

1

Write the service file

Paste the template above and adjust the fields for your app.
2

Reload systemd so it picks up the new file

3

Start the service

4

Enable it on boot

Manage a service

View logs

systemd captures your app’s stdout and stderr automatically — no log file setup required.
If your service fails to start, sudo systemctl status myapp usually tells you why in the last few lines of output. When it does not, journalctl -u myapp -n 50 will.