Hugonweb | SystemD Administration

Control services

List all active services with:

systemctl

add --all to show inactive ones, and list-unit-files to show all with files.

systemctl status <service>
# for example for CUPS printing:
systemctl status cups

In addition to status, you can run start, stop, restart, try-restart, reload, and force-reload.

Monitoring services

To monitor daemons, use journalctl. This command shows the most recent lines:

journalctl -xe

and this shows just for a given service:

journalctl -u <service>

Note that -u requires an exact match. The man page has an error saying it pattern matches. Use grep if you need to pattern match.

Adding a service

You can put new service files in /etc/systemd/system/

Then reload files with:

systemctl daemon-reload

Run a service or similar with:

systemctl start <service file name>

and enable it to be run based on its dependencies with

systemctl enable <service file name>

Service file

[Unit]
Description=System Reboot

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl --force reboot

For Type=oneshot, you can run multiple commands serially with multiple ExecStart= lines. If a command returns a non-zero exit code, the whole thing fails.

Using timers

The timer must have exactly the same base filename as the service, just replacing ".service" with ".timer".

[Unit]
Description=Reboot Scheduling

[Timer]
OnCalendar=*-*-* 00:00:00
RandomizedDelaySec=30m

[Install]
WantedBy=timers.target