systemd Service Generator

Create a systemd service unit to run your app on Linux at boot, with restart rules, environment variables, a service user and optional sandboxing.

Processed on your device. Files are not uploaded

How to make a systemd service

  1. Fill in the command

    The full path to the program, its working directory and the user to run as.

  2. Choose restarts and sandboxing

    Restart on failure is a good default; sandboxing limits what the service can touch.

  3. Install it

    Save the file to /etc/systemd/system/ and run the commands shown.

Worked example

This is what systemd Service Generator gives for its own sample values. Open the tool above and change anything to try your own.

Service name
myapp
Description
My web app
Command to run (full path)
/usr/bin/node /srv/myapp/server.js
Working directory
/srv/myapp
Run as user
myapp
Group (optional)

/etc/systemd/system/myapp.service

[Unit]
Description=My web app
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=myapp
WorkingDirectory=/srv/myapp
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node /srv/myapp/server.js
Restart=on-failure
RestartSec=5
...

Then run

sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
journalctl -u myapp -f

When to use systemd Service Generator

A Node, Python or Go app on a Linux server needs to start at boot and come back after a crash.

You are replacing a screen or nohup session with a proper service that logs to the journal.

Tips for a better systemd Service Generator result

Create a dedicated system user for the service and give it ownership of only its working directory.

Use an EnvironmentFile for secrets and set its permissions to 600, so they are not visible in systemctl show.

systemd Service Generator: limits and what to check

With Restart=always, a program that exits at once will restart in a loop until systemd's start limit stops it.

Programs that fork into the background need Type=forking and a PIDFile, otherwise systemd thinks they stopped.

About systemd Service Generator

Run your app as a proper Linux service: started at boot, restarted when it crashes, logged to the journal, and run as its own user. The optional sandboxing makes the system read-only to the service apart from its working directory, gives it a private /tmp and stops it gaining new privileges.

FAQs

What is the difference between simple and notify?

With simple, systemd treats the service as started at once. With notify, the program tells systemd when it is ready, which only works if it supports sd_notify.

Where do the logs go?

To the journal. Read them with journalctl -u myapp -f.

Why does my service fail with sandboxing on?

It writes somewhere outside its working directory. Add that path to ReadWritePaths.

Related tools