Enrico Zini: systemd-truelitehttp://www.enricozini.org/tags/systemd-truelite/
Enrico Zini: systemd-truelitestaticsiteMon, 19 Mar 2018 02:00:16 -0000Systemd socket unitshttp://www.enricozini.org/blog/2017/debian/systemd-09-socketshttp://www.enricozini.org/blog/2017/debian/systemd-09-sockets
pdodebianengswsystemd-trueliteFri, 29 Sep 2017 22:00:00 -00002017-09-30T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.socket</code> units</h3>
<p>Socket units tell systemd to listen on a given IPC, network socket, or file
system FIFO, and use another unit to service requests to it.</p>
<p>For example, this creates a network service that listens on port 55555:</p>
<div class="codehilite"><pre><span></span># /etc/systemd/system/ddate.socket
[Unit]
Description=ddate service on port 55555
[Socket]
ListenStream=55555
Accept=true
[Install]
WantedBy=sockets.target
</pre></div>
<div class="codehilite"><pre><span></span># /etc/systemd/system/ddate@.service
[Unit]
Description=Run ddate as a network service
[Service]
Type=simple
ExecStart=/bin/sh -ec &#39;while true; do /usr/bin/ddate; sleep 1m; done&#39;
StandardOutput=socket
StandardError=journal
</pre></div>
<p>Note that the <code>.service</code> file is called <code>ddate@</code> instead of <code>ddate</code>: units
whose name ends in '@' are <a href="https://fedoramagazine.org/systemd-template-unit-files/">template units</a>
which can be activated multiple times, by adding any string after the '@' in
the unit name.</p>
<p>If I run <code>nc localhost 55555</code> a couple of times, and then check the list of
running units, I see <code>ddate@…</code> instantiated twice, adding the local and remote
socket endpoints to the unit name:</p>
<div class="codehilite"><pre><span></span><span class="err">$</span> <span class="n">systemctl</span> <span class="n">list</span><span class="o">-</span><span class="n">units</span> <span class="err">&#39;</span><span class="n">ddate</span><span class="p">@</span><span class="o">*</span><span class="err">&#39;</span>
<span class="n">UNIT</span> <span class="n">LOAD</span> <span class="n">ACTIVE</span> <span class="n">SUB</span> <span class="n">DESCRIPTION</span>
<span class="n">ddate</span><span class="mi">@15</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">55555</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mf">36936.</span><span class="n">service</span> <span class="n">loaded</span> <span class="n">active</span> <span class="n">running</span> <span class="n">Run</span> <span class="n">ddate</span> <span class="n">as</span> <span class="n">a</span> <span class="n">network</span> <span class="n">service</span> <span class="p">(</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">36936</span><span class="p">)</span>
<span class="n">ddate</span><span class="mi">@16</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">55555</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mf">37002.</span><span class="n">service</span> <span class="n">loaded</span> <span class="n">active</span> <span class="n">running</span> <span class="n">Run</span> <span class="n">ddate</span> <span class="n">as</span> <span class="n">a</span> <span class="n">network</span> <span class="n">service</span> <span class="p">(</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">37002</span><span class="p">)</span>
</pre></div>
<p>This allows me to monitor each running service individually.</p>
<p>systemd also automatically creates a <a href="https://www.freedesktop.org/software/systemd/man/systemd.slice.html"><code>slice</code> unit</a>
called <code>system-ddate.slice</code> grouping all services together:</p>
<div class="codehilite"><pre><span></span><span class="err">$</span> <span class="n">systemctl</span> <span class="n">status</span> <span class="n">system</span><span class="o">-</span><span class="n">ddate</span><span class="p">.</span><span class="n">slice</span>
<span class="err">●</span> <span class="n">system</span><span class="o">-</span><span class="n">ddate</span><span class="p">.</span><span class="n">slice</span>
<span class="nl">Loaded</span><span class="p">:</span> <span class="n">loaded</span>
<span class="nl">Active</span><span class="p">:</span> <span class="n">active</span> <span class="n">since</span> <span class="n">Thu</span> <span class="mi">2017</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">14</span><span class="o">:</span><span class="mi">25</span><span class="o">:</span><span class="mo">02</span> <span class="n">CEST</span><span class="p">;</span> <span class="mi">9</span><span class="n">min</span> <span class="n">ago</span>
<span class="nl">Tasks</span><span class="p">:</span> <span class="mi">4</span>
<span class="nl">CGroup</span><span class="p">:</span> <span class="o">/</span><span class="n">system</span><span class="p">.</span><span class="n">slice</span><span class="o">/</span><span class="n">system</span><span class="o">-</span><span class="n">ddate</span><span class="p">.</span><span class="n">slice</span>
<span class="err">├─</span><span class="n">ddate</span><span class="mi">@15</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">55555</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mf">36936.</span><span class="n">service</span>
<span class="err">│</span> <span class="err">├─</span><span class="mi">18214</span> <span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">sh</span> <span class="o">-</span><span class="n">ec</span> <span class="k">while</span> <span class="nb">true</span><span class="p">;</span> <span class="k">do</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">ddate</span><span class="p">;</span> <span class="n">sleep</span> <span class="mi">1</span><span class="n">m</span><span class="p">;</span> <span class="n">done</span>
<span class="err">│</span> <span class="err">└─</span><span class="mi">18661</span> <span class="n">sleep</span> <span class="mi">1</span><span class="n">m</span>
<span class="err">└─</span><span class="n">ddate</span><span class="mi">@16</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mi">55555</span><span class="o">-</span><span class="mf">127.0.0.1</span><span class="o">:</span><span class="mf">37002.</span><span class="n">service</span>
<span class="err">├─</span><span class="mi">18228</span> <span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">sh</span> <span class="o">-</span><span class="n">ec</span> <span class="k">while</span> <span class="nb">true</span><span class="p">;</span> <span class="k">do</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">ddate</span><span class="p">;</span> <span class="n">sleep</span> <span class="mi">1</span><span class="n">m</span><span class="p">;</span> <span class="n">done</span>
<span class="err">└─</span><span class="mi">18670</span> <span class="n">sleep</span> <span class="mi">1</span><span class="n">m</span>
</pre></div>
<p>This allows to also work with all running services for this template unit as a
whole, sending a signal to all their processes and setting up
<a href="https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html">resource control</a>
features for the service as a whole.</p>
<p>See:</p>
<ul>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.socket.html"><code>man systemd.socket</code></a></li>
<li><a href="http://0pointer.de/blog/projects/socket-activation.html">systemd for developers: socket activation</a></li>
<li><a href="https://fedoramagazine.org/systemd-template-unit-files/">template units</a></li>
</ul>Systemd path unitshttp://www.enricozini.org/blog/2017/debian/systemd-08-pathshttp://www.enricozini.org/blog/2017/debian/systemd-08-paths
pdodebianengswsystemd-trueliteThu, 28 Sep 2017 22:00:00 -00002017-09-29T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.path</code> units</h3>
<p>This kind of unit can be used to monitor a file or directory for changes using
<a href="https://en.wikipedia.org/wiki/Inotify">inotify</a>, and activate other units when
an event happens.</p>
<p>For example, this activates a unit that manages a spool directory, which
activates another unit whenever a <code>.pdf</code> file is added to <code>/tmp/spool/</code>:</p>
<div class="codehilite"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Monitor /tmp/spool/ for new .pdf files</span>
<span class="k">[Path]</span>
<span class="na">Unit</span><span class="o">=</span><span class="s">beeponce.service</span>
<span class="na">PathExistsGlob</span><span class="o">=</span><span class="s">/tmp/spool/*.pdf</span>
<span class="na">MakeDirectory</span><span class="o">=</span><span class="s">true</span>
</pre></div>
<p>This instead activates another unit whenever <code>/tmp/ready</code> is changed, for
example by someone running <code>touch /tmp/ready</code>:</p>
<div class="codehilite"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Monitor /tmp/ready</span>
<span class="k">[Path]</span>
<span class="na">Unit</span><span class="o">=</span><span class="s">beeponce.service</span>
<span class="na">PathChanged</span><span class="o">=</span><span class="s">/tmp/ready</span>
</pre></div>
<p>And <code>beeponce.service</code>:</p>
<div class="codehilite"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Beeps once</span>
<span class="k">[Service]</span>
<span class="na">Type</span><span class="o">=</span><span class="s">oneshot</span>
<span class="na">ExecStart</span><span class="o">=</span><span class="s">/usr/bin/aplay /tmp/beep.wav</span>
</pre></div>
<p>See <a href="https://www.freedesktop.org/software/systemd/man/systemd.path.html"><code>man systemd.path</code></a></p>Systemd device unitshttp://www.enricozini.org/blog/2017/debian/systemd-07-deviceshttp://www.enricozini.org/blog/2017/debian/systemd-07-devices
pdodebianengswsystemd-trueliteWed, 27 Sep 2017 22:00:00 -00002017-09-28T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.device</code> units</h3>
<p>Several devices are automatically represented inside systemd by <code>.device</code>
units, which can be used to activate services when a given device exists in the
file system.</p>
<p>See <code>systemctl --all --full -t device</code> to see a list of all decives for which
systemd has a unit in your system.</p>
<p>For example, this <code>.service</code> unit plays a sound as long as a specific USB key
is plugged in my system:</p>
<div class="codehilite"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Beeps while a USB key is plugged</span>
<span class="na">DefaultDependencies</span><span class="o">=</span><span class="s">false</span>
<span class="na">StopWhenUnneeded</span><span class="o">=</span><span class="s">true</span>
<span class="k">[Install]</span>
<span class="na">WantedBy</span><span class="o">=</span><span class="s">dev-disk-by\x2dlabel-ERLUG.device</span>
<span class="k">[Service]</span>
<span class="na">Type</span><span class="o">=</span><span class="s">simple</span>
<span class="na">ExecStart</span><span class="o">=</span><span class="s">/bin/sh -ec &#39;while true; do /usr/bin/aplay -q /tmp/beep.wav; sleep 2; done&#39;</span>
</pre></div>
<p>If you need to work with a device not seen by default by systemd, you can add a
<a href="http://reactivated.net/writing_udev_rules.html">udev rule</a> that makes it available,
by adding the <code>systemd</code> tag to the device with <code>TAG+="systemd"</code>.</p>
<p>It is also possible to give the device an extra alias using <code>ENV{SYSTEMD_ALIAS}="/dev/my-alias-name"</code>.</p>
<p>To figure out all you can use for matching a device:</p>
<ol>
<li>Run <code>udevadm monitor --environment</code> and plug the device</li>
<li>Look at the <code>DEVNAME=</code> values and pick one that addresses your device the
way you prefer</li>
<li><code>udevadm info --attribute-walk --name=*the value of devname*</code> will give you
all you can use for matching in the udev rule.</li>
</ol>
<p>See:</p>
<ul>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.device.html"><code>man systemd.device</code></a></li>
<li><a href="https://coreos.com/os/docs/latest/using-systemd-and-udev-rules.html">Using systemd and udev rules</a></li>
</ul>Systemd timer unitshttp://www.enricozini.org/blog/2017/debian/systemd-06-timershttp://www.enricozini.org/blog/2017/debian/systemd-06-timers
pdodebianengswsystemd-trueliteTue, 26 Sep 2017 22:00:00 -00002017-09-27T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.timer</code> units</h3>
<p>Configure activation of other units (usually a <code>.service</code> unit) at some given time.</p>
<p>The functionality is similar to cron, with more features and a finer time
granularity. For example, in Debian Stretch apt has a timer for running <code>apt
update</code> which runs at a random time to distribute load on servers:</p>
<div class="codehilite"><pre><span></span># /lib/systemd/system/apt-daily.timer
[Unit]
Description=Daily apt download activities
After=network-online.target
Wants=network-online.target
[Timer]
OnCalendar=*-*-* 6,18:00
RandomizedDelaySec=12h
Persistent=true
[Install]
WantedBy=timers.target
</pre></div>
<p>The corresponding <code>apt-daily.service</code> file then only runs when the system is on
mains power, to avoid unexpected batter drains for systems like laptops:</p>
<div class="codehilite"><pre><span></span># /lib/systemd/system/apt-daily.service
[Unit]
Description=Daily apt download activities
Documentation=man:apt(8)
ConditionACPower=true
[Service]
Type=oneshot
ExecStart=/usr/lib/apt/apt.systemd.daily update
</pre></div>
<p><strong>Note</strong> that if you want to schedule tasks with an accuracy under a minute
(for example to play a beep every 10 seconds when running on battery), you need
to also configure <code>AccuracySec=</code> for the timer to a delay shorter than the
default <code>1 minute</code>.</p>
<p>This is how to make your computer beep when on battery:</p>
<div class="codehilite"><pre><span></span># /etc/systemd/system/beep-on-battery.timer
[Unit]
Description=Beeps every 10 seconds
[Install]
WantedBy=timers.target
[Timer]
AccuracySec=1s
OnUnitActiveSec=10s
</pre></div>
<div class="codehilite"><pre><span></span># /etc/systemd/system/beep-on-battery.service
[Unit]
Description=Beeps when on battery
ConditionACPower=false
[Service]
Type=oneshot
ExecStart=/usr/bin/aplay /tmp/beep.wav
</pre></div>
<p>See:</p>
<ul>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.timer.html"><code>man systemd.timer</code></a></li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.time.html"><code>man systemd.time</code></a></li>
<li><a href="https://joeyh.name/blog/entry/a_programmable_alarm_clock_using_systemd/">a programmable alarm clock using systemd</a></li>
<li><code>systemctl --all -t timer</code> for examples</li>
</ul>Systemd mount and swap unitshttp://www.enricozini.org/blog/2017/debian/systemd-05-mounthttp://www.enricozini.org/blog/2017/debian/systemd-05-mount
pdodebianengswsystemd-trueliteMon, 25 Sep 2017 22:00:00 -00002017-09-26T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.mount</code> and <code>.swap</code> units</h3>
<p>Describe mount points similarly as what <code>/etc/fstab</code>, but with more
functionality and integration with the dependency system.</p>
<p>It is possible to define, for example, a filesystem that should be mounted only
when the network is available and a given service has successfully started, and
a service that should be started only after a given filesystem has been
successfully mounted.</p>
<p>At boot, systemd uses <a href="https://www.freedesktop.org/software/systemd/man/systemd-fstab-generator.html"><code>systemd-fstab-generator</code></a>
to generate mount and swap units from <code>/etc/fstab</code>, so that the usual fstab
configuration file can still be used to configure the file system layout.</p>
<p>See <a href="https://www.freedesktop.org/software/systemd/man/systemd.mount.html"><code>man systemd.mount</code></a>, and
<a href="https://www.freedesktop.org/software/systemd/man/systemd.swap.html"><code>man systemd.swap</code></a>.</p>
<p>See <code>systemctl --all -t mount</code> and <code>systemctl --all -t swap</code> for examples.</p>Systemd service unitshttp://www.enricozini.org/blog/2017/debian/systemd-04-serviceshttp://www.enricozini.org/blog/2017/debian/systemd-04-services
pdodebianengswsystemd-trueliteSun, 24 Sep 2017 22:00:00 -00002017-09-25T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3><code>.service</code> units</h3>
<p>Describe how to start and stop system services, like daemons.</p>
<p>Services are described in a <code>[Service]</code> section. The <code>Type=</code> configuration
describes how the service wants to be brought up:</p>
<ul>
<li><code>Type=simple</code>: a program is started and runs forever, providing the servive.
systemd takes care of daemonizing it properly in the background, creating a
pidfile, stopping it and so on. The service is considered active as soon as
it has started.</li>
<li><code>Type=forking</code>: a traditional daemons that forks itself, creates a pidfile
and so on. The server is considered active as soon as the parent process
ends.</li>
<li><code>Type=oneshot</code>: a program is run once, and the service is considered started
after the program ends. This can be used, for example, to implement a service
to do one-off configuration, like checking a file system.</li>
<li><code>Type=dbus</code>: like <code>simple</code> but for D-Bus services: the service is considered
active as soon as it appears on the D-Bus bus.</li>
<li><code>Type=notify</code>: like <code>simple</code>, but the service tells sytemd when it has
finished initialization and is ready. Notification can happen via the
<a href="https://www.freedesktop.org/software/systemd/man/sd_notify.html"><code>sd_notify</code></a>
C function, or the
<a href="https://www.freedesktop.org/software/systemd/man/systemd-notify.html"><code>systemd-notify</code></a>
command.</li>
<li><code>Type=idle</code>: like <code>simple</code>, but it is run after all other services has been
started on a transaction. You can use this, for example, to start a shell on
a terminal after the boot, so that the prompt doesn't get flooded with boot
messages, or to play a happy trumped sound after the system has finished
booting.</li>
</ul>
<p>There are a lot more configuration options to fine-tune how the program should
be managed, to limit its resource access or capabilities to harden the system
security, to run setup/cleanup scripts before or after it started, and after it
gets stopped, to control what signals to send to ask for reload or quit, and
quite a lot more.</p>
<p>See:
<a href="https://www.freedesktop.org/software/systemd/man/systemd.service.html"><code>man systemd.service</code></a>,
<a href="https://www.freedesktop.org/software/systemd/man/systemd.exec.html"><code>man systemd.exec</code></a>,
<a href="https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html"><code>man systemd.resource-control</code></a>, and
<a href="https://www.freedesktop.org/software/systemd/man/systemd.kill.html"><code>man systemd.kill</code></a>.</p>
<p>See <code>systemctl --all -t service</code> for examples.</p>Systemd unit fileshttp://www.enricozini.org/blog/2017/debian/systemd-03-unitshttp://www.enricozini.org/blog/2017/debian/systemd-03-units
pdodebianengswsystemd-trueliteSat, 23 Sep 2017 22:00:00 -00002017-09-24T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h2>Writing <code>.unit</code> files</h2>
<p>For reference, the global index with all <code>.unit</code> file directives is at
<a href="https://www.freedesktop.org/software/systemd/man/systemd.directives.html"><code>man systemd.directives</code></a>.</p>
<p>All unit files have a <code>[Unit]</code> section with documentation and dependencies. See
<a href="https://www.freedesktop.org/software/systemd/man/systemd.unit.html"><code>man systemd.unit</code></a>
for documentation.</p>
<p>It is worth having a look at existing units to see what they are like. Use
<code>systemctl --all -t unittype</code> for a list, and <code>systemctl cat unitname</code> to see
its content wherever it is installed.</p>
<p>For example: <code>systemctl cat graphical.target</code>. Note that <code>systemctl cat</code> adds a
line of comment at the top so one can see where the unit file is installed.</p>
<p>Most unit files also have an <code>[Install]</code> section (also documented in
<a href="https://www.freedesktop.org/software/systemd/man/systemd.unit.html"><code>man systemd.unit</code></a>)
that controls what happens when enabling or disabling the unit.</p>
<p>See also:</p>
<ul>
<li><a href="https://fedoramagazine.org/systemd-unit-dependencies-and-order/">Systemd unit dependencies and order</a></li>
<li><a href="https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files">Understanding systemd units and unit files</a></li>
</ul>
<h3><code>.target</code> units</h3>
<p><code>.target</code> units only contain <code>[Unit]</code> and <code>[Install]</code> sections, and can be used
to give a name to a given set of dependencies.</p>
<p>For example, one could create a <code>remote-maintenance.target</code> unit, that when
brought up activates, via dependencies, a set of services, mounts, network
sockets, and so on.</p>
<p>See <a href="https://www.freedesktop.org/software/systemd/man/systemd.target.html"><code>man systemd.target</code></a></p>
<p>See <code>systemctl --all -t target</code> for examples.</p>
<h3>special units</h3>
<p><a href="https://www.freedesktop.org/software/systemd/man/systemd.special.html"><code>man systemd.special</code></a>
has a list of units names that have a standard use associated to them.</p>
<p>For example, <code>ctrl-alt-del.target</code> is a unit that is started whenever
Control+Alt+Del is pressed on the console. By default it is symlinked to
<code>reboot.target</code>, and you can provide your own version in <code>/etc/systemd/system/</code>
to perform another action when Control+Alt+Del is pressed.</p>
<h2>User units</h2>
<p>systemd can also be used to manage services on a user session, starting them at
login and stopping them at logout.</p>
<p>Add <code>--user</code> to the normal systemd commands to have them work with the current
user's session instead of the general system.</p>
<p>See <a href="https://wiki.archlinux.org/index.php/Systemd/User">systemd/User in the Arch Wiki</a>
for a good description of what it can do.</p>Systemd on the command linehttp://www.enricozini.org/blog/2017/debian/systemd-02-cmdlinehttp://www.enricozini.org/blog/2017/debian/systemd-02-cmdline
pdodebianengswsystemd-trueliteFri, 22 Sep 2017 22:00:00 -00002017-09-23T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<h3>Exploring the state of a system</h3>
<ul>
<li><code>systemctl status [unitname [unitname..]]</code> show status of one or more units,
or of the whole system. Glob patterns also work: <code>systemctl status "systemd-fsck@*"</code></li>
<li><code>systemctl list-units</code> or just <code>systemctl</code> show a table with all units, their
status and their description</li>
<li><code>systemctl list-sockets</code> lists listening sockets managed by systemd and what
they activate</li>
<li><code>systemctl list-timers</code> lists timer-activated units, with information about
when they last ran and when they will run again</li>
<li><code>systemctl is-active [pattern]</code> checks if one or more units are in active state</li>
<li><code>systemctl is-enabled [pattern]</code> checks if one or more units are enabled</li>
<li><code>systemctl is-failed [pattern]</code> checks if one or more units are in failed state</li>
<li><code>systemctl list-dependencies [unitname]</code> lists the dependencies of a unit, or
a system-wide dependency tree</li>
<li><code>systemctl is-system-running</code> check if the system is running correctly, or if
some unit is in a failed state</li>
<li><code>systemd-cgtop</code> like <a href="https://manpages.debian.org/stretch/procps/top.1.en.html">top</a>
but processes are aggregated by unit</li>
<li><code>systemd-analyze</code> produces reports on boot time, per-unit boot time charts,
dependency graphs, and more</li>
</ul>
<h3>Start and stop services</h3>
<p>Similar to the System V <a href="https://manpages.debian.org/stretch/init-system-helpers/service.8.en.html"><code>service</code></a> command,
<code>systemctl</code> provides commands to start/stop/restart/reload units or services:</p>
<ul>
<li><code>start</code>: starts a unit if it is not already started</li>
<li><code>stop</code>: stops a unit</li>
<li><code>restart</code>: starts or restarts a unit</li>
<li><code>reload</code>: tell a unit to reload its configuration (if it supports it)</li>
<li><code>try-restart</code>: restarts a unit only if it is already active, otherwise do nothing, to prevent accidentally starting a service</li>
<li><code>reload-or-restart</code>: tell a unit to reload its configuration if supported, otherwise restart it</li>
<li><code>try-reload-or-restart</code>: tell a unit to reload its configuration if supported, otherwise restart it. If the unit is not already active, do nothing to prevent accidentally starting a service.</li>
</ul>
<h3>Changing global system state</h3>
<p><code>systemctl</code> has <code>halt</code>, <code>poweroff</code>, <code>reboot</code>, <code>suspend</code>, <code>hibernate</code>, and
<code>hybrid-sleep</code> commands to tell systemd to reboot, power off, suspend and so
on. <code>kexec</code> and <code>switch-root</code> also work.</p>
<p>The <code>rescue</code> and <code>emergency</code> commands switch the system to rescue and emergency
mode (see <a href="https://www.freedesktop.org/software/systemd/man/systemd.special.html"><code>man systemd.special</code></a>.
<code>systemctl default</code> switches to the default mode, which also happens when
exiting the rescue or emergency shell.</p>
<h3>Run services at boot</h3>
<p>systemd does not implement runlevels, and services start at boot based on their
dependencies.</p>
<p>To start a service at boot, you add to its <code>.service</code> file a <code>WantedBy=</code>
dependency on a well-known <code>.target</code> unit.</p>
<p>At boot, systemd brings up the whole chain of dependency started from a default
unit, and that will eventually activate also your service.</p>
<p>See <code>systemctl get-default</code> for what unit is currently the default in your
system. You can change it via the <code>systemd.unit=</code> <a href="https://www.freedesktop.org/software/systemd/man/kernel-command-line.html">kernel command line</a>,
so you can configure multiple entries in the boot loader that boot the system
running different services. For example <code>systemd.unit=rescue.target</code> for a
rescue mode, <code>systemd.unit=multi-user.target</code> for a non-graphical mode, or add
your own <code>.target</code> file to implement new system modes.</p>
<p>See <code>systemctl list-units -t target --all</code> for a list of all currently
available targets in your system.</p>
<ul>
<li><code>systemctl enable unitname</code> enables the unit to start at boot, by creating
symlinks to it in the <code>.wants</code> directory of the units listed in its
<code>WantedBy=</code> configuration</li>
<li><code>systemctl disable unitname</code> removes the symlinks created by enable</li>
<li><code>systemctl reenable unitname</code> removes and readds the symlinks for when you
changed <code>WantedBy=</code></li>
</ul>
<p>Notes:
<em> <code>systemctl start</code> activates a unit right now, but does not automatically
enable it at boot
</em> <code>systemctl enable</code> enables a unit at boot, but does not automatically start
it right now
* a disabled unit can still be activated if another unit depends on it</p>
<p>To disable a unit so that it will never get started even if another unit
depends on it, use <code>systemctl mask unitname</code>. Use <code>systemctl unmask unitname</code>
to undo the masking.</p>
<h3>Reloading / restarting systemd</h3>
<p><code>systemctl daemon-reload</code> tells systemd to reload its configuration.</p>
<p><code>systemctl daemon-reexec</code> tells systemd to restart iself.</p>Systemd Truelite coursehttp://www.enricozini.org/blog/2017/debian/systemd-01-introhttp://www.enricozini.org/blog/2017/debian/systemd-01-intro
pdodebianengswsystemd-trueliteThu, 21 Sep 2017 22:00:00 -00002017-09-22T00:00:00+02:00<p>These are the notes of a training course on systemd I gave as part of my work
with <a href="https://www.truelite.it/">Truelite</a>.</p>
<p>There is quite a lot of material, so I split them into a series of posts,
running once a day for the next 9 days.</p>
<h2>Units</h2>
<p>Everything managed by systemd is called a <em>unit</em> (see <a href="https://www.freedesktop.org/software/systemd/man/systemd.unit.html"><code>man systemd.unit</code></a>),
and each unit is described by a configuration in <a href="https://en.wikipedia.org/wiki/INI_file">ini-style format</a>.</p>
<p>For example, this unit continuously plays an alarm sound when the system is in emergency or rescue mode:</p>
<div class="codehilite"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Beeps when in emergency or rescue mode</span>
<span class="na">DefaultDependencies</span><span class="o">=</span><span class="s">false</span>
<span class="na">StopWhenUnneeded</span><span class="o">=</span><span class="s">true</span>
<span class="k">[Install]</span>
<span class="na">WantedBy</span><span class="o">=</span><span class="s">emergency.target rescue.target</span>
<span class="k">[Service]</span>
<span class="na">Type</span><span class="o">=</span><span class="s">simple</span>
<span class="na">ExecStart</span><span class="o">=</span><span class="s">/bin/sh -ec &#39;while true; do /usr/bin/aplay -q /tmp/beep.wav; sleep 2; done&#39;</span>
</pre></div>
<p>Units can be described by configuration files, which have different extensions
based on what kind of thing they describe:</p>
<ul>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.service.html"><code>.service</code></a>: daemons</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.socket.html"><code>.socket</code></a>: communication sockets</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.device.html"><code>.device</code></a>: hardware devices</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.mount.html"><code>.mount</code></a>: mount points</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.automount.html"><code>.automount</code></a>: automounting</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.swap.html"><code>.swap</code></a>: swap files or partitions</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.target.html"><code>.target</code></a>: only dependencies, like Debian metapackages</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.path.html"><code>.path</code></a>: inotify monitoring of paths</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.timer.html"><code>.timer</code></a>: cron-like activation</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.slice.html"><code>.slice</code></a>: group processes for common resource management</li>
<li><a href="https://www.freedesktop.org/software/systemd/man/systemd.scope.html"><code>.scope</code></a>: group processes for common resource management</li>
</ul>
<p>System unit files can be installed in:</p>
<ul>
<li><code>/lib/systemd/system/</code>: for units provided by packaged software</li>
<li><code>/run/systemd/system/</code>: runtime-generated units</li>
<li><code>/etc/systemd/system/</code>: for units provided by systemd administrators</li>
</ul>
<p>Unit files in <code>/etc/</code> override unit files in <code>/lib/</code>. Note that while Debian
uses <code>/lib/</code>, other distributions may use <code>/usr/lib/</code> instead.</p>
<p>If there is a directory with the same name as the unit file plus a <code>.d</code> suffix,
any file <code>*.conf</code> it contains is parsed after the unit, and can be used to add
or override configuration options.</p>
<p>For example:</p>
<ul>
<li><code>/lib/systemd/system/beep.service.d/foo.conf</code> can be used to tweak
the contents of <code>/lib/systemd/system/beep.service</code>, so it is possible for a
package to distribute a tweak to the configuration of another package.</li>
<li><code>/etc/systemd/system/beep.service.d/foo.conf</code> can be used to tweak the
contents of <code>/lib/systemd/system/beep.service</code>, so it is possible a system
administrator to extend a packaged unit without needing to replace it
entirely.</li>
</ul>
<p>Similarly, a <code>unitname.wants/</code> or <code>unitname.requires/</code> directory can be used to
extend <code>Wants=</code> and <code>Requires=</code> dependencies on other units, by placing
symlinks to other units in them.</p>
<p>See also:</p>
<ul>
<li><a href="https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Managing_Services_with_systemd.html#sect-Managing_Services_with_systemd-Introduction">Introduction to systemd</a>.</li>
<li><a href="https://wiki.debian.org/systemd">systemd on the Debian Wiki</a></li>
<li><a href="https://wiki.archlinux.org/index.php/Systemd">systemd on the Arch Wiki</a></li>
</ul>