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