]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd.service.xml
Merge pull request #27685 from keszybz/the-semiannual-excercise-in-output-narrowing
[thirdparty/systemd.git] / man / systemd.service.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="systemd.service">
7 <refentryinfo>
8 <title>systemd.service</title>
9 <productname>systemd</productname>
10 </refentryinfo>
11
12 <refmeta>
13 <refentrytitle>systemd.service</refentrytitle>
14 <manvolnum>5</manvolnum>
15 </refmeta>
16
17 <refnamediv>
18 <refname>systemd.service</refname>
19 <refpurpose>Service unit configuration</refpurpose>
20 </refnamediv>
21
22 <refsynopsisdiv>
23 <para><filename><replaceable>service</replaceable>.service</filename></para>
24 </refsynopsisdiv>
25
26 <refsect1>
27 <title>Description</title>
28
29 <para>A unit configuration file whose name ends in
30 <literal>.service</literal> encodes information about a process
31 controlled and supervised by systemd.</para>
32
33 <para>This man page lists the configuration options specific to
34 this unit type. See
35 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
36 for the common options of all unit configuration files. The common
37 configuration items are configured in the generic
38 [Unit] and [Install]
39 sections. The service specific configuration options are
40 configured in the [Service] section.</para>
41
42 <para>Additional options are listed in
43 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
44 which define the execution environment the commands are executed
45 in, and in
46 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
47 which define the way the processes of the service are terminated,
48 and in
49 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
50 which configure resource control settings for the processes of the
51 service.</para>
52
53 <para>If SysV init compat is enabled, systemd automatically creates service units that wrap SysV init
54 scripts (the service name is the same as the name of the script, with a <literal>.service</literal>
55 suffix added); see
56 <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
57 </para>
58
59 <para>The <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>
60 command allows creating <filename>.service</filename> and <filename>.scope</filename> units dynamically
61 and transiently from the command line.</para>
62 </refsect1>
63
64 <refsect1>
65 <title>Service Templates</title>
66
67 <para>It is possible for <command>systemd</command> services to take a single argument via the
68 <literal><replaceable>service</replaceable>@<replaceable>argument</replaceable>.service</literal>
69 syntax. Such services are called "instantiated" services, while the unit definition without the
70 <replaceable>argument</replaceable> parameter is called a "template". An example could be a
71 <filename>dhcpcd@.service</filename> service template which takes a network interface as a
72 parameter to form an instantiated service. Within the service file, this parameter or "instance
73 name" can be accessed with %-specifiers. See
74 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
75 for details.</para>
76 </refsect1>
77
78 <refsect1>
79 <title>Automatic Dependencies</title>
80
81 <refsect2>
82 <title>Implicit Dependencies</title>
83
84 <para>The following dependencies are implicitly added:</para>
85
86 <itemizedlist>
87 <listitem><para>Services with <varname>Type=dbus</varname> set automatically
88 acquire dependencies of type <varname>Requires=</varname> and
89 <varname>After=</varname> on
90 <filename>dbus.socket</filename>.</para></listitem>
91
92 <listitem><para>Socket activated services are automatically ordered after
93 their activating <filename>.socket</filename> units via an
94 automatic <varname>After=</varname> dependency.
95 Services also pull in all <filename>.socket</filename> units
96 listed in <varname>Sockets=</varname> via automatic
97 <varname>Wants=</varname> and <varname>After=</varname> dependencies.</para></listitem>
98 </itemizedlist>
99
100 <para>Additional implicit dependencies may be added as result of
101 execution and resource control parameters as documented in
102 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
103 and
104 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
105 </refsect2>
106
107 <refsect2>
108 <title>Default Dependencies</title>
109
110 <para>The following dependencies are added unless <varname>DefaultDependencies=no</varname> is set:</para>
111
112 <itemizedlist>
113 <listitem><para>Service units will have dependencies of type <varname>Requires=</varname> and
114 <varname>After=</varname> on <filename>sysinit.target</filename>, a dependency of type <varname>After=</varname> on
115 <filename>basic.target</filename> as well as dependencies of type <varname>Conflicts=</varname> and
116 <varname>Before=</varname> on <filename>shutdown.target</filename>. These ensure that normal service units pull in
117 basic system initialization, and are terminated cleanly prior to system shutdown. Only services involved with early
118 boot or late system shutdown should disable this option.</para></listitem>
119
120 <listitem><para>Instanced service units (i.e. service units with an <literal>@</literal> in their name) are assigned by
121 default a per-template slice unit (see
122 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>), named after the
123 template unit, containing all instances of the specific template. This slice is normally stopped at shutdown,
124 together with all template instances. If that is not desired, set <varname>DefaultDependencies=no</varname> in the
125 template unit, and either define your own per-template slice unit file that also sets
126 <varname>DefaultDependencies=no</varname>, or set <varname>Slice=system.slice</varname> (or another suitable slice)
127 in the template unit. Also see
128 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
129 </para></listitem>
130 </itemizedlist>
131 </refsect2>
132 </refsect1>
133
134 <refsect1>
135 <title>Options</title>
136
137 <para>Service unit files may include [Unit] and [Install] sections, which are described in
138 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
139 </para>
140
141 <para>Service unit files must include a [Service]
142 section, which carries information about the service and the
143 process it supervises. A number of options that may be used in
144 this section are shared with other unit types. These options are
145 documented in
146 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
147 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
148 and
149 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
150 The options specific to the [Service] section
151 of service units are the following:</para>
152
153 <variablelist class='unit-directives'>
154 <varlistentry>
155 <term><varname>Type=</varname></term>
156
157 <listitem>
158 <para>Configures the process start-up type for this service unit. One of <option>simple</option>,
159 <option>exec</option>, <option>forking</option>, <option>oneshot</option>, <option>dbus</option>,
160 <option>notify</option>, <option>notify-reload</option> or <option>idle</option>:</para>
161
162 <itemizedlist>
163 <listitem><para>If set to <option>simple</option> (the default if <varname>ExecStart=</varname> is
164 specified but neither <varname>Type=</varname> nor <varname>BusName=</varname> are), the service manager
165 will consider the unit started immediately after the main service process has been forked off. It is
166 expected that the process configured with <varname>ExecStart=</varname> is the main process of the
167 service. In this mode, if the process offers functionality to other processes on the system, its
168 communication channels should be installed before the service is started up (e.g. sockets set up by
169 systemd, via socket activation), as the service manager will immediately proceed starting follow-up units,
170 right after creating the main service process, and before executing the service's binary. Note that this
171 means <command>systemctl start</command> command lines for <option>simple</option> services will report
172 success even if the service's binary cannot be invoked successfully (for example because the selected
173 <varname>User=</varname> doesn't exist, or the service binary is missing).</para></listitem>
174
175 <listitem><para>The <option>exec</option> type is similar to <option>simple</option>, but the service
176 manager will consider the unit started immediately after the main service binary has been executed. The service
177 manager will delay starting of follow-up units until that point. (Or in other words:
178 <option>simple</option> proceeds with further jobs right after <function>fork()</function> returns, while
179 <option>exec</option> will not proceed before both <function>fork()</function> and
180 <function>execve()</function> in the service process succeeded.) Note that this means <command>systemctl
181 start</command> command lines for <option>exec</option> services will report failure when the service's
182 binary cannot be invoked successfully (for example because the selected <varname>User=</varname> doesn't
183 exist, or the service binary is missing).</para></listitem>
184
185 <listitem><para>If set to <option>forking</option>, it is expected that the process configured with
186 <varname>ExecStart=</varname> will call <function>fork()</function> as part of its start-up. The parent
187 process is expected to exit when start-up is complete and all communication channels are set up. The child
188 continues to run as the main service process, and the service manager will consider the unit started when
189 the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is
190 recommended to also use the <varname>PIDFile=</varname> option, so that systemd can reliably identify the
191 main process of the service. systemd will proceed with starting follow-up units as soon as the parent
192 process exits.</para></listitem>
193
194 <listitem><para>Behavior of <option>oneshot</option> is similar to <option>simple</option>;
195 however, the service manager will consider the unit up after the main process exits. It will then
196 start follow-up units. <varname>RemainAfterExit=</varname> is particularly useful for this type
197 of service. <varname>Type=</varname><option>oneshot</option> is the implied default if neither
198 <varname>Type=</varname> nor <varname>ExecStart=</varname> are specified. Note that if this
199 option is used without <varname>RemainAfterExit=</varname> the service will never enter
200 <literal>active</literal> unit state, but directly transition from <literal>activating</literal>
201 to <literal>deactivating</literal> or <literal>dead</literal> since no process is configured that
202 shall run continuously. In particular this means that after a service of this type ran (and which
203 has <varname>RemainAfterExit=</varname> not set) it will not show up as started afterwards, but
204 as dead.</para></listitem>
205
206 <listitem><para>Behavior of <option>dbus</option> is similar to <option>simple</option>; however,
207 it is expected that the service acquires a name on the D-Bus bus, as configured by
208 <varname>BusName=</varname>. systemd will proceed with starting follow-up units after the D-Bus
209 bus name has been acquired. Service units with this option configured implicitly gain
210 dependencies on the <filename>dbus.socket</filename> unit. This type is the default if
211 <varname>BusName=</varname> is specified. A service unit of this type is considered to be in the
212 activating state until the specified bus name is acquired. It is considered activated while the
213 bus name is taken. Once the bus name is released the service is considered being no longer
214 functional which has the effect that the service manager attempts to terminate any remaining
215 processes belonging to the service. Services that drop their bus name as part of their shutdown
216 logic thus should be prepared to receive a <constant>SIGTERM</constant> (or whichever signal is
217 configured in <varname>KillSignal=</varname>) as result.</para></listitem>
218
219 <listitem><para>Behavior of <option>notify</option> is similar to <option>exec</option>; however,
220 it is expected that the service sends a <literal>READY=1</literal> notification message via
221 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> or
222 an equivalent call when it has finished starting up. systemd will proceed with starting follow-up
223 units after this notification message has been sent. If this option is used,
224 <varname>NotifyAccess=</varname> (see below) should be set to open access to the notification
225 socket provided by systemd. If <varname>NotifyAccess=</varname> is missing or set to
226 <option>none</option>, it will be forcibly set to <option>main</option>.</para></listitem>
227
228 <listitem><para>Behavior of <option>notify-reload</option> is identical to
229 <option>notify</option>. However, it extends the logic in one way: the
230 <constant>SIGHUP</constant> UNIX process signal is sent to the service's main process when the
231 service is asked to reload. (The signal to send can be tweaked via
232 <varname>ReloadSignal=</varname>, see below.) When
233 initiating the reload process the service is then expected to reply with a notification message
234 via <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
235 that contains the <literal>RELOADING=1</literal> field in combination with
236 <literal>MONOTONIC_USEC=</literal> set to the current monotonic time
237 (i.e. <constant>CLOCK_MONOTONIC</constant> in
238 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>)
239 in µs, formatted as decimal string. Once reloading is complete another notification message must
240 be sent, containing <literal>READY=1</literal>. Using this service type and implementing this
241 reload protocol is an efficient alternative to providing an <varname>ExecReload=</varname>
242 command for reloading of the service's configuration.</para></listitem>
243
244 <listitem><para>Behavior of <option>idle</option> is very similar to <option>simple</option>; however,
245 actual execution of the service program is delayed until all active jobs are dispatched. This may be used
246 to avoid interleaving of output of shell services with the status output on the console. Note that this
247 type is useful only to improve console output, it is not useful as a general unit ordering tool, and the
248 effect of this service type is subject to a 5s timeout, after which the service program is invoked
249 anyway.</para></listitem>
250 </itemizedlist>
251
252 <para>It is generally recommended to use <varname>Type=</varname><option>simple</option> for
253 long-running services whenever possible, as it is the simplest and fastest option. However, as this
254 service type won't propagate service start-up failures and doesn't allow ordering of other units
255 against completion of initialization of the service (which for example is useful if clients need to
256 connect to the service through some form of IPC, and the IPC channel is only established by the
257 service itself — in contrast to doing this ahead of time through socket or bus activation or
258 similar), it might not be sufficient for many cases. If so, <option>notify</option>,
259 <option>notify-reload</option> or <option>dbus</option> (the latter only in case the service
260 provides a D-Bus interface) are the preferred options as they allow service program code to
261 precisely schedule when to consider the service started up successfully and when to proceed with
262 follow-up units. The <option>notify</option>/<option>notify-reload</option> service types require
263 explicit support in the service codebase (as <function>sd_notify()</function> or an equivalent API
264 needs to be invoked by the service at the appropriate time) — if it's not supported, then
265 <option>forking</option> is an alternative: it supports the traditional UNIX service start-up
266 protocol. Finally, <option>exec</option> might be an option for cases where it is enough to ensure
267 the service binary is invoked, and where the service binary itself executes no or little
268 initialization on its own (and its initialization is unlikely to fail). Note that using any type
269 other than <option>simple</option> possibly delays the boot process, as the service manager needs
270 to wait for service initialization to complete. It is hence recommended not to needlessly use any
271 types other than <option>simple</option>. (Also note it is generally not recommended to use
272 <option>idle</option> or <option>oneshot</option> for long-running services.)</para>
273 </listitem>
274 </varlistentry>
275
276 <varlistentry>
277 <term><varname>ExitType=</varname></term>
278
279 <listitem>
280 <para>Specifies when the manager should consider the service to be finished. One of <option>main</option> or
281 <option>cgroup</option>:</para>
282
283 <itemizedlist>
284 <listitem><para>If set to <option>main</option> (the default), the service manager
285 will consider the unit stopped when the main process, which is determined according to the
286 <varname>Type=</varname>, exits. Consequently, it cannot be used with
287 <varname>Type=</varname><option>oneshot</option>.</para></listitem>
288
289 <listitem><para>If set to <option>cgroup</option>, the service will be considered running as long as at
290 least one process in the cgroup has not exited.</para></listitem>
291 </itemizedlist>
292
293 <para>It is generally recommended to use <varname>ExitType=</varname><option>main</option> when a service has
294 a known forking model and a main process can reliably be determined. <varname>ExitType=</varname>
295 <option>cgroup</option> is meant for applications whose forking model is not known ahead of time and which
296 might not have a specific main process. It is well suited for transient or automatically generated services,
297 such as graphical applications inside of a desktop environment.</para>
298 </listitem>
299 </varlistentry>
300
301 <varlistentry>
302 <term><varname>RemainAfterExit=</varname></term>
303
304 <listitem><para>Takes a boolean value that specifies whether
305 the service shall be considered active even when all its
306 processes exited. Defaults to <option>no</option>.</para>
307 </listitem>
308 </varlistentry>
309
310 <varlistentry>
311 <term><varname>GuessMainPID=</varname></term>
312
313 <listitem><para>Takes a boolean value that specifies whether
314 systemd should try to guess the main PID of a service if it
315 cannot be determined reliably. This option is ignored unless
316 <option>Type=forking</option> is set and
317 <option>PIDFile=</option> is unset because for the other types
318 or with an explicitly configured PID file, the main PID is
319 always known. The guessing algorithm might come to incorrect
320 conclusions if a daemon consists of more than one process. If
321 the main PID cannot be determined, failure detection and
322 automatic restarting of a service will not work reliably.
323 Defaults to <option>yes</option>.</para>
324 </listitem>
325 </varlistentry>
326
327 <varlistentry>
328 <term><varname>PIDFile=</varname></term>
329
330 <listitem><para>Takes a path referring to the PID file of the service. Usage of this option is recommended for
331 services where <varname>Type=</varname> is set to <option>forking</option>. The path specified typically points
332 to a file below <filename>/run/</filename>. If a relative path is specified it is hence prefixed with
333 <filename>/run/</filename>. The service manager will read the PID of the main process of the service from this
334 file after start-up of the service. The service manager will not write to the file configured here, although it
335 will remove the file after the service has shut down if it still exists. The PID file does not need to be owned
336 by a privileged user, but if it is owned by an unprivileged user additional safety restrictions are enforced:
337 the file may not be a symlink to a file owned by a different user (neither directly nor indirectly), and the
338 PID file must refer to a process already belonging to the service.</para>
339
340 <para>Note that PID files should be avoided in modern projects. Use <option>Type=notify</option>,
341 <option>Type=notify-reload</option> or <option>Type=simple</option> where possible, which does not
342 require use of PID files to determine the main process of a service and avoids needless
343 forking.</para></listitem>
344 </varlistentry>
345
346 <varlistentry>
347 <term><varname>BusName=</varname></term>
348
349 <listitem><para>Takes a D-Bus destination name that this service shall use. This option is mandatory
350 for services where <varname>Type=</varname> is set to <option>dbus</option>. It is recommended to
351 always set this property if known to make it easy to map the service name to the D-Bus destination.
352 In particular, <command>systemctl service-log-level/service-log-target</command> verbs make use of
353 this.</para>
354 </listitem>
355 </varlistentry>
356
357 <varlistentry>
358 <term><varname>ExecStart=</varname></term>
359 <listitem><para>Commands that are executed when this service is started. The value is split into zero
360 or more command lines according to the rules described in the section "Command Lines" below.</para>
361
362 <para>Unless <varname>Type=</varname> is <option>oneshot</option>, exactly one command must be given. When
363 <varname>Type=oneshot</varname> is used, zero or more commands may be specified. Commands may be specified by
364 providing multiple command lines in the same directive, or alternatively, this directive may be specified more
365 than once with the same effect. If the empty string is assigned to this option, the list of commands to start
366 is reset, prior assignments of this option will have no effect. If no <varname>ExecStart=</varname> is
367 specified, then the service must have <varname>RemainAfterExit=yes</varname> and at least one
368 <varname>ExecStop=</varname> line set. (Services lacking both <varname>ExecStart=</varname> and
369 <varname>ExecStop=</varname> are not valid.)</para>
370
371 <para>If more than one command is specified, the commands are
372 invoked sequentially in the order they appear in the unit
373 file. If one of the commands fails (and is not prefixed with
374 <literal>-</literal>), other lines are not executed, and the
375 unit is considered failed.</para>
376
377 <para>Unless <varname>Type=forking</varname> is set, the
378 process started via this command line will be considered the
379 main process of the daemon.</para>
380 </listitem>
381 </varlistentry>
382
383 <varlistentry>
384 <term><varname>ExecStartPre=</varname></term>
385 <term><varname>ExecStartPost=</varname></term>
386 <listitem><para>Additional commands that are executed before
387 or after the command in <varname>ExecStart=</varname>,
388 respectively. Syntax is the same as for
389 <varname>ExecStart=</varname>, except that multiple command
390 lines are allowed and the commands are executed one after the
391 other, serially.</para>
392
393 <para>If any of those commands (not prefixed with
394 <literal>-</literal>) fail, the rest are not executed and the
395 unit is considered failed.</para>
396
397 <para><varname>ExecStart=</varname> commands are only run after
398 all <varname>ExecStartPre=</varname> commands that were not prefixed
399 with a <literal>-</literal> exit successfully.</para>
400
401 <para><varname>ExecStartPost=</varname> commands are only run after the commands specified in
402 <varname>ExecStart=</varname> have been invoked successfully, as determined by
403 <varname>Type=</varname> (i.e. the process has been started for <varname>Type=simple</varname> or
404 <varname>Type=idle</varname>, the last <varname>ExecStart=</varname> process exited successfully for
405 <varname>Type=oneshot</varname>, the initial process exited successfully for
406 <varname>Type=forking</varname>, <literal>READY=1</literal> is sent for
407 <varname>Type=notify</varname>/<varname>Type=notify-reload</varname>, or the
408 <varname>BusName=</varname> has been taken for <varname>Type=dbus</varname>).</para>
409
410 <para>Note that <varname>ExecStartPre=</varname> may not be
411 used to start long-running processes. All processes forked
412 off by processes invoked via <varname>ExecStartPre=</varname> will
413 be killed before the next service process is run.</para>
414
415 <para>Note that if any of the commands specified in <varname>ExecStartPre=</varname>,
416 <varname>ExecStart=</varname>, or <varname>ExecStartPost=</varname> fail (and are not prefixed with
417 <literal>-</literal>, see above) or time out before the service is fully up, execution continues with commands
418 specified in <varname>ExecStopPost=</varname>, the commands in <varname>ExecStop=</varname> are skipped.</para>
419
420 <para>Note that the execution of <varname>ExecStartPost=</varname> is taken into account for the purpose of
421 <varname>Before=</varname>/<varname>After=</varname> ordering constraints.</para>
422 </listitem>
423 </varlistentry>
424
425 <varlistentry>
426 <term><varname>ExecCondition=</varname></term>
427 <listitem><para>Optional commands that are executed before the commands in <varname>ExecStartPre=</varname>.
428 Syntax is the same as for <varname>ExecStart=</varname>, except that multiple command lines are allowed and the
429 commands are executed one after the other, serially.</para>
430
431 <para>The behavior is like an <varname>ExecStartPre=</varname> and condition check hybrid: when an
432 <varname>ExecCondition=</varname> command exits with exit code 1 through 254 (inclusive), the remaining
433 commands are skipped and the unit is <emphasis>not</emphasis> marked as failed. However, if an
434 <varname>ExecCondition=</varname> command exits with 255 or abnormally (e.g. timeout, killed by a
435 signal, etc.), the unit will be considered failed (and remaining commands will be skipped). Exit code of 0 or
436 those matching <varname>SuccessExitStatus=</varname> will continue execution to the next commands.</para>
437
438 <para>The same recommendations about not running long-running processes in <varname>ExecStartPre=</varname>
439 also applies to <varname>ExecCondition=</varname>. <varname>ExecCondition=</varname> will also run the commands
440 in <varname>ExecStopPost=</varname>, as part of stopping the service, in the case of any non-zero or abnormal
441 exits, like the ones described above.</para>
442 </listitem>
443 </varlistentry>
444
445 <varlistentry>
446 <term><varname>ExecReload=</varname></term>
447
448 <listitem><para>Commands to execute to trigger a configuration reload in the service. This argument
449 takes multiple command lines, following the same scheme as described for
450 <varname>ExecStart=</varname> above. Use of this setting is optional. Specifier and environment
451 variable substitution is supported here following the same scheme as for
452 <varname>ExecStart=</varname>.</para>
453
454 <para>One additional, special environment variable is set: if known, <varname>$MAINPID</varname> is
455 set to the main process of the daemon, and may be used for command lines like the following:</para>
456
457 <programlisting>ExecReload=kill -HUP $MAINPID</programlisting>
458
459 <para>Note however that reloading a daemon by enqueuing a signal (as with the example line above) is
460 usually not a good choice, because this is an asynchronous operation and hence not suitable when
461 ordering reloads of multiple services against each other. It is thus strongly recommended to either
462 use <varname>Type=</varname><option>notify-reload</option> in place of
463 <varname>ExecReload=</varname>, or to set <varname>ExecReload=</varname> to a command that not only
464 triggers a configuration reload of the daemon, but also synchronously waits for it to complete. For
465 example, <citerefentry
466 project='mankier'><refentrytitle>dbus-broker</refentrytitle><manvolnum>1</manvolnum></citerefentry>
467 uses the following:</para>
468
469 <programlisting>ExecReload=busctl call org.freedesktop.DBus \
470 /org/freedesktop/DBus org.freedesktop.DBus \
471 ReloadConfig
472 </programlisting>
473 </listitem>
474 </varlistentry>
475
476 <varlistentry>
477 <term><varname>ExecStop=</varname></term>
478 <listitem><para>Commands to execute to stop the service started via
479 <varname>ExecStart=</varname>. This argument takes multiple command lines, following the same scheme
480 as described for <varname>ExecStart=</varname> above. Use of this setting is optional. After the
481 commands configured in this option are run, it is implied that the service is stopped, and any
482 processes remaining for it are terminated according to the <varname>KillMode=</varname> setting (see
483 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
484 If this option is not specified, the process is terminated by sending the signal specified in
485 <varname>KillSignal=</varname> or <varname>RestartKillSignal=</varname> when service stop is
486 requested. Specifier and environment variable substitution is supported (including
487 <varname>$MAINPID</varname>, see above).</para>
488
489 <para>Note that it is usually not sufficient to specify a command for this setting that only asks the
490 service to terminate (for example, by sending some form of termination signal to it), but does not
491 wait for it to do so. Since the remaining processes of the services are killed according to
492 <varname>KillMode=</varname> and <varname>KillSignal=</varname> or
493 <varname>RestartKillSignal=</varname> as described above immediately after the command exited, this
494 may not result in a clean stop. The specified command should hence be a synchronous operation, not an
495 asynchronous one.</para>
496
497 <para>Note that the commands specified in <varname>ExecStop=</varname> are only executed when the service
498 started successfully first. They are not invoked if the service was never started at all, or in case its
499 start-up failed, for example because any of the commands specified in <varname>ExecStart=</varname>,
500 <varname>ExecStartPre=</varname> or <varname>ExecStartPost=</varname> failed (and weren't prefixed with
501 <literal>-</literal>, see above) or timed out. Use <varname>ExecStopPost=</varname> to invoke commands when a
502 service failed to start up correctly and is shut down again. Also note that the stop operation is always
503 performed if the service started successfully, even if the processes in the service terminated on their
504 own or were killed. The stop commands must be prepared to deal with that case. <varname>$MAINPID</varname>
505 will be unset if systemd knows that the main process exited by the time the stop commands are called.</para>
506
507 <para>Service restart requests are implemented as stop operations followed by start operations. This
508 means that <varname>ExecStop=</varname> and <varname>ExecStopPost=</varname> are executed during a
509 service restart operation.</para>
510
511 <para>It is recommended to use this setting for commands that communicate with the service requesting
512 clean termination. For post-mortem clean-up steps use <varname>ExecStopPost=</varname> instead.
513 </para></listitem>
514 </varlistentry>
515
516 <varlistentry>
517 <term><varname>ExecStopPost=</varname></term>
518 <listitem><para>Additional commands that are executed after the service is stopped. This includes cases where
519 the commands configured in <varname>ExecStop=</varname> were used, where the service does not have any
520 <varname>ExecStop=</varname> defined, or where the service exited unexpectedly. This argument takes multiple
521 command lines, following the same scheme as described for <varname>ExecStart=</varname>. Use of these settings
522 is optional. Specifier and environment variable substitution is supported. Note that – unlike
523 <varname>ExecStop=</varname> – commands specified with this setting are invoked when a service failed to start
524 up correctly and is shut down again.</para>
525
526 <para>It is recommended to use this setting for clean-up operations that shall be executed even when the
527 service failed to start up correctly. Commands configured with this setting need to be able to operate even if
528 the service failed starting up half-way and left incompletely initialized data around. As the service's
529 processes have been terminated already when the commands specified with this setting are executed they should
530 not attempt to communicate with them.</para>
531
532 <para>Note that all commands that are configured with this setting are invoked with the result code of the
533 service, as well as the main process' exit code and status, set in the <varname>$SERVICE_RESULT</varname>,
534 <varname>$EXIT_CODE</varname> and <varname>$EXIT_STATUS</varname> environment variables, see
535 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
536 details.</para>
537
538 <para>Note that the execution of <varname>ExecStopPost=</varname> is taken into account for the purpose of
539 <varname>Before=</varname>/<varname>After=</varname> ordering constraints.</para></listitem>
540 </varlistentry>
541
542 <varlistentry>
543 <term><varname>RestartSec=</varname></term>
544 <listitem><para>Configures the time to sleep before restarting
545 a service (as configured with <varname>Restart=</varname>).
546 Takes a unit-less value in seconds, or a time span value such
547 as "5min 20s". Defaults to 100ms.</para></listitem>
548 </varlistentry>
549
550 <varlistentry>
551 <term><varname>RestartSteps=</varname></term>
552 <listitem><para>Configures the number of steps to take to increase the interval
553 of auto-restarts from <varname>RestartSec=</varname> to <varname>RestartMaxDelaySec=</varname>.
554 Takes a positive integer or 0 to disable it. Defaults to 0.</para>
555
556 <para>This setting is effective only if <varname>RestartMaxDelaySec=</varname> is also set.</para></listitem>
557 </varlistentry>
558
559 <varlistentry>
560 <term><varname>RestartMaxDelaySec=</varname></term>
561 <listitem><para>Configures the longest time to sleep before restarting a service
562 as the interval goes up with <varname>RestartSteps=</varname>. Takes a value
563 in the same format as <varname>RestartSec=</varname>, or <literal>infinity</literal>
564 to disable the setting. Defaults to <literal>infinity</literal>.</para>
565
566 <para>This setting is effective only if <varname>RestartSteps=</varname> is also set.</para></listitem>
567 </varlistentry>
568
569 <varlistentry>
570 <term><varname>TimeoutStartSec=</varname></term>
571 <listitem><para>Configures the time to wait for start-up. If a daemon service does not signal
572 start-up completion within the configured time, the service will be considered failed and will be
573 shut down again. The precise action depends on the <varname>TimeoutStartFailureMode=</varname>
574 option. Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass
575 <literal>infinity</literal> to disable the timeout logic. Defaults to
576 <varname>DefaultTimeoutStartSec=</varname> set in the manager, except when
577 <varname>Type=oneshot</varname> is used, in which case the timeout is disabled by default (see
578 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
579 </para>
580
581 <para>If a service of <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> sends
582 <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause the start time to be extended beyond
583 <varname>TimeoutStartSec=</varname>. The first receipt of this message must occur before
584 <varname>TimeoutStartSec=</varname> is exceeded, and once the start time has extended beyond
585 <varname>TimeoutStartSec=</varname>, the service manager will allow the service to continue to start,
586 provided the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified
587 until the service startup status is finished by <literal>READY=1</literal>. (see
588 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
589 </para></listitem>
590 </varlistentry>
591
592 <varlistentry>
593 <term><varname>TimeoutStopSec=</varname></term>
594 <listitem><para>This option serves two purposes. First, it configures the time to wait for each
595 <varname>ExecStop=</varname> command. If any of them times out, subsequent <varname>ExecStop=</varname> commands
596 are skipped and the service will be terminated by <constant>SIGTERM</constant>. If no <varname>ExecStop=</varname>
597 commands are specified, the service gets the <constant>SIGTERM</constant> immediately. This default behavior
598 can be changed by the <varname>TimeoutStopFailureMode=</varname> option. Second, it configures the time
599 to wait for the service itself to stop. If it doesn't terminate in the specified time, it will be forcibly terminated
600 by <constant>SIGKILL</constant> (see <varname>KillMode=</varname> in
601 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
602 Takes a unit-less value in seconds, or a time span value such
603 as "5min 20s". Pass <literal>infinity</literal> to disable the
604 timeout logic. Defaults to
605 <varname>DefaultTimeoutStopSec=</varname> from the manager
606 configuration file (see
607 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
608 </para>
609
610 <para>If a service of <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> sends
611 <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause the stop time to be extended beyond
612 <varname>TimeoutStopSec=</varname>. The first receipt of this message must occur before
613 <varname>TimeoutStopSec=</varname> is exceeded, and once the stop time has extended beyond
614 <varname>TimeoutStopSec=</varname>, the service manager will allow the service to continue to stop,
615 provided the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified,
616 or terminates itself (see
617 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
618 </para></listitem>
619 </varlistentry>
620
621 <varlistentry>
622 <term><varname>TimeoutAbortSec=</varname></term>
623 <listitem><para>This option configures the time to wait for the service to terminate when it was aborted due to a
624 watchdog timeout (see <varname>WatchdogSec=</varname>). If the service has a short <varname>TimeoutStopSec=</varname>
625 this option can be used to give the system more time to write a core dump of the service. Upon expiration the service
626 will be forcibly terminated by <constant>SIGKILL</constant> (see <varname>KillMode=</varname> in
627 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). The core file will
628 be truncated in this case. Use <varname>TimeoutAbortSec=</varname> to set a sensible timeout for the core dumping per
629 service that is large enough to write all expected data while also being short enough to handle the service failure
630 in due time.
631 </para>
632
633 <para>Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass an empty value to skip
634 the dedicated watchdog abort timeout handling and fall back <varname>TimeoutStopSec=</varname>. Pass
635 <literal>infinity</literal> to disable the timeout logic. Defaults to <varname>DefaultTimeoutAbortSec=</varname> from
636 the manager configuration file (see
637 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
638 </para>
639
640 <para>If a service of <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> handles
641 <constant>SIGABRT</constant> itself (instead of relying on the kernel to write a core dump) it can
642 send <literal>EXTEND_TIMEOUT_USEC=…</literal> to extended the abort time beyond
643 <varname>TimeoutAbortSec=</varname>. The first receipt of this message must occur before
644 <varname>TimeoutAbortSec=</varname> is exceeded, and once the abort time has extended beyond
645 <varname>TimeoutAbortSec=</varname>, the service manager will allow the service to continue to abort,
646 provided the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified,
647 or terminates itself (see
648 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
649 </para></listitem>
650 </varlistentry>
651
652 <varlistentry>
653 <term><varname>TimeoutSec=</varname></term>
654 <listitem><para>A shorthand for configuring both
655 <varname>TimeoutStartSec=</varname> and
656 <varname>TimeoutStopSec=</varname> to the specified value.
657 </para></listitem>
658 </varlistentry>
659
660 <varlistentry>
661 <term><varname>TimeoutStartFailureMode=</varname></term>
662 <term><varname>TimeoutStopFailureMode=</varname></term>
663
664 <listitem><para>These options configure the action that is taken in case a daemon service does not signal
665 start-up within its configured <varname>TimeoutStartSec=</varname>, respectively if it does not stop within
666 <varname>TimeoutStopSec=</varname>. Takes one of <option>terminate</option>, <option>abort</option> and
667 <option>kill</option>. Both options default to <option>terminate</option>.</para>
668
669 <para>If <option>terminate</option> is set the service will be gracefully terminated by sending the signal
670 specified in <varname>KillSignal=</varname> (defaults to <constant>SIGTERM</constant>, see
671 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If the
672 service does not terminate the <varname>FinalKillSignal=</varname> is sent after
673 <varname>TimeoutStopSec=</varname>. If <option>abort</option> is set, <varname>WatchdogSignal=</varname> is sent
674 instead and <varname>TimeoutAbortSec=</varname> applies before sending <varname>FinalKillSignal=</varname>.
675 This setting may be used to analyze services that fail to start-up or shut-down intermittently.
676 By using <option>kill</option> the service is immediately terminated by sending
677 <varname>FinalKillSignal=</varname> without any further timeout. This setting can be used to expedite the
678 shutdown of failing services.
679 </para></listitem>
680 </varlistentry>
681
682 <varlistentry>
683 <term><varname>RuntimeMaxSec=</varname></term>
684
685 <listitem><para>Configures a maximum time for the service to run. If this is used and the service has been
686 active for longer than the specified time it is terminated and put into a failure state. Note that this setting
687 does not have any effect on <varname>Type=oneshot</varname> services, as they terminate immediately after
688 activation completed. Pass <literal>infinity</literal> (the default) to configure no runtime
689 limit.</para>
690
691 <para>If a service of <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> sends
692 <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause the runtime to be extended beyond
693 <varname>RuntimeMaxSec=</varname>. The first receipt of this message must occur before
694 <varname>RuntimeMaxSec=</varname> is exceeded, and once the runtime has extended beyond
695 <varname>RuntimeMaxSec=</varname>, the service manager will allow the service to continue to run,
696 provided the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified
697 until the service shutdown is achieved by <literal>STOPPING=1</literal> (or termination). (see
698 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
699 </para></listitem>
700 </varlistentry>
701
702 <varlistentry>
703 <term><varname>RuntimeRandomizedExtraSec=</varname></term>
704
705 <listitem><para>This option modifies <varname>RuntimeMaxSec=</varname> by increasing the maximum runtime by an
706 evenly distributed duration between 0 and the specified value (in seconds). If <varname>RuntimeMaxSec=</varname> is
707 unspecified, then this feature will be disabled.
708 </para></listitem>
709 </varlistentry>
710
711 <varlistentry>
712 <term><varname>WatchdogSec=</varname></term>
713 <listitem><para>Configures the watchdog timeout for a service.
714 The watchdog is activated when the start-up is completed. The
715 service must call
716 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
717 regularly with <literal>WATCHDOG=1</literal> (i.e. the
718 "keep-alive ping"). If the time between two such calls is
719 larger than the configured time, then the service is placed in
720 a failed state and it will be terminated with
721 <constant>SIGABRT</constant> (or the signal specified by
722 <varname>WatchdogSignal=</varname>). By setting
723 <varname>Restart=</varname> to <option>on-failure</option>,
724 <option>on-watchdog</option>, <option>on-abnormal</option> or
725 <option>always</option>, the service will be automatically
726 restarted. The time configured here will be passed to the
727 executed service process in the
728 <varname>WATCHDOG_USEC=</varname> environment variable. This
729 allows daemons to automatically enable the keep-alive pinging
730 logic if watchdog support is enabled for the service. If this
731 option is used, <varname>NotifyAccess=</varname> (see below)
732 should be set to open access to the notification socket
733 provided by systemd. If <varname>NotifyAccess=</varname> is
734 not set, it will be implicitly set to <option>main</option>.
735 Defaults to 0, which disables this feature. The service can
736 check whether the service manager expects watchdog keep-alive
737 notifications. See
738 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
739 for details.
740 <citerefentry><refentrytitle>sd_event_set_watchdog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
741 may be used to enable automatic watchdog notification support.
742 </para></listitem>
743 </varlistentry>
744
745 <varlistentry>
746 <term><varname>Restart=</varname></term>
747 <listitem><para>Configures whether the service shall be
748 restarted when the service process exits, is killed, or a
749 timeout is reached. The service process may be the main
750 service process, but it may also be one of the processes
751 specified with <varname>ExecStartPre=</varname>,
752 <varname>ExecStartPost=</varname>,
753 <varname>ExecStop=</varname>,
754 <varname>ExecStopPost=</varname>, or
755 <varname>ExecReload=</varname>. When the death of the process
756 is a result of systemd operation (e.g. service stop or
757 restart), the service will not be restarted. Timeouts include
758 missing the watchdog "keep-alive ping" deadline and a service
759 start, reload, and stop operation timeouts.</para>
760
761 <para>Takes one of
762 <option>no</option>,
763 <option>on-success</option>,
764 <option>on-failure</option>,
765 <option>on-abnormal</option>,
766 <option>on-watchdog</option>,
767 <option>on-abort</option>, or
768 <option>always</option>.
769 If set to <option>no</option> (the default), the service will
770 not be restarted. If set to <option>on-success</option>, it
771 will be restarted only when the service process exits cleanly.
772 In this context, a clean exit means any of the following:
773 <itemizedlist>
774 <listitem><simpara>exit code of 0;</simpara></listitem>
775 <listitem><simpara>for types other than
776 <varname>Type=oneshot</varname>, one of the signals
777 <constant>SIGHUP</constant>,
778 <constant>SIGINT</constant>,
779 <constant>SIGTERM</constant>, or
780 <constant>SIGPIPE</constant>;</simpara></listitem>
781 <listitem><simpara>exit statuses and signals specified in
782 <varname>SuccessExitStatus=</varname>.</simpara></listitem>
783 </itemizedlist>
784 If set to
785 <option>on-failure</option>, the service will be restarted
786 when the process exits with a non-zero exit code, is
787 terminated by a signal (including on core dump, but excluding
788 the aforementioned four signals), when an operation (such as
789 service reload) times out, and when the configured watchdog
790 timeout is triggered. If set to <option>on-abnormal</option>,
791 the service will be restarted when the process is terminated
792 by a signal (including on core dump, excluding the
793 aforementioned four signals), when an operation times out, or
794 when the watchdog timeout is triggered. If set to
795 <option>on-abort</option>, the service will be restarted only
796 if the service process exits due to an uncaught signal not
797 specified as a clean exit status. If set to
798 <option>on-watchdog</option>, the service will be restarted
799 only if the watchdog timeout for the service expires. If set
800 to <option>always</option>, the service will be restarted
801 regardless of whether it exited cleanly or not, got terminated
802 abnormally by a signal, or hit a timeout.</para>
803
804 <table>
805 <title>Exit causes and the effect of the <varname>Restart=</varname> settings</title>
806
807 <tgroup cols='2'>
808 <colspec colname='path' />
809 <colspec colname='expl' />
810 <thead>
811 <row>
812 <entry>Restart settings/Exit causes</entry>
813 <entry><option>no</option></entry>
814 <entry><option>always</option></entry>
815 <entry><option>on-success</option></entry>
816 <entry><option>on-failure</option></entry>
817 <entry><option>on-abnormal</option></entry>
818 <entry><option>on-abort</option></entry>
819 <entry><option>on-watchdog</option></entry>
820 </row>
821 </thead>
822 <tbody>
823 <row>
824 <entry>Clean exit code or signal</entry>
825 <entry/>
826 <entry>X</entry>
827 <entry>X</entry>
828 <entry/>
829 <entry/>
830 <entry/>
831 <entry/>
832 </row>
833 <row>
834 <entry>Unclean exit code</entry>
835 <entry/>
836 <entry>X</entry>
837 <entry/>
838 <entry>X</entry>
839 <entry/>
840 <entry/>
841 <entry/>
842 </row>
843 <row>
844 <entry>Unclean signal</entry>
845 <entry/>
846 <entry>X</entry>
847 <entry/>
848 <entry>X</entry>
849 <entry>X</entry>
850 <entry>X</entry>
851 <entry/>
852 </row>
853 <row>
854 <entry>Timeout</entry>
855 <entry/>
856 <entry>X</entry>
857 <entry/>
858 <entry>X</entry>
859 <entry>X</entry>
860 <entry/>
861 <entry/>
862 </row>
863 <row>
864 <entry>Watchdog</entry>
865 <entry/>
866 <entry>X</entry>
867 <entry/>
868 <entry>X</entry>
869 <entry>X</entry>
870 <entry/>
871 <entry>X</entry>
872 </row>
873 </tbody>
874 </tgroup>
875 </table>
876
877 <para>As exceptions to the setting above, the service will not
878 be restarted if the exit code or signal is specified in
879 <varname>RestartPreventExitStatus=</varname> (see below) or
880 the service is stopped with <command>systemctl stop</command>
881 or an equivalent operation. Also, the services will always be
882 restarted if the exit code or signal is specified in
883 <varname>RestartForceExitStatus=</varname> (see below).</para>
884
885 <para>Note that service restart is subject to unit start rate
886 limiting configured with <varname>StartLimitIntervalSec=</varname>
887 and <varname>StartLimitBurst=</varname>, see
888 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
889 for details.</para>
890
891 <para>Setting this to <option>on-failure</option> is the
892 recommended choice for long-running services, in order to
893 increase reliability by attempting automatic recovery from
894 errors. For services that shall be able to terminate on their
895 own choice (and avoid immediate restarting),
896 <option>on-abnormal</option> is an alternative choice.</para>
897 </listitem>
898 </varlistentry>
899
900 <varlistentry>
901 <term><varname>SuccessExitStatus=</varname></term>
902
903 <listitem><para>Takes a list of exit status definitions that, when returned by the main service
904 process, will be considered successful termination, in addition to the normal successful exit status
905 0 and, except for <varname>Type=oneshot</varname>, the signals <constant>SIGHUP</constant>, <constant>SIGINT</constant>,
906 <constant>SIGTERM</constant>, and <constant>SIGPIPE</constant>. Exit status definitions can be
907 numeric termination statuses, termination status names, or termination signal names, separated by
908 spaces. See the Process Exit Codes section in
909 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
910 a list of termination status names (for this setting only the part without the
911 <literal>EXIT_</literal> or <literal>EX_</literal> prefix should be used). See <citerefentry
912 project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
913 a list of signal names.</para>
914
915 <para>Note that this setting does not change the mapping between numeric exit statuses and their
916 names, i.e. regardless how this setting is used 0 will still be mapped to <literal>SUCCESS</literal>
917 (and thus typically shown as <literal>0/SUCCESS</literal> in tool outputs) and 1 to
918 <literal>FAILURE</literal> (and thus typically shown as <literal>1/FAILURE</literal>), and so on. It
919 only controls what happens as effect of these exit statuses, and how it propagates to the state of
920 the service as a whole.</para>
921
922 <para>This option may appear more than once, in which case the list of successful exit statuses is
923 merged. If the empty string is assigned to this option, the list is reset, all prior assignments of
924 this option will have no effect.</para>
925
926 <example>
927 <title>A service with the <varname>SuccessExitStatus=</varname> setting</title>
928
929 <programlisting>SuccessExitStatus=TEMPFAIL 250 SIGKILL</programlisting>
930
931 <para>Exit status 75 (<constant>TEMPFAIL</constant>), 250, and the termination signal
932 <constant>SIGKILL</constant> are considered clean service terminations.</para>
933 </example>
934
935 <para>Note: <command>systemd-analyze exit-status</command> may be used to list exit statuses and
936 translate between numerical status values and names.</para></listitem>
937 </varlistentry>
938
939 <varlistentry>
940 <term><varname>RestartPreventExitStatus=</varname></term>
941
942 <listitem><para>Takes a list of exit status definitions that, when returned by the main service
943 process, will prevent automatic service restarts, regardless of the restart setting configured with
944 <varname>Restart=</varname>. Exit status definitions can either be numeric exit codes or termination
945 signal names, and are separated by spaces. Defaults to the empty list, so that, by default, no exit
946 status is excluded from the configured restart logic. For example:
947
948 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting>
949
950 ensures that exit codes 1 and 6 and the termination signal <constant>SIGABRT</constant> will not
951 result in automatic service restarting. This option may appear more than once, in which case the list
952 of restart-preventing statuses is merged. If the empty string is assigned to this option, the list is
953 reset and all prior assignments of this option will have no effect.</para>
954
955 <para>Note that this setting has no effect on processes configured via
956 <varname>ExecStartPre=</varname>, <varname>ExecStartPost=</varname>, <varname>ExecStop=</varname>,
957 <varname>ExecStopPost=</varname> or <varname>ExecReload=</varname>, but only on the main service
958 process, i.e. either the one invoked by <varname>ExecStart=</varname> or (depending on
959 <varname>Type=</varname>, <varname>PIDFile=</varname>, …) the otherwise configured main
960 process.</para></listitem>
961 </varlistentry>
962
963 <varlistentry>
964 <term><varname>RestartForceExitStatus=</varname></term>
965 <listitem><para>Takes a list of exit status definitions that,
966 when returned by the main service process, will force automatic
967 service restarts, regardless of the restart setting configured
968 with <varname>Restart=</varname>. The argument format is
969 similar to
970 <varname>RestartPreventExitStatus=</varname>.</para></listitem>
971 </varlistentry>
972
973 <varlistentry>
974 <term><varname>RootDirectoryStartOnly=</varname></term>
975 <listitem><para>Takes a boolean argument. If true, the root
976 directory, as configured with the
977 <varname>RootDirectory=</varname> option (see
978 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
979 for more information), is only applied to the process started
980 with <varname>ExecStart=</varname>, and not to the various
981 other <varname>ExecStartPre=</varname>,
982 <varname>ExecStartPost=</varname>,
983 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
984 and <varname>ExecStopPost=</varname> commands. If false, the
985 setting is applied to all configured commands the same way.
986 Defaults to false.</para></listitem>
987 </varlistentry>
988
989 <varlistentry>
990 <term><varname>NonBlocking=</varname></term>
991 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag for all file descriptors passed via socket-based
992 activation. If true, all file descriptors >= 3 (i.e. all except stdin, stdout, stderr), excluding those passed
993 in via the file descriptor storage logic (see <varname>FileDescriptorStoreMax=</varname> for details), will
994 have the <constant>O_NONBLOCK</constant> flag set and hence are in non-blocking mode. This option is only
995 useful in conjunction with a socket unit, as described in
996 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> and has no
997 effect on file descriptors which were previously saved in the file-descriptor store for example. Defaults to
998 false.</para></listitem>
999 </varlistentry>
1000
1001 <varlistentry>
1002 <term><varname>NotifyAccess=</varname></term>
1003 <listitem><para>Controls access to the service status notification socket, as accessible via the
1004 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
1005 call. Takes one of <option>none</option> (the default), <option>main</option>, <option>exec</option>
1006 or <option>all</option>. If <option>none</option>, no daemon status updates are accepted from the
1007 service processes, all status update messages are ignored. If <option>main</option>, only service
1008 updates sent from the main process of the service are accepted. If <option>exec</option>, only
1009 service updates sent from any of the main or control processes originating from one of the
1010 <varname>Exec*=</varname> commands are accepted. If <option>all</option>, all services updates from
1011 all members of the service's control group are accepted. This option should be set to open access to
1012 the notification socket when using
1013 <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> or
1014 <varname>WatchdogSec=</varname> (see above). If those options are used but
1015 <varname>NotifyAccess=</varname> is not configured, it will be implicitly set to
1016 <option>main</option>.</para>
1017
1018 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if
1019 either the sending process is still around at the time PID 1 processes the message, or if the sending process
1020 is explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally
1021 forked off the process, i.e. on all processes that match <option>main</option> or
1022 <option>exec</option>. Conversely, if an auxiliary process of the unit sends an
1023 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to
1024 properly attribute the message to the unit, and thus will ignore it, even if
1025 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para>
1026
1027 <para>Hence, to eliminate all race conditions involving lookup of the client's unit and attribution of notifications
1028 to units correctly, <function>sd_notify_barrier()</function> may be used. This call acts as a synchronization point
1029 and ensures all notifications sent before this call have been picked up by the service manager when it returns
1030 successfully. Use of <function>sd_notify_barrier()</function> is needed for clients which are not invoked by the
1031 service manager, otherwise this synchronization mechanism is unnecessary for attribution of notifications to the
1032 unit.</para></listitem>
1033 </varlistentry>
1034
1035 <varlistentry>
1036 <term><varname>Sockets=</varname></term>
1037 <listitem><para>Specifies the name of the socket units this
1038 service shall inherit socket file descriptors from when the
1039 service is started. Normally, it should not be necessary to use
1040 this setting, as all socket file descriptors whose unit shares
1041 the same name as the service (subject to the different unit
1042 name suffix of course) are passed to the spawned
1043 process.</para>
1044
1045 <para>Note that the same socket file descriptors may be passed
1046 to multiple processes simultaneously. Also note that a
1047 different service may be activated on incoming socket traffic
1048 than the one which is ultimately configured to inherit the
1049 socket file descriptors. Or, in other words: the
1050 <varname>Service=</varname> setting of
1051 <filename>.socket</filename> units does not have to match the
1052 inverse of the <varname>Sockets=</varname> setting of the
1053 <filename>.service</filename> it refers to.</para>
1054
1055 <para>This option may appear more than once, in which case the list of socket units is merged. Note
1056 that once set, clearing the list of sockets again (for example, by assigning the empty string to this
1057 option) is not supported.</para></listitem>
1058 </varlistentry>
1059
1060 <varlistentry>
1061 <term><varname>FileDescriptorStoreMax=</varname></term>
1062 <listitem><para>Configure how many file descriptors may be stored in the service manager for the
1063 service using
1064 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
1065 <literal>FDSTORE=1</literal> messages. This is useful for implementing services that can restart
1066 after an explicit request or a crash without losing state. Any open sockets and other file
1067 descriptors which should not be closed during the restart may be stored this way. Application state
1068 can either be serialized to a file in <varname>RuntimeDirectory=</varname>, or stored in a
1069 <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry>
1070 memory file descriptor. Defaults to 0, i.e. no file descriptors may be stored in the service
1071 manager. All file descriptors passed to the service manager from a specific service are passed back
1072 to the service's main process on the next service restart (see
1073 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
1074 details about the precise protocol used and the order in which the file descriptors are passed). Any
1075 file descriptors passed to the service manager are automatically closed when
1076 <constant>POLLHUP</constant> or <constant>POLLERR</constant> is seen on them, or when the service is
1077 fully stopped and no job is queued or being executed for it (the latter can be tweaked with
1078 <varname>FileDescriptorStorePreserve=</varname>, see below). If this option is used,
1079 <varname>NotifyAccess=</varname> (see above) should be set to open access to the notification socket
1080 provided by systemd. If <varname>NotifyAccess=</varname> is not set, it will be implicitly set to
1081 <option>main</option>.</para>
1082
1083 <para>The <command>fdstore</command> command of
1084 <citerefentry><refentrytitle>systemd-analyze</refentrytitle><manvolnum>1</manvolnum></citerefentry>
1085 may be used to list the current contents of a service's file descriptor store.</para>
1086
1087 <para>Note that the service manager will only pass file descriptors contained in the file descriptor
1088 store to the service's own processes, never to other clients via IPC or similar. However, it does
1089 allow unprivileged clients to query the list of currently open file descriptors of a
1090 service. Sensitive data may hence be safely placed inside the referenced files, but should not be
1091 attached to the metadata (e.g. included in filenames) of the stored file
1092 descriptors.</para>
1093
1094 <para>If this option is set to a non-zero value the <varname>$FDSTORE</varname> environment variable
1095 will be set for processes invoked for this service. See
1096 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
1097 details.</para></listitem>
1098 </varlistentry>
1099
1100 <varlistentry>
1101 <term><varname>FileDescriptorStorePreserve=</varname></term>
1102 <listitem><para>Takes one of <constant>no</constant>, <constant>yes</constant>,
1103 <constant>restart</constant> and controls when to release the service's file descriptor store
1104 (i.e. when to close the contained file descriptors, if any). If set to <constant>no</constant> the
1105 file descriptor store is automatically released when the service is stopped; if
1106 <constant>restart</constant> (the default) it is kept around as long as the unit is neither inactive
1107 nor failed, or a job is queued for the service, or the service is expected to be restarted. If
1108 <constant>yes</constant> the file descriptor store is kept around until the unit is removed from
1109 memory (i.e. is not referenced anymore and inactive). The latter is useful to keep entries in the
1110 file descriptor store pinned until the service manage exits.</para>
1111
1112 <para>Use <command>systemctl clean --what=fdstore …</command> to release the file descriptor store
1113 explicitly.</para></listitem>
1114 </varlistentry>
1115
1116 <varlistentry>
1117 <term><varname>USBFunctionDescriptors=</varname></term>
1118 <listitem><para>Configure the location of a file containing
1119 <ulink
1120 url="https://docs.kernel.org/usb/functionfs.html">USB
1121 FunctionFS</ulink> descriptors, for implementation of USB
1122 gadget functions. This is used only in conjunction with a
1123 socket unit with <varname>ListenUSBFunction=</varname>
1124 configured. The contents of this file are written to the
1125 <filename>ep0</filename> file after it is
1126 opened.</para></listitem>
1127 </varlistentry>
1128
1129 <varlistentry>
1130 <term><varname>USBFunctionStrings=</varname></term>
1131 <listitem><para>Configure the location of a file containing
1132 USB FunctionFS strings. Behavior is similar to
1133 <varname>USBFunctionDescriptors=</varname>
1134 above.</para></listitem>
1135 </varlistentry>
1136
1137 <varlistentry id='oom-policy'>
1138 <term><varname>OOMPolicy=</varname></term>
1139
1140 <listitem><para>Configure the out-of-memory (OOM) killing policy for the kernel and the userspace OOM
1141 killer
1142 <citerefentry><refentrytitle>systemd-oomd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
1143 On Linux, when memory becomes scarce to the point that the kernel has trouble allocating memory for
1144 itself, it might decide to kill a running process in order to free up memory and reduce memory
1145 pressure. Note that <filename>systemd-oomd.service</filename> is a more flexible solution that aims
1146 to prevent out-of-memory situations for the userspace too, not just the kernel, by attempting to
1147 terminate services earlier, before the kernel would have to act.</para>
1148
1149 <para>This setting takes one of <constant>continue</constant>, <constant>stop</constant> or
1150 <constant>kill</constant>. If set to <constant>continue</constant> and a process in the unit is
1151 killed by the OOM killer, this is logged but the unit continues running. If set to
1152 <constant>stop</constant> the event is logged but the unit is terminated cleanly by the service
1153 manager. If set to <constant>kill</constant> and one of the unit's processes is killed by the OOM
1154 killer the kernel is instructed to kill all remaining processes of the unit too, by setting the
1155 <filename>memory.oom.group</filename> attribute to <constant>1</constant>; also see <ulink
1156 url="https://docs.kernel.org/admin-guide/cgroup-v2.html">kernel documentation</ulink>.</para>
1157
1158 <para>Defaults to the setting <varname>DefaultOOMPolicy=</varname> in
1159 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1160 is set to, except for units where <varname>Delegate=</varname> is turned on, where it defaults to
1161 <constant>continue</constant>.</para>
1162
1163 <para>Use the <varname>OOMScoreAdjust=</varname> setting to configure whether processes of the unit
1164 shall be considered preferred or less preferred candidates for process termination by the Linux OOM
1165 killer logic. See
1166 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
1167 details.</para>
1168
1169 <para>This setting also applies to
1170 <citerefentry><refentrytitle>systemd-oomd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
1171 Similarly to the kernel OOM kills performed by the kernel, this setting determines the state of the
1172 unit after <command>systemd-oomd</command> kills a cgroup associated with it.</para></listitem>
1173 </varlistentry>
1174
1175 <varlistentry>
1176 <term><varname>OpenFile=</varname></term>
1177 <listitem><para>Takes an argument of the form <literal>path<optional><replaceable>:fd-name:options</replaceable></optional></literal>,
1178 where:
1179 <itemizedlist>
1180 <listitem><simpara><literal>path</literal> is a path to a file or an <constant>AF_UNIX</constant> socket in the file system;</simpara></listitem>
1181 <listitem><simpara><literal>fd-name</literal> is a name that will be associated with the file descriptor;
1182 the name may contain any ASCII character, but must exclude control characters and ":", and must be at most 255 characters in length;
1183 it is optional and, if not provided, defaults to the file name;</simpara></listitem>
1184 <listitem><simpara><literal>options</literal> is a comma-separated list of access options;
1185 possible values are
1186 <literal>read-only</literal>,
1187 <literal>append</literal>,
1188 <literal>truncate</literal>,
1189 <literal>graceful</literal>;
1190 if not specified, files will be opened in <constant>rw</constant> mode;
1191 if <literal>graceful</literal> is specified, errors during file/socket opening are ignored.
1192 Specifying the same option several times is treated as an error.</simpara></listitem>
1193 </itemizedlist>
1194 The file or socket is opened by the service manager and the file descriptor is passed to the service.
1195 If the path is a socket, we call <function>connect()</function> on it.
1196 See <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
1197 for more details on how to retrieve these file descriptors.</para>
1198
1199 <para>This setting is useful to allow services to access files/sockets that they can't access themselves
1200 (due to running in a separate mount namespace, not having privileges, ...).</para>
1201
1202 <para>This setting can be specified multiple times, in which case all the specified paths are opened and the file descriptors passed to the service.
1203 If the empty string is assigned, the entire list of open files defined prior to this is reset.</para></listitem>
1204 </varlistentry>
1205
1206 <varlistentry>
1207 <term><varname>ReloadSignal=</varname></term>
1208 <listitem><para>Configures the UNIX process signal to send to the service's main process when asked
1209 to reload the service's configuration. Defaults to <constant>SIGHUP</constant>. This option has no
1210 effect unless <varname>Type=</varname><option>notify-reload</option> is used, see
1211 above.</para></listitem>
1212 </varlistentry>
1213
1214 </variablelist>
1215
1216 <para id='shared-unit-options'>Check
1217 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1218 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, and
1219 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more
1220 settings.</para>
1221 </refsect1>
1222
1223 <refsect1>
1224 <title>Command lines</title>
1225
1226 <para>This section describes command line parsing and
1227 variable and specifier substitutions for
1228 <varname>ExecStart=</varname>,
1229 <varname>ExecStartPre=</varname>,
1230 <varname>ExecStartPost=</varname>,
1231 <varname>ExecReload=</varname>,
1232 <varname>ExecStop=</varname>, and
1233 <varname>ExecStopPost=</varname> options.</para>
1234
1235 <para>Multiple command lines may be concatenated in a single directive by separating them with semicolons
1236 (these semicolons must be passed as separate words). Lone semicolons may be escaped as
1237 <literal>\;</literal>.</para>
1238
1239 <para>Each command line is unquoted using the rules described in "Quoting" section in
1240 <citerefentry><refentrytitle>systemd.syntax</refentrytitle><manvolnum>7</manvolnum></citerefentry>. The
1241 first item becomes the command to execute, and the subsequent items the arguments.</para>
1242
1243 <para>This syntax is inspired by shell syntax, but only the meta-characters and expansions
1244 described in the following paragraphs are understood, and the expansion of variables is
1245 different. Specifically, redirection using
1246 <literal>&lt;</literal>,
1247 <literal>&lt;&lt;</literal>,
1248 <literal>&gt;</literal>, and
1249 <literal>&gt;&gt;</literal>, pipes using
1250 <literal>|</literal>, running programs in the background using
1251 <literal>&amp;</literal>, and <emphasis>other elements of shell
1252 syntax are not supported</emphasis>.</para>
1253
1254 <para>The command to execute may contain spaces, but control characters are not allowed.</para>
1255
1256 <para>Each command may be prefixed with a number of special characters:</para>
1257
1258 <table>
1259 <title>Special executable prefixes</title>
1260
1261 <tgroup cols='2'>
1262 <colspec colname='prefix'/>
1263 <colspec colname='meaning'/>
1264
1265 <thead>
1266 <row>
1267 <entry>Prefix</entry>
1268 <entry>Effect</entry>
1269 </row>
1270 </thead>
1271 <tbody>
1272 <row>
1273 <entry><literal>@</literal></entry>
1274 <entry>If the executable path is prefixed with <literal>@</literal>, the second specified token will be passed as <constant>argv[0]</constant> to the executed process (instead of the actual filename), followed by the further arguments specified.</entry>
1275 </row>
1276
1277 <row>
1278 <entry><literal>-</literal></entry>
1279 <entry>If the executable path is prefixed with <literal>-</literal>, an exit code of the command normally considered a failure (i.e. non-zero exit status or abnormal exit due to signal) is recorded, but has no further effect and is considered equivalent to success.</entry>
1280 </row>
1281
1282 <row>
1283 <entry><literal>:</literal></entry>
1284 <entry>If the executable path is prefixed with <literal>:</literal>, environment variable substitution (as described by the "Command Lines" section below) is not applied.</entry>
1285 </row>
1286
1287 <row>
1288 <entry><literal>+</literal></entry>
1289 <entry>If the executable path is prefixed with <literal>+</literal> then the process is executed with full privileges. In this mode privilege restrictions configured with <varname>User=</varname>, <varname>Group=</varname>, <varname>CapabilityBoundingSet=</varname> or the various file system namespacing options (such as <varname>PrivateDevices=</varname>, <varname>PrivateTmp=</varname>) are not applied to the invoked command line (but still affect any other <varname>ExecStart=</varname>, <varname>ExecStop=</varname>, … lines). However, note that this will not bypass options that apply to the whole control group, such as <varname>DevicePolicy=</varname>, see <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry> for the full list.</entry>
1290 </row>
1291
1292 <row>
1293 <entry><literal>!</literal></entry>
1294
1295 <entry>Similar to the <literal>+</literal> character discussed above this permits invoking command lines with elevated privileges. However, unlike <literal>+</literal> the <literal>!</literal> character exclusively alters the effect of <varname>User=</varname>, <varname>Group=</varname> and <varname>SupplementaryGroups=</varname>, i.e. only the stanzas that affect user and group credentials. Note that this setting may be combined with <varname>DynamicUser=</varname>, in which case a dynamic user/group pair is allocated before the command is invoked, but credential changing is left to the executed process itself.</entry>
1296 </row>
1297
1298 <row>
1299 <entry><literal>!!</literal></entry>
1300
1301 <entry>This prefix is very similar to <literal>!</literal>, however it only has an effect on systems lacking support for ambient process capabilities, i.e. without support for <varname>AmbientCapabilities=</varname>. It's intended to be used for unit files that take benefit of ambient capabilities to run processes with minimal privileges wherever possible while remaining compatible with systems that lack ambient capabilities support. Note that when <literal>!!</literal> is used, and a system lacking ambient capability support is detected any configured <varname>SystemCallFilter=</varname> and <varname>CapabilityBoundingSet=</varname> stanzas are implicitly modified, in order to permit spawned processes to drop credentials and capabilities themselves, even if this is configured to not be allowed. Moreover, if this prefix is used and a system lacking ambient capability support is detected <varname>AmbientCapabilities=</varname> will be skipped and not be applied. On systems supporting ambient capabilities, <literal>!!</literal> has no effect and is redundant.</entry>
1302 </row>
1303 </tbody>
1304 </tgroup>
1305 </table>
1306
1307 <para><literal>@</literal>, <literal>-</literal>, <literal>:</literal>, and one of
1308 <literal>+</literal>/<literal>!</literal>/<literal>!!</literal> may be used together and they can appear in any
1309 order. However, only one of <literal>+</literal>, <literal>!</literal>, <literal>!!</literal> may be used at a
1310 time.</para>
1311
1312 <para>For each command, the first argument must be either an absolute path to an executable or a simple
1313 file name without any slashes. If the command is not a full (absolute) path, it will be resolved to a
1314 full path using a fixed search path determined at compilation time. Searched directories include
1315 <filename>/usr/local/bin/</filename>, <filename>/usr/bin/</filename>, <filename>/bin/</filename> on
1316 systems using split <filename>/usr/bin/</filename> and <filename>/bin/</filename> directories, and their
1317 <filename>sbin/</filename> counterparts on systems using split <filename>bin/</filename> and
1318 <filename>sbin/</filename>. It is thus safe to use just the executable name in case of executables
1319 located in any of the "standard" directories, and an absolute path must be used in other cases. Using an
1320 absolute path is recommended to avoid ambiguity. Hint: this search path may be queried using
1321 <command>systemd-path search-binaries-default</command>.</para>
1322
1323 <para>The command line accepts <literal>%</literal> specifiers as described in
1324 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
1325
1326 <para>Basic environment variable substitution is supported. Use
1327 <literal>${FOO}</literal> as part of a word, or as a word of its
1328 own, on the command line, in which case it will be erased and replaced
1329 by the exact value of the environment variable (if any) including all
1330 whitespace it contains, always resulting in exactly a single argument.
1331 Use <literal>$FOO</literal> as a separate word on the command line, in
1332 which case it will be replaced by the value of the environment
1333 variable split at whitespace, resulting in zero or more arguments.
1334 For this type of expansion, quotes are respected when splitting
1335 into words, and afterwards removed.</para>
1336
1337 <para>Example:</para>
1338
1339 <programlisting>Environment="ONE=one" 'TWO=two two'
1340 ExecStart=echo $ONE $TWO ${TWO}</programlisting>
1341
1342 <para>This will execute <command>/bin/echo</command> with four
1343 arguments: <literal>one</literal>, <literal>two</literal>,
1344 <literal>two</literal>, and <literal>two two</literal>.</para>
1345
1346 <para>Example:</para>
1347 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
1348 ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1349 ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
1350 <para>This results in <filename>/bin/echo</filename> being
1351 called twice, the first time with arguments
1352 <literal>'one'</literal>,
1353 <literal>'two two' too</literal>, <literal></literal>,
1354 and the second time with arguments
1355 <literal>one</literal>, <literal>two two</literal>,
1356 <literal>too</literal>.
1357 </para>
1358
1359 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1360 Variables whose value is not known at expansion time are treated
1361 as empty strings. Note that the first argument (i.e. the program
1362 to execute) may not be a variable.</para>
1363
1364 <para>Variables to be used in this fashion may be defined through
1365 <varname>Environment=</varname> and
1366 <varname>EnvironmentFile=</varname>. In addition, variables listed
1367 in the section "Environment variables in spawned processes" in
1368 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1369 which are considered "static configuration", may be used (this
1370 includes e.g. <varname>$USER</varname>, but not
1371 <varname>$TERM</varname>).</para>
1372
1373 <para>Note that shell command lines are not directly supported. If
1374 shell command lines are to be used, they need to be passed
1375 explicitly to a shell implementation of some kind. Example:</para>
1376 <programlisting>ExecStart=sh -c 'dmesg | tac'</programlisting>
1377
1378 <para>Example:</para>
1379
1380 <programlisting>ExecStart=echo one ; echo "two two"</programlisting>
1381
1382 <para>This will execute <command>echo</command> two times,
1383 each time with one argument: <literal>one</literal> and
1384 <literal>two two</literal>, respectively. Because two commands are
1385 specified, <varname>Type=oneshot</varname> must be used.</para>
1386
1387 <para>Example:</para>
1388
1389 <programlisting>Type=oneshot
1390 ExecStart=:echo $USER ; -false ; +:@true $TEST</programlisting>
1391
1392 <para>This will execute <command>/usr/bin/echo</command> with the literal argument
1393 <literal>$USER</literal> (<literal>:</literal> suppresses variable expansion), and then
1394 <command>/usr/bin/false</command> (the return value will be ignored because <literal>-</literal>
1395 suppresses checking of the return value), and <command>/usr/bin/true</command> (with elevated privileges,
1396 with <literal>$TEST</literal> as <constant>argv[0]</constant>).</para>
1397
1398 <para>Example:</para>
1399
1400 <programlisting>ExecStart=echo / &gt;/dev/null &amp; \; \
1401 ls</programlisting>
1402
1403 <para>This will execute <command>echo</command>
1404 with five arguments: <literal>/</literal>,
1405 <literal>&gt;/dev/null</literal>,
1406 <literal>&amp;</literal>, <literal>;</literal>, and
1407 <literal>ls</literal>.</para>
1408 </refsect1>
1409
1410 <refsect1>
1411 <title>Examples</title>
1412
1413 <example>
1414 <title>Simple service</title>
1415
1416 <para>The following unit file creates a service that will
1417 execute <filename index="false">/usr/sbin/foo-daemon</filename>. Since no
1418 <varname>Type=</varname> is specified, the default
1419 <varname>Type=</varname><option>simple</option> will be assumed.
1420 systemd will assume the unit to be started immediately after the
1421 program has begun executing.</para>
1422
1423 <programlisting>[Unit]
1424 Description=Foo
1425
1426 [Service]
1427 ExecStart=/usr/sbin/foo-daemon
1428
1429 [Install]
1430 WantedBy=multi-user.target</programlisting>
1431
1432 <para>Note that systemd assumes here that the process started by
1433 systemd will continue running until the service terminates. If
1434 the program daemonizes itself (i.e. forks), please use
1435 <varname>Type=</varname><option>forking</option> instead.</para>
1436
1437 <para>Since no <varname>ExecStop=</varname> was specified,
1438 systemd will send SIGTERM to all processes started from this
1439 service, and after a timeout also SIGKILL. This behavior can be
1440 modified, see
1441 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1442 for details.</para>
1443
1444 <para>Note that this unit type does not include any type of notification when a service has completed
1445 initialization. For this, you should use other unit types, such as
1446 <varname>Type=</varname><option>notify</option>/<varname>Type=</varname><option>notify-reload</option>
1447 if the service understands systemd's notification protocol,
1448 <varname>Type=</varname><option>forking</option> if the service can background itself or
1449 <varname>Type=</varname><option>dbus</option> if the unit acquires a DBus name once initialization is
1450 complete. See below.</para>
1451 </example>
1452
1453 <example>
1454 <title>Oneshot service</title>
1455
1456 <para>Sometimes, units should just execute an action without
1457 keeping active processes, such as a filesystem check or a
1458 cleanup action on boot. For this,
1459 <varname>Type=</varname><option>oneshot</option> exists. Units
1460 of this type will wait until the process specified terminates
1461 and then fall back to being inactive. The following unit will
1462 perform a cleanup action:</para>
1463
1464 <programlisting>[Unit]
1465 Description=Cleanup old Foo data
1466
1467 [Service]
1468 Type=oneshot
1469 ExecStart=/usr/sbin/foo-cleanup
1470
1471 [Install]
1472 WantedBy=multi-user.target</programlisting>
1473
1474 <para>Note that systemd will consider the unit to be in the
1475 state "starting" until the program has terminated, so ordered
1476 dependencies will wait for the program to finish before starting
1477 themselves. The unit will revert to the "inactive" state after
1478 the execution is done, never reaching the "active" state. That
1479 means another request to start the unit will perform the action
1480 again.</para>
1481
1482 <para><varname>Type=</varname><option>oneshot</option> are the
1483 only service units that may have more than one
1484 <varname>ExecStart=</varname> specified. For units with multiple
1485 commands (<varname index="false">Type=oneshot</varname>), all commands will be run again.</para>
1486 <para> For <varname index="false">Type=oneshot</varname>, <varname>Restart=</varname><option>always</option>
1487 and <varname>Restart=</varname><option>on-success</option> are <emphasis>not</emphasis> allowed.</para>
1488 </example>
1489
1490 <example>
1491 <title>Stoppable oneshot service</title>
1492
1493 <para>Similarly to the oneshot services, there are sometimes
1494 units that need to execute a program to set up something and
1495 then execute another to shut it down, but no process remains
1496 active while they are considered "started". Network
1497 configuration can sometimes fall into this category. Another use
1498 case is if a oneshot service shall not be executed each time
1499 when they are pulled in as a dependency, but only the first
1500 time.</para>
1501
1502 <para>For this, systemd knows the setting
1503 <varname>RemainAfterExit=</varname><option>yes</option>, which
1504 causes systemd to consider the unit to be active if the start
1505 action exited successfully. This directive can be used with all
1506 types, but is most useful with
1507 <varname>Type=</varname><option>oneshot</option> and
1508 <varname>Type=</varname><option>simple</option>. With
1509 <varname>Type=</varname><option>oneshot</option>, systemd waits
1510 until the start action has completed before it considers the
1511 unit to be active, so dependencies start only after the start
1512 action has succeeded. With
1513 <varname>Type=</varname><option>simple</option>, dependencies
1514 will start immediately after the start action has been
1515 dispatched. The following unit provides an example for a simple
1516 static firewall.</para>
1517
1518 <programlisting>[Unit]
1519 Description=Simple firewall
1520
1521 [Service]
1522 Type=oneshot
1523 RemainAfterExit=yes
1524 ExecStart=/usr/local/sbin/simple-firewall-start
1525 ExecStop=/usr/local/sbin/simple-firewall-stop
1526
1527 [Install]
1528 WantedBy=multi-user.target</programlisting>
1529
1530 <para>Since the unit is considered to be running after the start
1531 action has exited, invoking <command>systemctl start</command>
1532 on that unit again will cause no action to be taken.</para>
1533 </example>
1534
1535 <example>
1536 <title>Traditional forking services</title>
1537
1538 <para>Many traditional daemons/services background (i.e. fork,
1539 daemonize) themselves when starting. Set
1540 <varname>Type=</varname><option>forking</option> in the
1541 service's unit file to support this mode of operation. systemd
1542 will consider the service to be in the process of initialization
1543 while the original program is still running. Once it exits
1544 successfully and at least a process remains (and
1545 <varname>RemainAfterExit=</varname><option>no</option>), the
1546 service is considered started.</para>
1547
1548 <para>Often, a traditional daemon only consists of one process.
1549 Therefore, if only one process is left after the original
1550 process terminates, systemd will consider that process the main
1551 process of the service. In that case, the
1552 <varname>$MAINPID</varname> variable will be available in
1553 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1554 etc.</para>
1555
1556 <para>In case more than one process remains, systemd will be
1557 unable to determine the main process, so it will not assume
1558 there is one. In that case, <varname>$MAINPID</varname> will not
1559 expand to anything. However, if the process decides to write a
1560 traditional PID file, systemd will be able to read the main PID
1561 from there. Please set <varname>PIDFile=</varname> accordingly.
1562 Note that the daemon should write that file before finishing
1563 with its initialization. Otherwise, systemd might try to read the
1564 file before it exists.</para>
1565
1566 <para>The following example shows a simple daemon that forks and
1567 just starts one process in the background:</para>
1568
1569 <programlisting>[Unit]
1570 Description=Some simple daemon
1571
1572 [Service]
1573 Type=forking
1574 ExecStart=/usr/sbin/my-simple-daemon -d
1575
1576 [Install]
1577 WantedBy=multi-user.target</programlisting>
1578
1579 <para>Please see
1580 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1581 for details on how you can influence the way systemd terminates
1582 the service.</para>
1583 </example>
1584
1585 <example>
1586 <title>DBus services</title>
1587
1588 <para>For services that acquire a name on the DBus system bus,
1589 use <varname>Type=</varname><option>dbus</option> and set
1590 <varname>BusName=</varname> accordingly. The service should not
1591 fork (daemonize). systemd will consider the service to be
1592 initialized once the name has been acquired on the system bus.
1593 The following example shows a typical DBus service:</para>
1594
1595 <programlisting>[Unit]
1596 Description=Simple DBus service
1597
1598 [Service]
1599 Type=dbus
1600 BusName=org.example.simple-dbus-service
1601 ExecStart=/usr/sbin/simple-dbus-service
1602
1603 [Install]
1604 WantedBy=multi-user.target</programlisting>
1605
1606 <para>For <emphasis>bus-activatable</emphasis> services, do not
1607 include a [Install] section in the systemd
1608 service file, but use the <varname>SystemdService=</varname>
1609 option in the corresponding DBus service file, for example
1610 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
1611
1612 <programlisting>[D-BUS Service]
1613 Name=org.example.simple-dbus-service
1614 Exec=/usr/sbin/simple-dbus-service
1615 User=root
1616 SystemdService=simple-dbus-service.service</programlisting>
1617
1618 <para>Please see
1619 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1620 for details on how you can influence the way systemd terminates
1621 the service.</para>
1622 </example>
1623
1624 <example>
1625 <title>Services that notify systemd about their initialization</title>
1626
1627 <para><varname>Type=</varname><option>simple</option> services are really easy to write, but have the
1628 major disadvantage of systemd not being able to tell when initialization of the given service is
1629 complete. For this reason, systemd supports a simple notification protocol that allows daemons to make
1630 systemd aware that they are done initializing. Use <varname>Type=</varname><option>notify</option> or
1631 <varname>Type=</varname><option>notify-reload</option> for this. A typical service file for such a
1632 daemon would look like this:</para>
1633
1634 <programlisting>[Unit]
1635 Description=Simple notifying service
1636
1637 [Service]
1638 Type=notify
1639 ExecStart=/usr/sbin/simple-notifying-service
1640
1641 [Install]
1642 WantedBy=multi-user.target</programlisting>
1643
1644 <para>Note that the daemon has to support systemd's notification
1645 protocol, else systemd will think the service has not started yet
1646 and kill it after a timeout. For an example of how to update
1647 daemons to support this protocol transparently, take a look at
1648 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1649 systemd will consider the unit to be in the 'starting' state
1650 until a readiness notification has arrived.</para>
1651
1652 <para>Please see
1653 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1654 for details on how you can influence the way systemd terminates
1655 the service.</para>
1656 </example>
1657 </refsect1>
1658
1659 <refsect1>
1660 <title>See Also</title>
1661 <para>
1662 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1663 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1664 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1665 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1666 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1667 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1668 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1669 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
1670 <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>
1671 </para>
1672 </refsect1>
1673
1674 </refentry>