SystemD#

Basics:

systemctl status foo  # same as foo.service
systemctl status foo.mount

# what's wrong?
systemctl --failed

# what's slow?
systemd-analyze blame

systemctl list-dependencies foo.service

Compose project that needs host mount starting on boot:

cat <<'EOF' > /etc/systemd/system/foo.service
[Unit]
Description=foo
Requires=docker.service
After=network.target docker.service foo.mount

[Service]
WorkingDirectory=/srv/foo

# ExecStartPre=/usr/local/bin/docker-compose pull
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down --remove-orphans

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable foo
systemctl start foo

Mounts:

vi /etc/fstab
systemctl restart remote-fs.target

Simple service running as unprivileged user foo that depends on another service and network:

cat <<'EOF' > /etc/systemd/system/foo.service
[Unit]
Description=foo
Requires=another.service
After=network.target another.service

[Service]
Type=simple
User=foo
Group=foo
WorkingDirectory=/srv/foo
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down --remove-orphans

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable foo
systemctl start foo

Links:

Rants#

Where’s the 90%-use-case tutorial? Why are User= and Group= defined in systemd.exec when I want to define a systemd.service in a systemd.unit ?