Skip to content

systemd Service Generator

Build a .service unit and an optional .timer, with the command quoted the way systemd reads it, a sandbox scored the way systemd-analyze security scores it, and the next run times of any schedule.

Free · No signupBuilt in your browser

Start from
Service

Saved as myapp.service

The full path to the program, then its arguments, as you would type them.

Type

The command stays in the foreground. Right for almost every app.

Commands before start, on reload and on stop

A leading - lets start go on if this command fails.

Run by systemctl reload.

Leave empty and systemd sends SIGTERM, then SIGKILL after 90 seconds.

Runs as

A user made at start and gone at stop, with the system read-only around it. Best when it owns no files.

Environment

For secrets: KEY=value lines in a file only root can read. A leading - makes it optional.

Restart and order

Seconds, or 30s, 2min.

Network

Starts after the network stack is up. Enough for a server that listens.

Pulled in and started first.

Stops when these stop.

Sandbox

Read-only system, private /tmp, no hardware or kernel access. Works for almost any service.

Everything else stays read-only. Separate paths with spaces.

Limits

Killed above this.

100% is one core.

Schedule
31 lines
[Unit]
Description=My application
After=network.target

[Service]
Type=simple
DynamicUser=yes
ExecStart=/usr/local/bin/myapp
Restart=on-failure
RestartSec=5

# Sandboxing. Check the result with systemd-analyze security
NoNewPrivileges=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
ProtectHostname=yes
RestrictSUIDSGID=yes
RestrictRealtime=yes
RestrictNamespaces=yes
LockPersonality=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallArchitectures=native

[Install]
WantedBy=multi-user.target

Save as /etc/systemd/system/myapp.service. The Install steps tab writes, checks and starts it in one go.

systemd-analyze security

4.8OK

0 is fully sandboxed, 10 is wide open. This is the score the server prints.

Counts most against it

  • CapabilityBoundingSet=~CAP_SYS_ADMINKeeps CAP_SYS_ADMIN
  • CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)Keeps CAP_SET(UID|GID|PCAP)
  • CapabilityBoundingSet=~CAP_SYS_PTRACEKeeps CAP_SYS_PTRACE
  • CapabilityBoundingSet=~CAP_NET_ADMINKeeps CAP_NET_ADMIN
  • A dynamic user can write almost nowhere. Turn on the data folder for /var/lib/myapp if it saves anything.

Built in this tab as you type. Nothing you enter leaves this page.

From unit file to running fleet

Manage services across every server.

CtrlOps deploys apps and manages their services across your whole fleet: start, stop, watch status and logs, and monitor resources from one local-first desktop app, with your credentials encrypted on your own machine.

See how deployment worksDownload for MacDownload for Windows

Directive reference

The lines that
matter most.

Every one of these is set from the form above. The full list is in man systemd.directives.

Common systemd directives
DirectiveSectionWhat it does
Description[Unit]The name systemctl status and the journal show.
After / Wants[Unit]After= only orders the start; Wants= pulls the other unit in. Most dependencies need both.
Requires[Unit]Stop this service too when the listed unit stops or fails.
Type[Service]When systemd counts the service as started: simple, exec, notify, forking or oneshot.
ExecStart[Service]The command. Not run by a shell: pipes, redirects and && need /bin/sh -c.
Restart / RestartSec[Service]When to bring it back, and how long to wait first.
DynamicUser[Service]Runs it as a throwaway user created at start, with a read-only system around it.
StateDirectory[Service]Creates /var/lib/name, owned by the service user, and keeps it writable inside the sandbox.
ProtectSystem=strict[Service]The whole file system is read-only for it, except the paths you list.
Environment[Service]One variable, quoted. $ is literal here; % has to be written %%.
WantedBy[Install]What enable hooks it into. multi-user.target means at boot.
OnCalendar[Timer]When a timer fires, such as daily or Mon..Fri 09:00. Persistent=true catches up on missed runs.

How a unit file works

What you write.
What systemd runs.

What is a systemd service?

On modern Linux, systemd starts, supervises, restarts and logs long-running programs. Each one is described in a unit file such as myapp.service, in three sections: [Unit] for its name and what it depends on, [Service] for how to run it, and [Install] for how it is enabled. Files you write go in /etc/systemd/system/.

ExecStart is not a shell

systemd splits the command into words itself. A pipe, a redirect, && or a $(…) reaches the program as a literal argument, and a % is read as a specifier, so date +%Y fails to load. The generator reads your command the way a shell would: plain commands are quoted for systemd word by word, and anything that needs a shell runs under /bin/sh -c, with $ and % escaped so the shell sees exactly what you typed.

Choosing a service type

simple fits a program that stays in the foreground, which is almost all of them. exec also fails the start when the program cannot run at all. notify waits for the program to report it is ready, forking is for old daemons that background themselves, and oneshot is for a job that runs and exits, usually from a timer. systemd refuses Restart=always on a oneshot.

The sandbox, and its score

A handful of lines take most of a service's power away: ProtectSystem=strict makes the file system read-only, PrivateTmp and PrivateDevices hide what other programs use, and NoNewPrivileges stops setuid programs from lifting it back to root. Run systemd-analyze security myapp.service and systemd scores the result from 0, fully sandboxed, to 10, wide open. The score beside the unit above is computed the same way, check by check.

Where a sandboxed service can write

With ProtectSystem=strict a service can write almost nowhere, which is the point. Give it StateDirectory=myapp and systemd creates /var/lib/myapp, owned by the service user, and passes its path in $STATE_DIRECTORY. For other paths, list them in ReadWritePaths.

Timers instead of cron

A oneshot service paired with a .timer runs on schedule with its output in the journal, catches up on runs missed while the server was off with Persistent=true, and can wait for the network or a database first. OnCalendar takes expressions such as daily, Mon..Fri 09:00 or *:0/15; the generator shows how systemd normalizes yours and when it fires next.

Install, enable, and check logs

After saving the file, sudo systemctl daemon-reload makes systemd read it, and sudo systemctl enable --now myapp starts it and enables it at boot. systemctl status myapp shows whether it is running, and journalctl -u myapp -f follows its output.

Build a crontab line with the cron expression generator

systemd FAQ

systemd services,
explained.

Keep reading

Tools and guides for your next step.

Your privacy preferences. We use product analytics to improve CtrlOps. Google measurement runs without analytics cookies. Live chat stays off until you allow it. Cookie policy