]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd.service.xml
Merge pull request #27669 from keszybz/man-fixes-254
[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>RestartSecMax=</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>RestartSecMax=</varname> is also set.</para></listitem>
557 </varlistentry>
558
559 <varlistentry>
560 <term><varname>RestartSecMax=</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. A restarted service enters the failed state only
890 after the start limits are reached.</para>
891
892 <para>Setting this to <option>on-failure</option> is the
893 recommended choice for long-running services, in order to
894 increase reliability by attempting automatic recovery from
895 errors. For services that shall be able to terminate on their
896 own choice (and avoid immediate restarting),
897 <option>on-abnormal</option> is an alternative choice.</para>
898 </listitem>
899 </varlistentry>
900
901 <varlistentry>
902 <term><varname>SuccessExitStatus=</varname></term>
903
904 <listitem><para>Takes a list of exit status definitions that, when returned by the main service
905 process, will be considered successful termination, in addition to the normal successful exit status
906 0 and, except for <varname>Type=oneshot</varname>, the signals <constant>SIGHUP</constant>, <constant>SIGINT</constant>,
907 <constant>SIGTERM</constant>, and <constant>SIGPIPE</constant>. Exit status definitions can be
908 numeric termination statuses, termination status names, or termination signal names, separated by
909 spaces. See the Process Exit Codes section in
910 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
911 a list of termination status names (for this setting only the part without the
912 <literal>EXIT_</literal> or <literal>EX_</literal> prefix should be used). See <citerefentry
913 project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
914 a list of signal names.</para>
915
916 <para>Note that this setting does not change the mapping between numeric exit statuses and their
917 names, i.e. regardless how this setting is used 0 will still be mapped to <literal>SUCCESS</literal>
918 (and thus typically shown as <literal>0/SUCCESS</literal> in tool outputs) and 1 to
919 <literal>FAILURE</literal> (and thus typically shown as <literal>1/FAILURE</literal>), and so on. It
920 only controls what happens as effect of these exit statuses, and how it propagates to the state of
921 the service as a whole.</para>
922
923 <para>This option may appear more than once, in which case the list of successful exit statuses is
924 merged. If the empty string is assigned to this option, the list is reset, all prior assignments of
925 this option will have no effect.</para>
926
927 <example>
928 <title>A service with the <varname>SuccessExitStatus=</varname> setting</title>
929
930 <programlisting>SuccessExitStatus=TEMPFAIL 250 SIGKILL</programlisting>
931
932 <para>Exit status 75 (<constant>TEMPFAIL</constant>), 250, and the termination signal
933 <constant>SIGKILL</constant> are considered clean service terminations.</para>
934 </example>
935
936 <para>Note: <command>systemd-analyze exit-status</command> may be used to list exit statuses and
937 translate between numerical status values and names.</para></listitem>
938 </varlistentry>
939
940 <varlistentry>
941 <term><varname>RestartPreventExitStatus=</varname></term>
942
943 <listitem><para>Takes a list of exit status definitions that, when returned by the main service
944 process, will prevent automatic service restarts, regardless of the restart setting configured with
945 <varname>Restart=</varname>. Exit status definitions can either be numeric exit codes or termination
946 signal names, and are separated by spaces. Defaults to the empty list, so that, by default, no exit
947 status is excluded from the configured restart logic. For example:
948
949 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting>
950
951 ensures that exit codes 1 and 6 and the termination signal <constant>SIGABRT</constant> will not
952 result in automatic service restarting. This option may appear more than once, in which case the list
953 of restart-preventing statuses is merged. If the empty string is assigned to this option, the list is
954 reset and all prior assignments of this option will have no effect.</para>
955
956 <para>Note that this setting has no effect on processes configured via
957 <varname>ExecStartPre=</varname>, <varname>ExecStartPost=</varname>, <varname>ExecStop=</varname>,
958 <varname>ExecStopPost=</varname> or <varname>ExecReload=</varname>, but only on the main service
959 process, i.e. either the one invoked by <varname>ExecStart=</varname> or (depending on
960 <varname>Type=</varname>, <varname>PIDFile=</varname>, …) the otherwise configured main
961 process.</para></listitem>
962 </varlistentry>
963
964 <varlistentry>
965 <term><varname>RestartForceExitStatus=</varname></term>
966 <listitem><para>Takes a list of exit status definitions that,
967 when returned by the main service process, will force automatic
968 service restarts, regardless of the restart setting configured
969 with <varname>Restart=</varname>. The argument format is
970 similar to
971 <varname>RestartPreventExitStatus=</varname>.</para></listitem>
972 </varlistentry>
973
974 <varlistentry>
975 <term><varname>RootDirectoryStartOnly=</varname></term>
976 <listitem><para>Takes a boolean argument. If true, the root
977 directory, as configured with the
978 <varname>RootDirectory=</varname> option (see
979 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
980 for more information), is only applied to the process started
981 with <varname>ExecStart=</varname>, and not to the various
982 other <varname>ExecStartPre=</varname>,
983 <varname>ExecStartPost=</varname>,
984 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
985 and <varname>ExecStopPost=</varname> commands. If false, the
986 setting is applied to all configured commands the same way.
987 Defaults to false.</para></listitem>
988 </varlistentry>
989
990 <varlistentry>
991 <term><varname>NonBlocking=</varname></term>
992 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag for all file descriptors passed via socket-based
993 activation. If true, all file descriptors >= 3 (i.e. all except stdin, stdout, stderr), excluding those passed
994 in via the file descriptor storage logic (see <varname>FileDescriptorStoreMax=</varname> for details), will
995 have the <constant>O_NONBLOCK</constant> flag set and hence are in non-blocking mode. This option is only
996 useful in conjunction with a socket unit, as described in
997 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> and has no
998 effect on file descriptors which were previously saved in the file-descriptor store for example. Defaults to
999 false.</para></listitem>
1000 </varlistentry>
1001
1002 <varlistentry>
1003 <term><varname>NotifyAccess=</varname></term>
1004 <listitem><para>Controls access to the service status notification socket, as accessible via the
1005 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
1006 call. Takes one of <option>none</option> (the default), <option>main</option>, <option>exec</option>
1007 or <option>all</option>. If <option>none</option>, no daemon status updates are accepted from the
1008 service processes, all status update messages are ignored. If <option>main</option>, only service
1009 updates sent from the main process of the service are accepted. If <option>exec</option>, only
1010 service updates sent from any of the main or control processes originating from one of the
1011 <varname>Exec*=</varname> commands are accepted. If <option>all</option>, all services updates from
1012 all members of the service's control group are accepted. This option should be set to open access to
1013 the notification socket when using
1014 <varname>Type=notify</varname>/<varname>Type=notify-reload</varname> or
1015 <varname>WatchdogSec=</varname> (see above). If those options are used but
1016 <varname>NotifyAccess=</varname> is not configured, it will be implicitly set to
1017 <option>main</option>.</para>
1018
1019 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if
1020 either the sending process is still around at the time PID 1 processes the message, or if the sending process
1021 is explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally
1022 forked off the process, i.e. on all processes that match <option>main</option> or
1023 <option>exec</option>. Conversely, if an auxiliary process of the unit sends an
1024 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to
1025 properly attribute the message to the unit, and thus will ignore it, even if
1026 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para>
1027
1028 <para>Hence, to eliminate all race conditions involving lookup of the client's unit and attribution of notifications
1029 to units correctly, <function>sd_notify_barrier()</function> may be used. This call acts as a synchronization point
1030 and ensures all notifications sent before this call have been picked up by the service manager when it returns
1031 successfully. Use of <function>sd_notify_barrier()</function> is needed for clients which are not invoked by the
1032 service manager, otherwise this synchronization mechanism is unnecessary for attribution of notifications to the
1033 unit.</para></listitem>
1034 </varlistentry>
1035
1036 <varlistentry>
1037 <term><varname>Sockets=</varname></term>
1038 <listitem><para>Specifies the name of the socket units this
1039 service shall inherit socket file descriptors from when the
1040 service is started. Normally, it should not be necessary to use
1041 this setting, as all socket file descriptors whose unit shares
1042 the same name as the service (subject to the different unit
1043 name suffix of course) are passed to the spawned
1044 process.</para>
1045
1046 <para>Note that the same socket file descriptors may be passed
1047 to multiple processes simultaneously. Also note that a
1048 different service may be activated on incoming socket traffic
1049 than the one which is ultimately configured to inherit the
1050 socket file descriptors. Or, in other words: the
1051 <varname>Service=</varname> setting of
1052 <filename>.socket</filename> units does not have to match the
1053 inverse of the <varname>Sockets=</varname> setting of the
1054 <filename>.service</filename> it refers to.</para>
1055
1056 <para>This option may appear more than once, in which case the list of socket units is merged. Note
1057 that once set, clearing the list of sockets again (for example, by assigning the empty string to this
1058 option) is not supported.</para></listitem>
1059 </varlistentry>
1060
1061 <varlistentry>
1062 <term><varname>FileDescriptorStoreMax=</varname></term>
1063 <listitem><para>Configure how many file descriptors may be stored in the service manager for the
1064 service using
1065 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
1066 <literal>FDSTORE=1</literal> messages. This is useful for implementing services that can restart
1067 after an explicit request or a crash without losing state. Any open sockets and other file
1068 descriptors which should not be closed during the restart may be stored this way. Application state
1069 can either be serialized to a file in <varname>RuntimeDirectory=</varname>, or stored in a
1070 <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry>
1071 memory file descriptor. Defaults to 0, i.e. no file descriptors may be stored in the service
1072 manager. All file descriptors passed to the service manager from a specific service are passed back
1073 to the service's main process on the next service restart (see
1074 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
1075 details about the precise protocol used and the order in which the file descriptors are passed). Any
1076 file descriptors passed to the service manager are automatically closed when
1077 <constant>POLLHUP</constant> or <constant>POLLERR</constant> is seen on them, or when the service is
1078 fully stopped and no job is queued or being executed for it (the latter can be tweaked with
1079 <varname>FileDescriptorStorePreserve=</varname>, see below). If this option is used,
1080 <varname>NotifyAccess=</varname> (see above) should be set to open access to the notification socket
1081 provided by systemd. If <varname>NotifyAccess=</varname> is not set, it will be implicitly set to
1082 <option>main</option>.</para>
1083
1084 <para>The <command>fdstore</command> command of
1085 <citerefentry><refentrytitle>systemd-analyze</refentrytitle><manvolnum>1</manvolnum></citerefentry>
1086 may be used to list the current contents of a service's file descriptor store.</para>
1087
1088 <para>Note that the service manager will only pass file descriptors contained in the file descriptor
1089 store to the service's own processes, never to other clients via IPC or similar. However, it does
1090 allow unprivileged clients to query the list of currently open file descriptors of a
1091 service. Sensitive data may hence be safely placed inside the referenced files, but should not be
1092 attached to the metadata (e.g. included in filenames) of the stored file
1093 descriptors.</para>
1094
1095 <para>If this option is set to a non-zero value the <varname>$FDSTORE</varname> environment variable
1096 will be set for processes invoked for this service. See
1097 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
1098 details.</para></listitem>
1099 </varlistentry>
1100
1101 <varlistentry>
1102 <term><varname>FileDescriptorStorePreserve=</varname></term>
1103 <listitem><para>Takes one of <constant>no</constant>, <constant>yes</constant>,
1104 <constant>restart</constant> and controls when to release the service's file descriptor store
1105 (i.e. when to close the contained file descriptors, if any). If set to <constant>no</constant> the
1106 file descriptor store is automatically released when the service is stopped; if
1107 <constant>restart</constant> (the default) it is kept around as long as the unit is neither inactive
1108 nor failed, or a job is queued for the service, or the service is expected to be restarted. If
1109 <constant>yes</constant> the file descriptor store is kept around until the unit is removed from
1110 memory (i.e. is not referenced anymore and inactive). The latter is useful to keep entries in the
1111 file descriptor store pinned until the service manage exits.</para>
1112
1113 <para>Use <command>systemctl clean --what=fdstore …</command> to release the file descriptor store
1114 explicitly.</para></listitem>
1115 </varlistentry>
1116
1117 <varlistentry>
1118 <term><varname>USBFunctionDescriptors=</varname></term>
1119 <listitem><para>Configure the location of a file containing
1120 <ulink
1121 url="https://docs.kernel.org/usb/functionfs.html">USB
1122 FunctionFS</ulink> descriptors, for implementation of USB
1123 gadget functions. This is used only in conjunction with a
1124 socket unit with <varname>ListenUSBFunction=</varname>
1125 configured. The contents of this file are written to the
1126 <filename>ep0</filename> file after it is
1127 opened.</para></listitem>
1128 </varlistentry>
1129
1130 <varlistentry>
1131 <term><varname>USBFunctionStrings=</varname></term>
1132 <listitem><para>Configure the location of a file containing
1133 USB FunctionFS strings. Behavior is similar to
1134 <varname>USBFunctionDescriptors=</varname>
1135 above.</para></listitem>
1136 </varlistentry>
1137
1138 <varlistentry id='oom-policy'>
1139 <term><varname>OOMPolicy=</varname></term>
1140
1141 <listitem><para>Configure the out-of-memory (OOM) killing policy for the kernel and the userspace OOM
1142 killer
1143 <citerefentry><refentrytitle>systemd-oomd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
1144 On Linux, when memory becomes scarce to the point that the kernel has trouble allocating memory for
1145 itself, it might decide to kill a running process in order to free up memory and reduce memory
1146 pressure. Note that <filename>systemd-oomd.service</filename> is a more flexible solution that aims
1147 to prevent out-of-memory situations for the userspace too, not just the kernel, by attempting to
1148 terminate services earlier, before the kernel would have to act.</para>
1149
1150 <para>This setting takes one of <constant>continue</constant>, <constant>stop</constant> or
1151 <constant>kill</constant>. If set to <constant>continue</constant> and a process in the unit is
1152 killed by the OOM killer, this is logged but the unit continues running. If set to
1153 <constant>stop</constant> the event is logged but the unit is terminated cleanly by the service
1154 manager. If set to <constant>kill</constant> and one of the unit's processes is killed by the OOM
1155 killer the kernel is instructed to kill all remaining processes of the unit too, by setting the
1156 <filename>memory.oom.group</filename> attribute to <constant>1</constant>; also see <ulink
1157 url="https://docs.kernel.org/admin-guide/cgroup-v2.html">kernel documentation</ulink>.</para>
1158
1159 <para>Defaults to the setting <varname>DefaultOOMPolicy=</varname> in
1160 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1161 is set to, except for units where <varname>Delegate=</varname> is turned on, where it defaults to
1162 <constant>continue</constant>.</para>
1163
1164 <para>Use the <varname>OOMScoreAdjust=</varname> setting to configure whether processes of the unit
1165 shall be considered preferred or less preferred candidates for process termination by the Linux OOM
1166 killer logic. See
1167 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
1168 details.</para>
1169
1170 <para>This setting also applies to
1171 <citerefentry><refentrytitle>systemd-oomd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
1172 Similarly to the kernel OOM kills performed by the kernel, this setting determines the state of the
1173 unit after <command>systemd-oomd</command> kills a cgroup associated with it.</para></listitem>
1174 </varlistentry>
1175
1176 <varlistentry>
1177 <term><varname>OpenFile=</varname></term>
1178 <listitem><para>Takes an argument of the form <literal>path<optional><replaceable>:fd-name:options</replaceable></optional></literal>,
1179 where:
1180 <itemizedlist>
1181 <listitem><simpara><literal>path</literal> is a path to a file or an <constant>AF_UNIX</constant> socket in the file system;</simpara></listitem>
1182 <listitem><simpara><literal>fd-name</literal> is a name that will be associated with the file descriptor;
1183 the name may contain any ASCII character, but must exclude control characters and ":", and must be at most 255 characters in length;
1184 it is optional and, if not provided, defaults to the file name;</simpara></listitem>
1185 <listitem><simpara><literal>options</literal> is a comma-separated list of access options;
1186 possible values are
1187 <literal>read-only</literal>,
1188 <literal>append</literal>,
1189 <literal>truncate</literal>,
1190 <literal>graceful</literal>;
1191 if not specified, files will be opened in <constant>rw</constant> mode;
1192 if <literal>graceful</literal> is specified, errors during file/socket opening are ignored.
1193 Specifying the same option several times is treated as an error.</simpara></listitem>
1194 </itemizedlist>
1195 The file or socket is opened by the service manager and the file descriptor is passed to the service.
1196 If the path is a socket, we call <function>connect()</function> on it.
1197 See <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
1198 for more details on how to retrieve these file descriptors.</para>
1199
1200 <para>This setting is useful to allow services to access files/sockets that they can't access themselves
1201 (due to running in a separate mount namespace, not having privileges, ...).</para>
1202
1203 <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.
1204 If the empty string is assigned, the entire list of open files defined prior to this is reset.</para></listitem>
1205 </varlistentry>
1206
1207 <varlistentry>
1208 <term><varname>ReloadSignal=</varname></term>
1209 <listitem><para>Configures the UNIX process signal to send to the service's main process when asked
1210 to reload the service's configuration. Defaults to <constant>SIGHUP</constant>. This option has no
1211 effect unless <varname>Type=</varname><option>notify-reload</option> is used, see
1212 above.</para></listitem>
1213 </varlistentry>
1214
1215 </variablelist>
1216
1217 <para id='shared-unit-options'>Check
1218 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1219 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, and
1220 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more
1221 settings.</para>
1222 </refsect1>
1223
1224 <refsect1>
1225 <title>Command lines</title>
1226
1227 <para>This section describes command line parsing and
1228 variable and specifier substitutions for
1229 <varname>ExecStart=</varname>,
1230 <varname>ExecStartPre=</varname>,
1231 <varname>ExecStartPost=</varname>,
1232 <varname>ExecReload=</varname>,
1233 <varname>ExecStop=</varname>, and
1234 <varname>ExecStopPost=</varname> options.</para>
1235
1236 <para>Multiple command lines may be concatenated in a single directive by separating them with semicolons
1237 (these semicolons must be passed as separate words). Lone semicolons may be escaped as
1238 <literal>\;</literal>.</para>
1239
1240 <para>Each command line is unquoted using the rules described in "Quoting" section in
1241 <citerefentry><refentrytitle>systemd.syntax</refentrytitle><manvolnum>7</manvolnum></citerefentry>. The
1242 first item becomes the command to execute, and the subsequent items the arguments.</para>
1243
1244 <para>This syntax is inspired by shell syntax, but only the meta-characters and expansions
1245 described in the following paragraphs are understood, and the expansion of variables is
1246 different. Specifically, redirection using
1247 <literal>&lt;</literal>,
1248 <literal>&lt;&lt;</literal>,
1249 <literal>&gt;</literal>, and
1250 <literal>&gt;&gt;</literal>, pipes using
1251 <literal>|</literal>, running programs in the background using
1252 <literal>&amp;</literal>, and <emphasis>other elements of shell
1253 syntax are not supported</emphasis>.</para>
1254
1255 <para>The command to execute may contain spaces, but control characters are not allowed.</para>
1256
1257 <para>Each command may be prefixed with a number of special characters:</para>
1258
1259 <table>
1260 <title>Special executable prefixes</title>
1261
1262 <tgroup cols='2'>
1263 <colspec colname='prefix'/>
1264 <colspec colname='meaning'/>
1265
1266 <thead>
1267 <row>
1268 <entry>Prefix</entry>
1269 <entry>Effect</entry>
1270 </row>
1271 </thead>
1272 <tbody>
1273 <row>
1274 <entry><literal>@</literal></entry>
1275 <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>
1276 </row>
1277
1278 <row>
1279 <entry><literal>-</literal></entry>
1280 <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>
1281 </row>
1282
1283 <row>
1284 <entry><literal>:</literal></entry>
1285 <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>
1286 </row>
1287
1288 <row>
1289 <entry><literal>+</literal></entry>
1290 <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>
1291 </row>
1292
1293 <row>
1294 <entry><literal>!</literal></entry>
1295
1296 <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>
1297 </row>
1298
1299 <row>
1300 <entry><literal>!!</literal></entry>
1301
1302 <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>
1303 </row>
1304 </tbody>
1305 </tgroup>
1306 </table>
1307
1308 <para><literal>@</literal>, <literal>-</literal>, <literal>:</literal>, and one of
1309 <literal>+</literal>/<literal>!</literal>/<literal>!!</literal> may be used together and they can appear in any
1310 order. However, only one of <literal>+</literal>, <literal>!</literal>, <literal>!!</literal> may be used at a
1311 time.</para>
1312
1313 <para>For each command, the first argument must be either an absolute path to an executable or a simple
1314 file name without any slashes. If the command is not a full (absolute) path, it will be resolved to a
1315 full path using a fixed search path determined at compilation time. Searched directories include
1316 <filename>/usr/local/bin/</filename>, <filename>/usr/bin/</filename>, <filename>/bin/</filename> on
1317 systems using split <filename>/usr/bin/</filename> and <filename>/bin/</filename> directories, and their
1318 <filename>sbin/</filename> counterparts on systems using split <filename>bin/</filename> and
1319 <filename>sbin/</filename>. It is thus safe to use just the executable name in case of executables
1320 located in any of the "standard" directories, and an absolute path must be used in other cases. Using an
1321 absolute path is recommended to avoid ambiguity. Hint: this search path may be queried using
1322 <command>systemd-path search-binaries-default</command>.</para>
1323
1324 <para>The command line accepts <literal>%</literal> specifiers as described in
1325 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
1326
1327 <para>Basic environment variable substitution is supported. Use
1328 <literal>${FOO}</literal> as part of a word, or as a word of its
1329 own, on the command line, in which case it will be erased and replaced
1330 by the exact value of the environment variable (if any) including all
1331 whitespace it contains, always resulting in exactly a single argument.
1332 Use <literal>$FOO</literal> as a separate word on the command line, in
1333 which case it will be replaced by the value of the environment
1334 variable split at whitespace, resulting in zero or more arguments.
1335 For this type of expansion, quotes are respected when splitting
1336 into words, and afterwards removed.</para>
1337
1338 <para>Example:</para>
1339
1340 <programlisting>Environment="ONE=one" 'TWO=two two'
1341 ExecStart=echo $ONE $TWO ${TWO}</programlisting>
1342
1343 <para>This will execute <command>/bin/echo</command> with four
1344 arguments: <literal>one</literal>, <literal>two</literal>,
1345 <literal>two</literal>, and <literal>two two</literal>.</para>
1346
1347 <para>Example:</para>
1348 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
1349 ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1350 ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
1351 <para>This results in <filename>/bin/echo</filename> being
1352 called twice, the first time with arguments
1353 <literal>'one'</literal>,
1354 <literal>'two two' too</literal>, <literal></literal>,
1355 and the second time with arguments
1356 <literal>one</literal>, <literal>two two</literal>,
1357 <literal>too</literal>.
1358 </para>
1359
1360 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1361 Variables whose value is not known at expansion time are treated
1362 as empty strings. Note that the first argument (i.e. the program
1363 to execute) may not be a variable.</para>
1364
1365 <para>Variables to be used in this fashion may be defined through
1366 <varname>Environment=</varname> and
1367 <varname>EnvironmentFile=</varname>. In addition, variables listed
1368 in the section "Environment variables in spawned processes" in
1369 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1370 which are considered "static configuration", may be used (this
1371 includes e.g. <varname>$USER</varname>, but not
1372 <varname>$TERM</varname>).</para>
1373
1374 <para>Note that shell command lines are not directly supported. If
1375 shell command lines are to be used, they need to be passed
1376 explicitly to a shell implementation of some kind. Example:</para>
1377 <programlisting>ExecStart=sh -c 'dmesg | tac'</programlisting>
1378
1379 <para>Example:</para>
1380
1381 <programlisting>ExecStart=echo one ; echo "two two"</programlisting>
1382
1383 <para>This will execute <command>echo</command> two times,
1384 each time with one argument: <literal>one</literal> and
1385 <literal>two two</literal>, respectively. Because two commands are
1386 specified, <varname>Type=oneshot</varname> must be used.</para>
1387
1388 <para>Example:</para>
1389
1390 <programlisting>Type=oneshot
1391 ExecStart=:echo $USER ; -false ; +:@true $TEST</programlisting>
1392
1393 <para>This will execute <command>/usr/bin/echo</command> with the literal argument
1394 <literal>$USER</literal> (<literal>:</literal> suppresses variable expansion), and then
1395 <command>/usr/bin/false</command> (the return value will be ignored because <literal>-</literal>
1396 suppresses checking of the return value), and <command>/usr/bin/true</command> (with elevated privileges,
1397 with <literal>$TEST</literal> as <constant>argv[0]</constant>).</para>
1398
1399 <para>Example:</para>
1400
1401 <programlisting>ExecStart=echo / &gt;/dev/null &amp; \; \
1402 ls</programlisting>
1403
1404 <para>This will execute <command>echo</command>
1405 with five arguments: <literal>/</literal>,
1406 <literal>&gt;/dev/null</literal>,
1407 <literal>&amp;</literal>, <literal>;</literal>, and
1408 <literal>ls</literal>.</para>
1409 </refsect1>
1410
1411 <refsect1>
1412 <title>Examples</title>
1413
1414 <example>
1415 <title>Simple service</title>
1416
1417 <para>The following unit file creates a service that will
1418 execute <filename index="false">/usr/sbin/foo-daemon</filename>. Since no
1419 <varname>Type=</varname> is specified, the default
1420 <varname>Type=</varname><option>simple</option> will be assumed.
1421 systemd will assume the unit to be started immediately after the
1422 program has begun executing.</para>
1423
1424 <programlisting>[Unit]
1425 Description=Foo
1426
1427 [Service]
1428 ExecStart=/usr/sbin/foo-daemon
1429
1430 [Install]
1431 WantedBy=multi-user.target</programlisting>
1432
1433 <para>Note that systemd assumes here that the process started by
1434 systemd will continue running until the service terminates. If
1435 the program daemonizes itself (i.e. forks), please use
1436 <varname>Type=</varname><option>forking</option> instead.</para>
1437
1438 <para>Since no <varname>ExecStop=</varname> was specified,
1439 systemd will send SIGTERM to all processes started from this
1440 service, and after a timeout also SIGKILL. This behavior can be
1441 modified, see
1442 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1443 for details.</para>
1444
1445 <para>Note that this unit type does not include any type of notification when a service has completed
1446 initialization. For this, you should use other unit types, such as
1447 <varname>Type=</varname><option>notify</option>/<varname>Type=</varname><option>notify-reload</option>
1448 if the service understands systemd's notification protocol,
1449 <varname>Type=</varname><option>forking</option> if the service can background itself or
1450 <varname>Type=</varname><option>dbus</option> if the unit acquires a DBus name once initialization is
1451 complete. See below.</para>
1452 </example>
1453
1454 <example>
1455 <title>Oneshot service</title>
1456
1457 <para>Sometimes, units should just execute an action without
1458 keeping active processes, such as a filesystem check or a
1459 cleanup action on boot. For this,
1460 <varname>Type=</varname><option>oneshot</option> exists. Units
1461 of this type will wait until the process specified terminates
1462 and then fall back to being inactive. The following unit will
1463 perform a cleanup action:</para>
1464
1465 <programlisting>[Unit]
1466 Description=Cleanup old Foo data
1467
1468 [Service]
1469 Type=oneshot
1470 ExecStart=/usr/sbin/foo-cleanup
1471
1472 [Install]
1473 WantedBy=multi-user.target</programlisting>
1474
1475 <para>Note that systemd will consider the unit to be in the
1476 state "starting" until the program has terminated, so ordered
1477 dependencies will wait for the program to finish before starting
1478 themselves. The unit will revert to the "inactive" state after
1479 the execution is done, never reaching the "active" state. That
1480 means another request to start the unit will perform the action
1481 again.</para>
1482
1483 <para><varname>Type=</varname><option>oneshot</option> are the
1484 only service units that may have more than one
1485 <varname>ExecStart=</varname> specified. For units with multiple
1486 commands (<varname index="false">Type=oneshot</varname>), all commands will be run again.</para>
1487 <para> For <varname index="false">Type=oneshot</varname>, <varname>Restart=</varname><option>always</option>
1488 and <varname>Restart=</varname><option>on-success</option> are <emphasis>not</emphasis> allowed.</para>
1489 </example>
1490
1491 <example>
1492 <title>Stoppable oneshot service</title>
1493
1494 <para>Similarly to the oneshot services, there are sometimes
1495 units that need to execute a program to set up something and
1496 then execute another to shut it down, but no process remains
1497 active while they are considered "started". Network
1498 configuration can sometimes fall into this category. Another use
1499 case is if a oneshot service shall not be executed each time
1500 when they are pulled in as a dependency, but only the first
1501 time.</para>
1502
1503 <para>For this, systemd knows the setting
1504 <varname>RemainAfterExit=</varname><option>yes</option>, which
1505 causes systemd to consider the unit to be active if the start
1506 action exited successfully. This directive can be used with all
1507 types, but is most useful with
1508 <varname>Type=</varname><option>oneshot</option> and
1509 <varname>Type=</varname><option>simple</option>. With
1510 <varname>Type=</varname><option>oneshot</option>, systemd waits
1511 until the start action has completed before it considers the
1512 unit to be active, so dependencies start only after the start
1513 action has succeeded. With
1514 <varname>Type=</varname><option>simple</option>, dependencies
1515 will start immediately after the start action has been
1516 dispatched. The following unit provides an example for a simple
1517 static firewall.</para>
1518
1519 <programlisting>[Unit]
1520 Description=Simple firewall
1521
1522 [Service]
1523 Type=oneshot
1524 RemainAfterExit=yes
1525 ExecStart=/usr/local/sbin/simple-firewall-start
1526 ExecStop=/usr/local/sbin/simple-firewall-stop
1527
1528 [Install]
1529 WantedBy=multi-user.target</programlisting>
1530
1531 <para>Since the unit is considered to be running after the start
1532 action has exited, invoking <command>systemctl start</command>
1533 on that unit again will cause no action to be taken.</para>
1534 </example>
1535
1536 <example>
1537 <title>Traditional forking services</title>
1538
1539 <para>Many traditional daemons/services background (i.e. fork,
1540 daemonize) themselves when starting. Set
1541 <varname>Type=</varname><option>forking</option> in the
1542 service's unit file to support this mode of operation. systemd
1543 will consider the service to be in the process of initialization
1544 while the original program is still running. Once it exits
1545 successfully and at least a process remains (and
1546 <varname>RemainAfterExit=</varname><option>no</option>), the
1547 service is considered started.</para>
1548
1549 <para>Often, a traditional daemon only consists of one process.
1550 Therefore, if only one process is left after the original
1551 process terminates, systemd will consider that process the main
1552 process of the service. In that case, the
1553 <varname>$MAINPID</varname> variable will be available in
1554 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1555 etc.</para>
1556
1557 <para>In case more than one process remains, systemd will be
1558 unable to determine the main process, so it will not assume
1559 there is one. In that case, <varname>$MAINPID</varname> will not
1560 expand to anything. However, if the process decides to write a
1561 traditional PID file, systemd will be able to read the main PID
1562 from there. Please set <varname>PIDFile=</varname> accordingly.
1563 Note that the daemon should write that file before finishing
1564 with its initialization. Otherwise, systemd might try to read the
1565 file before it exists.</para>
1566
1567 <para>The following example shows a simple daemon that forks and
1568 just starts one process in the background:</para>
1569
1570 <programlisting>[Unit]
1571 Description=Some simple daemon
1572
1573 [Service]
1574 Type=forking
1575 ExecStart=/usr/sbin/my-simple-daemon -d
1576
1577 [Install]
1578 WantedBy=multi-user.target</programlisting>
1579
1580 <para>Please see
1581 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1582 for details on how you can influence the way systemd terminates
1583 the service.</para>
1584 </example>
1585
1586 <example>
1587 <title>DBus services</title>
1588
1589 <para>For services that acquire a name on the DBus system bus,
1590 use <varname>Type=</varname><option>dbus</option> and set
1591 <varname>BusName=</varname> accordingly. The service should not
1592 fork (daemonize). systemd will consider the service to be
1593 initialized once the name has been acquired on the system bus.
1594 The following example shows a typical DBus service:</para>
1595
1596 <programlisting>[Unit]
1597 Description=Simple DBus service
1598
1599 [Service]
1600 Type=dbus
1601 BusName=org.example.simple-dbus-service
1602 ExecStart=/usr/sbin/simple-dbus-service
1603
1604 [Install]
1605 WantedBy=multi-user.target</programlisting>
1606
1607 <para>For <emphasis>bus-activatable</emphasis> services, do not
1608 include a [Install] section in the systemd
1609 service file, but use the <varname>SystemdService=</varname>
1610 option in the corresponding DBus service file, for example
1611 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
1612
1613 <programlisting>[D-BUS Service]
1614 Name=org.example.simple-dbus-service
1615 Exec=/usr/sbin/simple-dbus-service
1616 User=root
1617 SystemdService=simple-dbus-service.service</programlisting>
1618
1619 <para>Please see
1620 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1621 for details on how you can influence the way systemd terminates
1622 the service.</para>
1623 </example>
1624
1625 <example>
1626 <title>Services that notify systemd about their initialization</title>
1627
1628 <para><varname>Type=</varname><option>simple</option> services are really easy to write, but have the
1629 major disadvantage of systemd not being able to tell when initialization of the given service is
1630 complete. For this reason, systemd supports a simple notification protocol that allows daemons to make
1631 systemd aware that they are done initializing. Use <varname>Type=</varname><option>notify</option> or
1632 <varname>Type=</varname><option>notify-reload</option> for this. A typical service file for such a
1633 daemon would look like this:</para>
1634
1635 <programlisting>[Unit]
1636 Description=Simple notifying service
1637
1638 [Service]
1639 Type=notify
1640 ExecStart=/usr/sbin/simple-notifying-service
1641
1642 [Install]
1643 WantedBy=multi-user.target</programlisting>
1644
1645 <para>Note that the daemon has to support systemd's notification
1646 protocol, else systemd will think the service has not started yet
1647 and kill it after a timeout. For an example of how to update
1648 daemons to support this protocol transparently, take a look at
1649 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1650 systemd will consider the unit to be in the 'starting' state
1651 until a readiness notification has arrived.</para>
1652
1653 <para>Please see
1654 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1655 for details on how you can influence the way systemd terminates
1656 the service.</para>
1657 </example>
1658 </refsect1>
1659
1660 <refsect1>
1661 <title>See Also</title>
1662 <para>
1663 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1664 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1665 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1666 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1667 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1668 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1669 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1670 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
1671 <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>
1672 </para>
1673 </refsect1>
1674
1675 </refentry>