home blog portfolio Ian Fisher

systemd cheatsheet

See also: wiki/systemd

Commands

# reload config files
sudo systemctl daemon-reload

# the full name is 'foo.service'
# '.service' is assumed if not specified
sudo systemctl start foo
sudo systemctl stop foo
sudo systemctl restart foo
sudo systemctl status foo

# view logs
sudo journalctl -u foo

Non-root services

Set-up:

  1. Make the change to /etc/systemd/journald.conf described here.
  2. Reboot the system.
  3. mkdir -p ~/.config/systemd/user
  4. Put your service files in that directory. Remove any User or Group directives.
  5. Set up lingering so services can run without logging in: loginctl enable-linger $USER
  6. systemctl --user daemon-reexec
systemctl --user daemon-reload
sudo systemctl --user start foo
# etc
journalctl --user -u foo

Service file

[Unit]
Description=Grafana

[Service]
ExecStart=/usr/bin/grafana server
User=ian
WorkingDirectory=/home/ubuntu/grafana
Environment="MY_VAR=value"
Environment="MY_VAR2=value"
Restart=always
# if restarts more than 3 times in 60 seconds, don't try to restart
# NOTE: before systemd v230, called `StartLimitInterval`
StartLimitIntervalSec=60s
StartLimitBurst=3

[Install]
# start service on system start-up
WantedBy=default.target

See also