Systemd: Difference between revisions

From miki
Jump to navigation Jump to search
Line 1: Line 1:
== References ==
* [https://unix.stackexchange.com/questions/332747/in-systemd-what-starts-units-generated-by-generator SE - What starts units generated by generator]

== Command-line interface ==
== Command-line interface ==
<source lang=bash>
<source lang=bash>

Revision as of 10:16, 4 May 2020

References

Command-line interface

systemctl list-unit-files     # List unit files installed and their status
systemctl                     # Show all loaded modules and their status
systemctl status              # Show all process and system status

journalctl -b                 # Kernel logs (incl. boot), with coloring

Services

  • systemd:0 means first systemd socket defined in the .socket file (FD 3), systemd:1 is the second (FD 4) and so on.

Configure a service to start at boot

sudo systemctl enable courier-authdaemon

Create a new service

From SE:

  • Create a file /etc/systemd/system/foo.service
[Unit]
Description=foo

[Service]
ExecStart=/bin/bash -c "while true; do /bin/inotifywait -qq --event close_write /sys/class/backlight/acpi_video0/brightness; su myusername -c '/bin/xbacklight -display :0 -set $(cat /sys/class/backlight/acpi_video0/brightness)'; done"

[Install]
WantedBy=multi-user.target

Reload systemd:

systemctl daemon-reload

Enable the new service:

systemctl enable foo

The service will start automatically at boot. To start it immediately:

systemctl start foo
systemctl status foo # optional, just to verify

Example of options to add in the service file:

[Unit]
Description=rapid spam filtering system
After=nss-lookup.target

[Service]
ExecStart=/usr/bin/rspamd -c /etc/rspamd/rspamd.conf -f
User=_rspamd
RuntimeDirectory=rspamd
RuntimeDirectoryMode=0755
Restart=always

[Install]
WantedBy=multi-user.target

Timers

Timers are a bit like cron job except they trigger systemd services, and have more scheduling and management options.

By default, a timer fires the service with the same name (see timer man page).

Power management (systemd-inhibit)

systemd-inhibit may be used to execute a program with a shutdown, sleep, or idle inhibitor lock taken. It allows you do disable only selected features and allows setting user visible string to explain why these features are inhibited [1]. See man systemd-inhibit

systemd-inhibit sleep 2h                                            # Prohibit idle, sleep, shutdown for 2 hours
systemd-inhibit wget "https://example.com/files/huge-download.dat"  # Same, while download ongoing

To list active inhibitors:

systemd-inhibit --list