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:
- Make the change to
/etc/systemd/journald.conf
described here. - Reboot the system.
mkdir -p ~/.config/systemd/user
- Put your service files in that directory. Remove any
User
orGroup
directives. - Set up lingering so services can run without logging in:
loginctl enable-linger $USER
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
Links
- systemd (Arch Linux wiki)
- systemd by example