1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version="5.0"
5 xml:id="sec-systemctl">
6
7<title>Service Management</title>
8
9<para>In NixOS, all system services are started and monitored using
10the systemd program. Systemd is the “init” process of the system
11(i.e. PID 1), the parent of all other processes. It manages a set of
12so-called “units”, which can be things like system services
13(programs), but also mount points, swap files, devices, targets
14(groups of units) and more. Units can have complex dependencies; for
15instance, one unit can require that another unit must be successfully
16started before the first unit can be started. When the system boots,
17it starts a unit named <literal>default.target</literal>; the
18dependencies of this unit cause all system services to be started,
19file systems to be mounted, swap files to be activated, and so
20on.</para>
21
22<para>The command <command>systemctl</command> is the main way to
23interact with <command>systemd</command>. Without any arguments, it
24shows the status of active units:
25
26<screen>
27$ systemctl
28-.mount loaded active mounted /
29swapfile.swap loaded active active /swapfile
30sshd.service loaded active running SSH Daemon
31graphical.target loaded active active Graphical Interface
32<replaceable>...</replaceable>
33</screen>
34
35</para>
36
37<para>You can ask for detailed status information about a unit, for
38instance, the PostgreSQL database service:
39
40<screen>
41$ systemctl status postgresql.service
42postgresql.service - PostgreSQL Server
43 Loaded: loaded (/nix/store/pn3q73mvh75gsrl8w7fdlfk3fq5qm5mw-unit/postgresql.service)
44 Active: active (running) since Mon, 2013-01-07 15:55:57 CET; 9h ago
45 Main PID: 2390 (postgres)
46 CGroup: name=systemd:/system/postgresql.service
47 ├─2390 postgres
48 ├─2418 postgres: writer process
49 ├─2419 postgres: wal writer process
50 ├─2420 postgres: autovacuum launcher process
51 ├─2421 postgres: stats collector process
52 └─2498 postgres: zabbix zabbix [local] idle
53
54Jan 07 15:55:55 hagbard postgres[2394]: [1-1] LOG: database system was shut down at 2013-01-07 15:55:05 CET
55Jan 07 15:55:57 hagbard postgres[2390]: [1-1] LOG: database system is ready to accept connections
56Jan 07 15:55:57 hagbard postgres[2420]: [1-1] LOG: autovacuum launcher started
57Jan 07 15:55:57 hagbard systemd[1]: Started PostgreSQL Server.
58</screen>
59
60Note that this shows the status of the unit (active and running), all
61the processes belonging to the service, as well as the most recent log
62messages from the service.
63
64</para>
65
66<para>Units can be stopped, started or restarted:
67
68<screen>
69# systemctl stop postgresql.service
70# systemctl start postgresql.service
71# systemctl restart postgresql.service
72</screen>
73
74These operations are synchronous: they wait until the service has
75finished starting or stopping (or has failed). Starting a unit will
76cause the dependencies of that unit to be started as well (if
77necessary).</para>
78
79<!-- - cgroups: each service and user session is a cgroup
80
81- cgroup resource management -->
82
83</chapter>