]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.service.xml
man: briefly document Following=
[thirdparty/systemd.git] / man / systemd.service.xml
CommitLineData
c129bd5d 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
d1ab0ca0 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
12b42c76 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
d1ab0ca0
LP
4
5<!--
572eb058
ZJS
6 SPDX-License-Identifier: LGPL-2.1+
7
d1ab0ca0
LP
8 This file is part of systemd.
9
10 Copyright 2010 Lennart Poettering
d1ab0ca0
LP
11-->
12
13<refentry id="systemd.service">
798d3a52
ZJS
14 <refentryinfo>
15 <title>systemd.service</title>
16 <productname>systemd</productname>
17
18 <authorgroup>
19 <author>
20 <contrib>Developer</contrib>
21 <firstname>Lennart</firstname>
22 <surname>Poettering</surname>
23 <email>lennart@poettering.net</email>
24 </author>
25 </authorgroup>
26 </refentryinfo>
27
28 <refmeta>
29 <refentrytitle>systemd.service</refentrytitle>
30 <manvolnum>5</manvolnum>
31 </refmeta>
32
33 <refnamediv>
34 <refname>systemd.service</refname>
35 <refpurpose>Service unit configuration</refpurpose>
36 </refnamediv>
37
38 <refsynopsisdiv>
39 <para><filename><replaceable>service</replaceable>.service</filename></para>
40 </refsynopsisdiv>
41
42 <refsect1>
43 <title>Description</title>
44
45 <para>A unit configuration file whose name ends in
ed10715a 46 <literal>.service</literal> encodes information about a process
798d3a52
ZJS
47 controlled and supervised by systemd.</para>
48
49 <para>This man page lists the configuration options specific to
50 this unit type. See
51 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
52 for the common options of all unit configuration files. The common
53 configuration items are configured in the generic
54 <literal>[Unit]</literal> and <literal>[Install]</literal>
55 sections. The service specific configuration options are
56 configured in the <literal>[Service]</literal> section.</para>
57
58 <para>Additional options are listed in
59 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
60 which define the execution environment the commands are executed
61 in, and in
62 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
63 which define the way the processes of the service are terminated,
64 and in
65 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
66 which configure resource control settings for the processes of the
67 service.</para>
68
798d3a52
ZJS
69 <para>If a service is requested under a certain name but no unit
70 configuration file is found, systemd looks for a SysV init script
71 by the same name (with the <filename>.service</filename> suffix
72 removed) and dynamically creates a service unit from that script.
73 This is useful for compatibility with SysV. Note that this
74 compatibility is quite comprehensive but not 100%. For details
75 about the incompatibilities, see the <ulink
28a0ad81 76 url="https://www.freedesktop.org/wiki/Software/systemd/Incompatibilities">Incompatibilities
c129bd5d
LP
77 with SysV</ulink> document.</para>
78 </refsect1>
79
80 <refsect1>
45f09f93
JL
81 <title>Implicit Dependencies</title>
82
83 <para>The following dependencies are implicitly added:</para>
84
85 <itemizedlist>
86 <listitem><para>Services with <varname>Type=dbus</varname> set automatically
87 acquire dependencies of type <varname>Requires=</varname> and
88 <varname>After=</varname> on
89 <filename>dbus.socket</filename>.</para></listitem>
90
91 <listitem><para>Socket activated services are automatically ordered after
92 their activating <filename>.socket</filename> units via an
93 automatic <varname>After=</varname> dependency.
94 Services also pull in all <filename>.socket</filename> units
95 listed in <varname>Sockets=</varname> via automatic
96 <varname>Wants=</varname> and <varname>After=</varname> dependencies.</para></listitem>
97 </itemizedlist>
ae0a5fb1 98
c129bd5d
LP
99 <para>Additional implicit dependencies may be added as result of
100 execution and resource control parameters as documented in
101 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
102 and
103 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
798d3a52
ZJS
104 </refsect1>
105
45f09f93
JL
106 <refsect1>
107 <title>Default Dependencies</title>
108
109 <para>The following dependencies are added unless <varname>DefaultDependencies=no</varname> is set:</para>
110
111 <itemizedlist>
112 <listitem><para>Service units will have dependencies of type <varname>Requires=</varname> and
113 <varname>After=</varname> on <filename>sysinit.target</filename>, a dependency of type <varname>After=</varname> on
114 <filename>basic.target</filename> as well as dependencies of type <varname>Conflicts=</varname> and
115 <varname>Before=</varname> on <filename>shutdown.target</filename>. These ensure that normal service units pull in
116 basic system initialization, and are terminated cleanly prior to system shutdown. Only services involved with early
117 boot or late system shutdown should disable this option.</para></listitem>
118
119 <listitem><para>Instanced service units (i.e. service units with an <literal>@</literal> in their name) are assigned by
120 default a per-template slice unit (see
121 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>), named after the
122 template unit, containing all instances of the specific template. This slice is normally stopped at shutdown,
123 together with all template instances. If that is not desired, set <varname>DefaultDependencies=no</varname> in the
124 template unit, and either define your own per-template slice unit file that also sets
125 <varname>DefaultDependencies=no</varname>, or set <varname>Slice=system.slice</varname> (or another suitable slice)
126 in the template unit. Also see
127 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
128 </para></listitem>
129 </itemizedlist>
130 </refsect1>
131
798d3a52
ZJS
132 <refsect1>
133 <title>Options</title>
134
135 <para>Service files must include a <literal>[Service]</literal>
136 section, which carries information about the service and the
137 process it supervises. A number of options that may be used in
138 this section are shared with other unit types. These options are
139 documented in
aa9f9e58
LW
140 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
141 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
798d3a52 142 and
aa9f9e58 143 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
798d3a52
ZJS
144 The options specific to the <literal>[Service]</literal> section
145 of service units are the following:</para>
146
147 <variablelist class='unit-directives'>
148 <varlistentry>
149 <term><varname>Type=</varname></term>
150
151 <listitem><para>Configures the process start-up type for this
152 service unit. One of
153 <option>simple</option>,
154 <option>forking</option>,
155 <option>oneshot</option>,
156 <option>dbus</option>,
157 <option>notify</option> or
158 <option>idle</option>.</para>
159
160 <para>If set to <option>simple</option> (the default if
161 neither <varname>Type=</varname> nor
162 <varname>BusName=</varname>, but <varname>ExecStart=</varname>
163 are specified), it is expected that the process configured
164 with <varname>ExecStart=</varname> is the main process of the
165 service. In this mode, if the process offers functionality to
166 other processes on the system, its communication channels
167 should be installed before the daemon is started up (e.g.
168 sockets set up by systemd, via socket activation), as systemd
169 will immediately proceed starting follow-up units.</para>
170
171 <para>If set to <option>forking</option>, it is expected that
172 the process configured with <varname>ExecStart=</varname> will
173 call <function>fork()</function> as part of its start-up. The
174 parent process is expected to exit when start-up is complete
175 and all communication channels are set up. The child continues
176 to run as the main daemon process. This is the behavior of
177 traditional UNIX daemons. If this setting is used, it is
178 recommended to also use the <varname>PIDFile=</varname>
179 option, so that systemd can identify the main process of the
180 daemon. systemd will proceed with starting follow-up units as
181 soon as the parent process exits.</para>
182
183 <para>Behavior of <option>oneshot</option> is similar to
184 <option>simple</option>; however, it is expected that the
185 process has to exit before systemd starts follow-up units.
186 <varname>RemainAfterExit=</varname> is particularly useful for
187 this type of service. This is the implied default if neither
bda99fab 188 <varname>Type=</varname> nor <varname>ExecStart=</varname> are
798d3a52
ZJS
189 specified.</para>
190
191 <para>Behavior of <option>dbus</option> is similar to
192 <option>simple</option>; however, it is expected that the
193 daemon acquires a name on the D-Bus bus, as configured by
194 <varname>BusName=</varname>. systemd will proceed with
195 starting follow-up units after the D-Bus bus name has been
196 acquired. Service units with this option configured implicitly
197 gain dependencies on the <filename>dbus.socket</filename>
198 unit. This type is the default if <varname>BusName=</varname>
199 is specified.</para>
200
201 <para>Behavior of <option>notify</option> is similar to
202 <option>simple</option>; however, it is expected that the
203 daemon sends a notification message via
204 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
205 or an equivalent call when it has finished starting up.
206 systemd will proceed with starting follow-up units after this
207 notification message has been sent. If this option is used,
208 <varname>NotifyAccess=</varname> (see below) should be set to
209 open access to the notification socket provided by systemd. If
aa4f6cf1
LB
210 <varname>NotifyAccess=</varname> is missing or set to
211 <option>none</option>, it will be forcibly set to
212 <option>main</option>. Note that currently
798d3a52
ZJS
213 <varname>Type=</varname><option>notify</option> will not work
214 if used in combination with
215 <varname>PrivateNetwork=</varname><option>yes</option>.</para>
216
6dcda09c 217 <para>Behavior of <option>idle</option> is very similar to <option>simple</option>; however, actual execution
3f2d1365 218 of the service program is delayed until all active jobs are dispatched. This may be used to avoid interleaving
6dcda09c
LP
219 of output of shell services with the status output on the console. Note that this type is useful only to
220 improve console output, it is not useful as a general unit ordering tool, and the effect of this service type
3f2d1365 221 is subject to a 5s time-out, after which the service program is invoked anyway.</para>
798d3a52
ZJS
222 </listitem>
223 </varlistentry>
224
225 <varlistentry>
226 <term><varname>RemainAfterExit=</varname></term>
227
228 <listitem><para>Takes a boolean value that specifies whether
229 the service shall be considered active even when all its
230 processes exited. Defaults to <option>no</option>.</para>
231 </listitem>
232 </varlistentry>
233
234 <varlistentry>
235 <term><varname>GuessMainPID=</varname></term>
236
237 <listitem><para>Takes a boolean value that specifies whether
238 systemd should try to guess the main PID of a service if it
239 cannot be determined reliably. This option is ignored unless
240 <option>Type=forking</option> is set and
241 <option>PIDFile=</option> is unset because for the other types
242 or with an explicitly configured PID file, the main PID is
243 always known. The guessing algorithm might come to incorrect
244 conclusions if a daemon consists of more than one process. If
245 the main PID cannot be determined, failure detection and
246 automatic restarting of a service will not work reliably.
247 Defaults to <option>yes</option>.</para>
248 </listitem>
249 </varlistentry>
250
251 <varlistentry>
252 <term><varname>PIDFile=</varname></term>
253
db256aab
LP
254 <listitem><para>Takes an absolute path referring to the PID file of the service. Usage of this option is
255 recommended for services where <varname>Type=</varname> is set to <option>forking</option>. The service manager
256 will read the PID of the main process of the service from this file after start-up of the service. The service
257 manager will not write to the file configured here, although it will remove the file after the service has shut
258 down if it still exists. The PID file does not need to be owned by a privileged user, but if it is owned by an
259 unprivileged user additional safety restrictions are enforced: the file may not be a symlink to a file owned by
260 a different user (neither directly nor indirectly), and the PID file must refer to a process already belonging
261 to the service.</para></listitem>
798d3a52
ZJS
262 </varlistentry>
263
264 <varlistentry>
265 <term><varname>BusName=</varname></term>
266
267 <listitem><para>Takes a D-Bus bus name that this service is
268 reachable as. This option is mandatory for services where
269 <varname>Type=</varname> is set to
270 <option>dbus</option>.</para>
271 </listitem>
272 </varlistentry>
273
798d3a52
ZJS
274 <varlistentry>
275 <term><varname>ExecStart=</varname></term>
276 <listitem><para>Commands with their arguments that are
277 executed when this service is started. The value is split into
a8eaaee7 278 zero or more command lines according to the rules described
798d3a52
ZJS
279 below (see section "Command Lines" below).
280 </para>
281
29df65f9
ZJS
282 <para>Unless <varname>Type=</varname> is <option>oneshot</option>, exactly one command must be given. When
283 <varname>Type=oneshot</varname> is used, zero or more commands may be specified. Commands may be specified by
284 providing multiple command lines in the same directive, or alternatively, this directive may be specified more
285 than once with the same effect. If the empty string is assigned to this option, the list of commands to start
286 is reset, prior assignments of this option will have no effect. If no <varname>ExecStart=</varname> is
e4b45b32
LP
287 specified, then the service must have <varname>RemainAfterExit=yes</varname> and at least one
288 <varname>ExecStop=</varname> line set. (Services lacking both <varname>ExecStart=</varname> and
289 <varname>ExecStop=</varname> are not valid.)</para>
798d3a52 290
43eb109a 291 <para>For each of the specified commands, the first argument must be an absolute path to an
1245e413 292 executable. Optionally, this filename may be prefixed with a number of special characters:</para>
165a31c0
LP
293
294 <table>
295 <title>Special executable prefixes</title>
296
297 <tgroup cols='2'>
298 <colspec colname='prefix'/>
299 <colspec colname='meaning'/>
300
301 <thead>
302 <row>
303 <entry>Prefix</entry>
304 <entry>Effect</entry>
305 </row>
306 </thead>
307 <tbody>
308 <row>
309 <entry><literal>@</literal></entry>
310 <entry>If the executable path is prefixed with <literal>@</literal>, the second specified token will be passed as <literal>argv[0]</literal> to the executed process (instead of the actual filename), followed by the further arguments specified.</entry>
311 </row>
312
313 <row>
314 <entry><literal>-</literal></entry>
315 <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 ignored and considered success.</entry>
316 </row>
317
318 <row>
319 <entry><literal>+</literal></entry>
320 <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).</entry>
321 </row>
322
323 <row>
324 <entry><literal>!</literal></entry>
325
326 <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 the 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>
327 </row>
328
329 <row>
330 <entry><literal>!!</literal></entry>
331
132523e7 332 <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>
165a31c0
LP
333 </row>
334 </tbody>
335 </tgroup>
336 </table>
337
338 <para><literal>@</literal>, <literal>-</literal>, and one of
339 <literal>+</literal>/<literal>!</literal>/<literal>!!</literal> may be used together and they can appear in any
ee905de0 340 order. However, only one of <literal>+</literal>, <literal>!</literal>, <literal>!!</literal> may be used at a
165a31c0 341 time. Note that these prefixes are also supported for the other command line settings,
78a263f4 342 i.e. <varname>ExecStartPre=</varname>, <varname>ExecStartPost=</varname>, <varname>ExecReload=</varname>,
165a31c0 343 <varname>ExecStop=</varname> and <varname>ExecStopPost=</varname>.</para>
798d3a52
ZJS
344
345 <para>If more than one command is specified, the commands are
346 invoked sequentially in the order they appear in the unit
347 file. If one of the commands fails (and is not prefixed with
348 <literal>-</literal>), other lines are not executed, and the
349 unit is considered failed.</para>
350
351 <para>Unless <varname>Type=forking</varname> is set, the
352 process started via this command line will be considered the
353 main process of the daemon.</para>
354 </listitem>
355 </varlistentry>
356
357 <varlistentry>
358 <term><varname>ExecStartPre=</varname></term>
359 <term><varname>ExecStartPost=</varname></term>
360 <listitem><para>Additional commands that are executed before
361 or after the command in <varname>ExecStart=</varname>,
362 respectively. Syntax is the same as for
363 <varname>ExecStart=</varname>, except that multiple command
364 lines are allowed and the commands are executed one after the
365 other, serially.</para>
366
367 <para>If any of those commands (not prefixed with
368 <literal>-</literal>) fail, the rest are not executed and the
369 unit is considered failed.</para>
b481de3b 370
12e2683d
RM
371 <para><varname>ExecStart=</varname> commands are only run after
372 all <varname>ExecStartPre=</varname> commands that were not prefixed
373 with a <literal>-</literal> exit successfully.</para>
374
80af263b
LP
375 <para><varname>ExecStartPost=</varname> commands are only run after the commands specified in
376 <varname>ExecStart=</varname> have been invoked successfully, as determined by <varname>Type=</varname>
377 (i.e. the process has been started for <varname>Type=simple</varname> or <varname>Type=idle</varname>, the last
378 <varname>ExecStart=</varname> process exited successfully for <varname>Type=oneshot</varname>, the initial
379 process exited successfully for <varname>Type=forking</varname>, <literal>READY=1</literal> is sent for
380 <varname>Type=notify</varname>, or the <varname>BusName=</varname> has been taken for
381 <varname>Type=dbus</varname>).</para>
12e2683d 382
b481de3b
LP
383 <para>Note that <varname>ExecStartPre=</varname> may not be
384 used to start long-running processes. All processes forked
385 off by processes invoked via <varname>ExecStartPre=</varname> will
386 be killed before the next service process is run.</para>
ce359e98
LP
387
388 <para>Note that if any of the commands specified in <varname>ExecStartPre=</varname>,
389 <varname>ExecStart=</varname>, or <varname>ExecStartPost=</varname> fail (and are not prefixed with
390 <literal>-</literal>, see above) or time out before the service is fully up, execution continues with commands
391 specified in <varname>ExecStopPost=</varname>, the commands in <varname>ExecStop=</varname> are skipped.</para>
798d3a52
ZJS
392 </listitem>
393 </varlistentry>
394
395 <varlistentry>
396 <term><varname>ExecReload=</varname></term>
397 <listitem><para>Commands to execute to trigger a configuration
398 reload in the service. This argument takes multiple command
399 lines, following the same scheme as described for
400 <varname>ExecStart=</varname> above. Use of this setting is
401 optional. Specifier and environment variable substitution is
402 supported here following the same scheme as for
403 <varname>ExecStart=</varname>.</para>
404
405 <para>One additional, special environment variable is set: if
406 known, <varname>$MAINPID</varname> is set to the main process
407 of the daemon, and may be used for command lines like the
408 following:</para>
409
410 <programlisting>/bin/kill -HUP $MAINPID</programlisting>
411
412 <para>Note however that reloading a daemon by sending a signal
413 (as with the example line above) is usually not a good choice,
414 because this is an asynchronous operation and hence not
415 suitable to order reloads of multiple services against each
416 other. It is strongly recommended to set
417 <varname>ExecReload=</varname> to a command that not only
418 triggers a configuration reload of the daemon, but also
419 synchronously waits for it to complete.</para>
420 </listitem>
421 </varlistentry>
422
423 <varlistentry>
424 <term><varname>ExecStop=</varname></term>
425 <listitem><para>Commands to execute to stop the service
426 started via <varname>ExecStart=</varname>. This argument takes
427 multiple command lines, following the same scheme as described
428 for <varname>ExecStart=</varname> above. Use of this setting
429 is optional. After the commands configured in this option are
23656d2b
AY
430 run, it is implied that the service is stopped, and any processes
431 remaining for it are terminated
798d3a52
ZJS
432 according to the <varname>KillMode=</varname> setting (see
433 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
cceb20c7
LP
434 If this option is not specified, the process is terminated by
435 sending the signal specified in <varname>KillSignal=</varname>
436 when service stop is requested. Specifier and environment
437 variable substitution is supported (including
438 <varname>$MAINPID</varname>, see above).</para>
439
99479986
FS
440 <para>Note that it is usually not sufficient to specify a command for this setting that only asks the service
441 to terminate (for example, by queuing some form of termination signal for it), but does not wait for it to do
442 so. Since the remaining processes of the services are killed according to <varname>KillMode=</varname> and
443 <varname>KillSignal=</varname> as described above immediately after the command exited, this may not result in
444 a clean stop. The specified command should hence be a synchronous operation, not an asynchronous one.</para>
ce359e98
LP
445
446 <para>Note that the commands specified in <varname>ExecStop=</varname> are only executed when the service
07ff561c 447 started successfully first. They are not invoked if the service was never started at all, or in case its
ce359e98
LP
448 start-up failed, for example because any of the commands specified in <varname>ExecStart=</varname>,
449 <varname>ExecStartPre=</varname> or <varname>ExecStartPost=</varname> failed (and weren't prefixed with
450 <literal>-</literal>, see above) or timed out. Use <varname>ExecStopPost=</varname> to invoke commands when a
8adf5358
LP
451 service failed to start up correctly and is shut down again. Also note that, service restart requests are
452 implemented as stop operations followed by start operations. This means that <varname>ExecStop=</varname> and
453 <varname>ExecStopPost=</varname> are executed during a service restart operation.</para>
ce359e98
LP
454
455 <para>It is recommended to use this setting for commands that communicate with the service requesting clean
456 termination. When the commands specified with this option are executed it should be assumed that the service is
457 still fully up and is able to react correctly to all commands. For post-mortem clean-up steps use
458 <varname>ExecStopPost=</varname> instead.</para></listitem>
798d3a52
ZJS
459 </varlistentry>
460
461 <varlistentry>
462 <term><varname>ExecStopPost=</varname></term>
ce359e98
LP
463 <listitem><para>Additional commands that are executed after the service is stopped. This includes cases where
464 the commands configured in <varname>ExecStop=</varname> were used, where the service does not have any
465 <varname>ExecStop=</varname> defined, or where the service exited unexpectedly. This argument takes multiple
466 command lines, following the same scheme as described for <varname>ExecStart=</varname>. Use of these settings
467 is optional. Specifier and environment variable substitution is supported. Note that – unlike
468 <varname>ExecStop=</varname> – commands specified with this setting are invoked when a service failed to start
469 up correctly and is shut down again.</para>
470
471 <para>It is recommended to use this setting for clean-up operations that shall be executed even when the
472 service failed to start up correctly. Commands configured with this setting need to be able to operate even if
473 the service failed starting up half-way and left incompletely initialized data around. As the service's
474 processes have been terminated already when the commands specified with this setting are executed they should
136dc4c4
LP
475 not attempt to communicate with them.</para>
476
477 <para>Note that all commands that are configured with this setting are invoked with the result code of the
478 service, as well as the main process' exit code and status, set in the <varname>$SERVICE_RESULT</varname>,
479 <varname>$EXIT_CODE</varname> and <varname>$EXIT_STATUS</varname> environment variables, see
480 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
481 details.</para></listitem>
798d3a52
ZJS
482 </varlistentry>
483
484 <varlistentry>
485 <term><varname>RestartSec=</varname></term>
486 <listitem><para>Configures the time to sleep before restarting
487 a service (as configured with <varname>Restart=</varname>).
488 Takes a unit-less value in seconds, or a time span value such
489 as "5min 20s". Defaults to 100ms.</para></listitem>
490 </varlistentry>
491
492 <varlistentry>
493 <term><varname>TimeoutStartSec=</varname></term>
494 <listitem><para>Configures the time to wait for start-up. If a
495 daemon service does not signal start-up completion within the
496 configured time, the service will be considered failed and
497 will be shut down again. Takes a unit-less value in seconds,
498 or a time span value such as "5min 20s". Pass
2c29d332 499 <literal>infinity</literal> to disable the timeout logic. Defaults to
798d3a52
ZJS
500 <varname>DefaultTimeoutStartSec=</varname> from the manager
501 configuration file, except when
502 <varname>Type=oneshot</varname> is used, in which case the
503 timeout is disabled by default (see
504 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
a327431b
DB
505 </para>
506
507 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause
508 the start time to be extended beyond <varname>TimeoutStartSec=</varname>. The first receipt of this message
509 must occur before <varname>TimeoutStartSec=</varname> is exceeded, and once the start time has exended beyond
510 <varname>TimeoutStartSec=</varname>, the service manager will allow the service to continue to start, provided
511 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified until the service
512 startup status is finished by <literal>READY=1</literal>. (see
513 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
798d3a52
ZJS
514 </para></listitem>
515 </varlistentry>
516
517 <varlistentry>
518 <term><varname>TimeoutStopSec=</varname></term>
519 <listitem><para>Configures the time to wait for stop. If a
520 service is asked to stop, but does not terminate in the
521 specified time, it will be terminated forcibly via
522 <constant>SIGTERM</constant>, and after another timeout of
523 equal duration with <constant>SIGKILL</constant> (see
524 <varname>KillMode=</varname> in
525 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
526 Takes a unit-less value in seconds, or a time span value such
2c29d332 527 as "5min 20s". Pass <literal>infinity</literal> to disable the
798d3a52
ZJS
528 timeout logic. Defaults to
529 <varname>DefaultTimeoutStopSec=</varname> from the manager
530 configuration file (see
531 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
a327431b
DB
532 </para>
533
534 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause
535 the stop time to be extended beyond <varname>TimeoutStopSec=</varname>. The first receipt of this message
536 must occur before <varname>TimeoutStopSec=</varname> is exceeded, and once the stop time has exended beyond
537 <varname>TimeoutStopSec=</varname>, the service manager will allow the service to continue to stop, provided
538 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified, or terminates itself
539 (see <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
798d3a52
ZJS
540 </para></listitem>
541 </varlistentry>
542
543 <varlistentry>
544 <term><varname>TimeoutSec=</varname></term>
545 <listitem><para>A shorthand for configuring both
546 <varname>TimeoutStartSec=</varname> and
547 <varname>TimeoutStopSec=</varname> to the specified value.
548 </para></listitem>
549 </varlistentry>
550
2c29d332
LP
551 <varlistentry>
552 <term><varname>RuntimeMaxSec=</varname></term>
553
554 <listitem><para>Configures a maximum time for the service to run. If this is used and the service has been
555 active for longer than the specified time it is terminated and put into a failure state. Note that this setting
556 does not have any effect on <varname>Type=oneshot</varname> services, as they terminate immediately after
557 activation completed. Pass <literal>infinity</literal> (the default) to configure no runtime
a327431b
DB
558 limit.</para>
559
560 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause
561 the runtime to be extended beyond <varname>RuntimeMaxSec=</varname>. The first receipt of this message
562 must occur before <varname>RuntimeMaxSec=</varname> is exceeded, and once the runtime has exended beyond
563 <varname>RuntimeMaxSec=</varname>, the service manager will allow the service to continue to run, provided
564 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified until the service
565 shutdown is acheived by <literal>STOPPING=1</literal> (or termination). (see
566 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
567 </para></listitem>
2c29d332
LP
568 </varlistentry>
569
798d3a52
ZJS
570 <varlistentry>
571 <term><varname>WatchdogSec=</varname></term>
572 <listitem><para>Configures the watchdog timeout for a service.
573 The watchdog is activated when the start-up is completed. The
574 service must call
575 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
576 regularly with <literal>WATCHDOG=1</literal> (i.e. the
577 "keep-alive ping"). If the time between two such calls is
578 larger than the configured time, then the service is placed in
579 a failed state and it will be terminated with
22065311 580 <constant>SIGABRT</constant>. By setting
a0533c6d
EV
581 <varname>Restart=</varname> to <option>on-failure</option>,
582 <option>on-watchdog</option>, <option>on-abnormal</option> or
798d3a52
ZJS
583 <option>always</option>, the service will be automatically
584 restarted. The time configured here will be passed to the
585 executed service process in the
586 <varname>WATCHDOG_USEC=</varname> environment variable. This
587 allows daemons to automatically enable the keep-alive pinging
588 logic if watchdog support is enabled for the service. If this
589 option is used, <varname>NotifyAccess=</varname> (see below)
590 should be set to open access to the notification socket
591 provided by systemd. If <varname>NotifyAccess=</varname> is
592 not set, it will be implicitly set to <option>main</option>.
582f2fcb
EV
593 Defaults to 0, which disables this feature. The service can
594 check whether the service manager expects watchdog keep-alive
595 notifications. See
596 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
a0533c6d
EV
597 for details.
598 <citerefentry><refentrytitle>sd_event_set_watchdog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
599 may be used to enable automatic watchdog notification support.
582f2fcb 600 </para></listitem>
798d3a52
ZJS
601 </varlistentry>
602
603 <varlistentry>
604 <term><varname>Restart=</varname></term>
605 <listitem><para>Configures whether the service shall be
606 restarted when the service process exits, is killed, or a
607 timeout is reached. The service process may be the main
608 service process, but it may also be one of the processes
609 specified with <varname>ExecStartPre=</varname>,
610 <varname>ExecStartPost=</varname>,
611 <varname>ExecStop=</varname>,
612 <varname>ExecStopPost=</varname>, or
613 <varname>ExecReload=</varname>. When the death of the process
614 is a result of systemd operation (e.g. service stop or
615 restart), the service will not be restarted. Timeouts include
616 missing the watchdog "keep-alive ping" deadline and a service
617 start, reload, and stop operation timeouts.</para>
618
619 <para>Takes one of
620 <option>no</option>,
621 <option>on-success</option>,
622 <option>on-failure</option>,
623 <option>on-abnormal</option>,
624 <option>on-watchdog</option>,
625 <option>on-abort</option>, or
626 <option>always</option>.
627 If set to <option>no</option> (the default), the service will
628 not be restarted. If set to <option>on-success</option>, it
629 will be restarted only when the service process exits cleanly.
630 In this context, a clean exit means an exit code of 0, or one
631 of the signals
632 <constant>SIGHUP</constant>,
633 <constant>SIGINT</constant>,
634 <constant>SIGTERM</constant> or
635 <constant>SIGPIPE</constant>, and
636 additionally, exit statuses and signals specified in
637 <varname>SuccessExitStatus=</varname>. If set to
638 <option>on-failure</option>, the service will be restarted
639 when the process exits with a non-zero exit code, is
640 terminated by a signal (including on core dump, but excluding
ff9b60f3 641 the aforementioned four signals), when an operation (such as
798d3a52
ZJS
642 service reload) times out, and when the configured watchdog
643 timeout is triggered. If set to <option>on-abnormal</option>,
644 the service will be restarted when the process is terminated
645 by a signal (including on core dump, excluding the
646 aforementioned four signals), when an operation times out, or
647 when the watchdog timeout is triggered. If set to
648 <option>on-abort</option>, the service will be restarted only
649 if the service process exits due to an uncaught signal not
650 specified as a clean exit status. If set to
651 <option>on-watchdog</option>, the service will be restarted
652 only if the watchdog timeout for the service expires. If set
653 to <option>always</option>, the service will be restarted
654 regardless of whether it exited cleanly or not, got terminated
655 abnormally by a signal, or hit a timeout.</para>
656
657 <table>
658 <title>Exit causes and the effect of the <varname>Restart=</varname> settings on them</title>
659
660 <tgroup cols='2'>
661 <colspec colname='path' />
662 <colspec colname='expl' />
663 <thead>
664 <row>
665 <entry>Restart settings/Exit causes</entry>
666 <entry><option>no</option></entry>
667 <entry><option>always</option></entry>
668 <entry><option>on-success</option></entry>
669 <entry><option>on-failure</option></entry>
670 <entry><option>on-abnormal</option></entry>
671 <entry><option>on-abort</option></entry>
672 <entry><option>on-watchdog</option></entry>
673 </row>
674 </thead>
675 <tbody>
676 <row>
677 <entry>Clean exit code or signal</entry>
678 <entry/>
679 <entry>X</entry>
680 <entry>X</entry>
681 <entry/>
682 <entry/>
683 <entry/>
684 <entry/>
685 </row>
686 <row>
687 <entry>Unclean exit code</entry>
688 <entry/>
689 <entry>X</entry>
690 <entry/>
691 <entry>X</entry>
692 <entry/>
693 <entry/>
694 <entry/>
695 </row>
696 <row>
697 <entry>Unclean signal</entry>
698 <entry/>
699 <entry>X</entry>
700 <entry/>
701 <entry>X</entry>
702 <entry>X</entry>
703 <entry>X</entry>
704 <entry/>
705 </row>
706 <row>
707 <entry>Timeout</entry>
708 <entry/>
709 <entry>X</entry>
710 <entry/>
711 <entry>X</entry>
712 <entry>X</entry>
713 <entry/>
714 <entry/>
715 </row>
716 <row>
717 <entry>Watchdog</entry>
718 <entry/>
719 <entry>X</entry>
720 <entry/>
721 <entry>X</entry>
722 <entry>X</entry>
723 <entry/>
724 <entry>X</entry>
725 </row>
726 </tbody>
727 </tgroup>
728 </table>
729
b938cb90 730 <para>As exceptions to the setting above, the service will not
798d3a52 731 be restarted if the exit code or signal is specified in
09b69d68
TW
732 <varname>RestartPreventExitStatus=</varname> (see below) or
733 the service is stopped with <command>systemctl stop</command>
734 or an equivalent operation. Also, the services will always be
735 restarted if the exit code or signal is specified in
798d3a52
ZJS
736 <varname>RestartForceExitStatus=</varname> (see below).</para>
737
6d249476
LW
738 <para>Note that service restart is subject to unit start rate
739 limiting configured with <varname>StartLimitIntervalSec=</varname>
740 and <varname>StartLimitBurst=</varname>, see
741 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
bd2538b5
KBM
742 for details. A restarted service enters the failed state only
743 after the start limits are reached.</para>
6d249476 744
798d3a52
ZJS
745 <para>Setting this to <option>on-failure</option> is the
746 recommended choice for long-running services, in order to
747 increase reliability by attempting automatic recovery from
748 errors. For services that shall be able to terminate on their
749 own choice (and avoid immediate restarting),
750 <option>on-abnormal</option> is an alternative choice.</para>
751 </listitem>
752 </varlistentry>
753
754 <varlistentry>
755 <term><varname>SuccessExitStatus=</varname></term>
b938cb90
JE
756 <listitem><para>Takes a list of exit status definitions that,
757 when returned by the main service process, will be considered
798d3a52
ZJS
758 successful termination, in addition to the normal successful
759 exit code 0 and the signals <constant>SIGHUP</constant>,
760 <constant>SIGINT</constant>, <constant>SIGTERM</constant>, and
761 <constant>SIGPIPE</constant>. Exit status definitions can
762 either be numeric exit codes or termination signal names,
763 separated by spaces. For example:
dc83f27a
LP
764
765 <programlisting>SuccessExitStatus=1 2 8 SIGKILL</programlisting>
766
767 ensures that exit codes 1, 2, 8 and
798d3a52
ZJS
768 the termination signal <constant>SIGKILL</constant> are
769 considered clean service terminations.
770 </para>
771
798d3a52
ZJS
772 <para>This option may appear more than once, in which case the
773 list of successful exit statuses is merged. If the empty
774 string is assigned to this option, the list is reset, all
775 prior assignments of this option will have no
776 effect.</para></listitem>
777 </varlistentry>
778
779 <varlistentry>
780 <term><varname>RestartPreventExitStatus=</varname></term>
b938cb90
JE
781 <listitem><para>Takes a list of exit status definitions that,
782 when returned by the main service process, will prevent
798d3a52
ZJS
783 automatic service restarts, regardless of the restart setting
784 configured with <varname>Restart=</varname>. Exit status
785 definitions can either be numeric exit codes or termination
786 signal names, and are separated by spaces. Defaults to the
787 empty list, so that, by default, no exit status is excluded
788 from the configured restart logic. For example:
dc83f27a
LP
789
790 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting>
791
792 ensures that exit codes 1 and 6 and the termination signal
793 <constant>SIGABRT</constant> will not result in automatic
794 service restarting. This option may appear more than once, in
795 which case the list of restart-preventing statuses is
796 merged. If the empty string is assigned to this option, the
797 list is reset and all prior assignments of this option will
798 have no effect.</para></listitem>
798d3a52
ZJS
799 </varlistentry>
800
801 <varlistentry>
802 <term><varname>RestartForceExitStatus=</varname></term>
b938cb90
JE
803 <listitem><para>Takes a list of exit status definitions that,
804 when returned by the main service process, will force automatic
798d3a52
ZJS
805 service restarts, regardless of the restart setting configured
806 with <varname>Restart=</varname>. The argument format is
807 similar to
808 <varname>RestartPreventExitStatus=</varname>.</para></listitem>
809 </varlistentry>
810
811 <varlistentry>
812 <term><varname>PermissionsStartOnly=</varname></term>
813 <listitem><para>Takes a boolean argument. If true, the
814 permission-related execution options, as configured with
815 <varname>User=</varname> and similar options (see
816 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
817 for more information), are only applied to the process started
818 with
819 <varname>ExecStart=</varname>, and not to the various other
820 <varname>ExecStartPre=</varname>,
821 <varname>ExecStartPost=</varname>,
822 <varname>ExecReload=</varname>,
823 <varname>ExecStop=</varname>, and
824 <varname>ExecStopPost=</varname>
825 commands. If false, the setting is applied to all configured
826 commands the same way. Defaults to false.</para></listitem>
827 </varlistentry>
828
829 <varlistentry>
830 <term><varname>RootDirectoryStartOnly=</varname></term>
831 <listitem><para>Takes a boolean argument. If true, the root
832 directory, as configured with the
833 <varname>RootDirectory=</varname> option (see
834 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
835 for more information), is only applied to the process started
836 with <varname>ExecStart=</varname>, and not to the various
837 other <varname>ExecStartPre=</varname>,
838 <varname>ExecStartPost=</varname>,
839 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
840 and <varname>ExecStopPost=</varname> commands. If false, the
841 setting is applied to all configured commands the same way.
842 Defaults to false.</para></listitem>
843 </varlistentry>
844
845 <varlistentry>
846 <term><varname>NonBlocking=</varname></term>
9b141911
FB
847 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag for all file descriptors passed via socket-based
848 activation. If true, all file descriptors >= 3 (i.e. all except stdin, stdout, stderr), excluding those passed
849 in via the file descriptor storage logic (see <varname>FileDescriptorStoreMax=</varname> for details), will
850 have the <constant>O_NONBLOCK</constant> flag set and hence are in non-blocking mode. This option is only
851 useful in conjunction with a socket unit, as described in
852 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> and has no
853 effect on file descriptors which were previously saved in the file-descriptor store for example. Defaults to
854 false.</para></listitem>
798d3a52
ZJS
855 </varlistentry>
856
857 <varlistentry>
858 <term><varname>NotifyAccess=</varname></term>
b3bb6476
LP
859 <listitem><para>Controls access to the service status notification socket, as accessible via the
860 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> call. Takes one
861 of <option>none</option> (the default), <option>main</option>, <option>exec</option> or
862 <option>all</option>. If <option>none</option>, no daemon status updates are accepted from the service
863 processes, all status update messages are ignored. If <option>main</option>, only service updates sent from the
864 main process of the service are accepted. If <option>exec</option>, only service updates sent from any of the
865 main or control processes originating from one of the <varname>Exec*=</varname> commands are accepted. If
866 <option>all</option>, all services updates from all members of the service's control group are accepted. This
867 option should be set to open access to the notification socket when using <varname>Type=notify</varname> or
868 <varname>WatchdogSec=</varname> (see above). If those options are used but <varname>NotifyAccess=</varname> is
869 not configured, it will be implicitly set to <option>main</option>.</para>
870
871 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if
872 either the sending process is still around at the time PID 1 processes the message, or if the sending process
873 is explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally
874 forked off the process, i.e. on all processes that match <option>main</option> or
875 <option>exec</option>. Conversely, if an auxiliary process of the unit sends an
876 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to
877 properly attribute the message to the unit, and thus will ignore it, even if
878 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para></listitem>
798d3a52
ZJS
879 </varlistentry>
880
881 <varlistentry>
882 <term><varname>Sockets=</varname></term>
883 <listitem><para>Specifies the name of the socket units this
884 service shall inherit socket file descriptors from when the
b938cb90
JE
885 service is started. Normally, it should not be necessary to use
886 this setting, as all socket file descriptors whose unit shares
798d3a52
ZJS
887 the same name as the service (subject to the different unit
888 name suffix of course) are passed to the spawned
889 process.</para>
890
891 <para>Note that the same socket file descriptors may be passed
892 to multiple processes simultaneously. Also note that a
893 different service may be activated on incoming socket traffic
894 than the one which is ultimately configured to inherit the
b938cb90 895 socket file descriptors. Or, in other words: the
798d3a52
ZJS
896 <varname>Service=</varname> setting of
897 <filename>.socket</filename> units does not have to match the
898 inverse of the <varname>Sockets=</varname> setting of the
899 <filename>.service</filename> it refers to.</para>
900
901 <para>This option may appear more than once, in which case the
902 list of socket units is merged. If the empty string is
903 assigned to this option, the list of sockets is reset, and all
904 prior uses of this setting will have no
905 effect.</para></listitem>
906 </varlistentry>
907
798d3a52
ZJS
908 <varlistentry>
909 <term><varname>FileDescriptorStoreMax=</varname></term>
3ceb72e5 910 <listitem><para>Configure how many file descriptors may be stored in the service manager for the service using
798d3a52 911 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
3ceb72e5
LP
912 <literal>FDSTORE=1</literal> messages. This is useful for implementing services that can restart after an
913 explicit request or a crash without losing state. Any open sockets and other file descriptors which should not
914 be closed during the restart may be stored this way. Application state can either be serialized to a file in
915 <filename>/run</filename>, or better, stored in a
916 <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory file
917 descriptor. Defaults to 0, i.e. no file descriptors may be stored in the service manager. All file descriptors
918 passed to the service manager from a specific service are passed back to the service's main process on the next
919 service restart. Any file descriptors passed to the service manager are automatically closed when
920 <constant>POLLHUP</constant> or <constant>POLLERR</constant> is seen on them, or when the service is fully
921 stopped and no job is queued or being executed for it.</para></listitem>
798d3a52
ZJS
922 </varlistentry>
923
8c7c9839
PS
924 <varlistentry>
925 <term><varname>USBFunctionDescriptors=</varname></term>
3d314510
LP
926 <listitem><para>Configure the location of a file containing
927 <ulink
928 url="https://www.kernel.org/doc/Documentation/usb/functionfs.txt">USB
929 FunctionFS</ulink> descriptors, for implementation of USB
a8eaaee7 930 gadget functions. This is used only in conjunction with a
3d314510 931 socket unit with <varname>ListenUSBFunction=</varname>
a8eaaee7 932 configured. The contents of this file are written to the
3d314510
LP
933 <filename>ep0</filename> file after it is
934 opened.</para></listitem>
8c7c9839
PS
935 </varlistentry>
936
937 <varlistentry>
938 <term><varname>USBFunctionStrings=</varname></term>
3d314510
LP
939 <listitem><para>Configure the location of a file containing
940 USB FunctionFS strings. Behavior is similar to
941 <varname>USBFunctionDescriptors=</varname>
942 above.</para></listitem>
8c7c9839
PS
943 </varlistentry>
944
798d3a52
ZJS
945 </variablelist>
946
947 <para>Check
948 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
949 and
950 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
951 for more settings.</para>
952
953 </refsect1>
954
955 <refsect1>
956 <title>Command lines</title>
957
958 <para>This section describes command line parsing and
ff9b60f3 959 variable and specifier substitutions for
798d3a52
ZJS
960 <varname>ExecStart=</varname>,
961 <varname>ExecStartPre=</varname>,
962 <varname>ExecStartPost=</varname>,
963 <varname>ExecReload=</varname>,
964 <varname>ExecStop=</varname>, and
965 <varname>ExecStopPost=</varname> options.</para>
966
967 <para>Multiple command lines may be concatenated in a single
968 directive by separating them with semicolons (these semicolons
969 must be passed as separate words). Lone semicolons may be escaped
970 as <literal>\;</literal>.</para>
971
330785f5 972 <para>Each command line is split on whitespace, with the first item being the command to
1eecafb8 973 execute, and the subsequent items being the arguments. Double quotes ("…") and single quotes
fa0c9e63
ZJS
974 ('…') may be used to wrap a whole item (the opening quote may appear only at the beginning or
975 after whitespace that is not quoted, and the closing quote must be followed by whitespace or the
976 end of line), in which case everything until the next matching quote becomes part of the same
977 argument. Quotes themselves are removed. C-style escapes are also supported. The table below
978 contains the list of known escape patterns. Only escape patterns which match the syntax in the
979 table are allowed; other patterns may be added in the future and unknown patterns will result in
980 a warning. In particular, any backslashes should be doubled. Finally, a trailing backslash
981 (<literal>\</literal>) may be used to merge lines.</para>
798d3a52 982
0e3f51cf
ZJS
983 <para>This syntax is inspired by shell syntax, but only the meta-characters and expansions
984 described in the following paragraphs are understood, and the expansion of variables is
985 different. Specifically, redirection using
798d3a52
ZJS
986 <literal>&lt;</literal>,
987 <literal>&lt;&lt;</literal>,
988 <literal>&gt;</literal>, and
989 <literal>&gt;&gt;</literal>, pipes using
990 <literal>|</literal>, running programs in the background using
991 <literal>&amp;</literal>, and <emphasis>other elements of shell
992 syntax are not supported</emphasis>.</para>
993
388a91b0 994 <para>The command to execute must be an absolute path name. It may
798d3a52
ZJS
995 contain spaces, but control characters are not allowed.</para>
996
997 <para>The command line accepts <literal>%</literal> specifiers as
998 described in
2d06ddb7 999 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
798d3a52
ZJS
1000
1001 <para>Basic environment variable substitution is supported. Use
1002 <literal>${FOO}</literal> as part of a word, or as a word of its
1003 own, on the command line, in which case it will be replaced by the
1004 value of the environment variable including all whitespace it
1005 contains, resulting in a single argument. Use
1006 <literal>$FOO</literal> as a separate word on the command line, in
1007 which case it will be replaced by the value of the environment
b938cb90 1008 variable split at whitespace, resulting in zero or more arguments.
3faf145d 1009 For this type of expansion, quotes are respected when splitting
798d3a52
ZJS
1010 into words, and afterwards removed.</para>
1011
1012 <para>Example:</para>
1013
1014 <programlisting>Environment="ONE=one" 'TWO=two two'
5d9a2698
ZJS
1015ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
1016
798d3a52
ZJS
1017 <para>This will execute <command>/bin/echo</command> with four
1018 arguments: <literal>one</literal>, <literal>two</literal>,
1019 <literal>two</literal>, and <literal>two two</literal>.</para>
5d9a2698 1020
798d3a52
ZJS
1021 <para>Example:</para>
1022 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
5d9a2698
ZJS
1023ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1024ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
798d3a52
ZJS
1025 <para>This results in <filename>echo</filename> being
1026 called twice, the first time with arguments
1027 <literal>'one'</literal>,
1028 <literal>'two two' too</literal>, <literal></literal>,
1029 and the second time with arguments
1030 <literal>one</literal>, <literal>two two</literal>,
1031 <literal>too</literal>.
1032 </para>
1033
1034 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1035 Variables whose value is not known at expansion time are treated
1036 as empty strings. Note that the first argument (i.e. the program
1037 to execute) may not be a variable.</para>
1038
1039 <para>Variables to be used in this fashion may be defined through
1040 <varname>Environment=</varname> and
1041 <varname>EnvironmentFile=</varname>. In addition, variables listed
1042 in the section "Environment variables in spawned processes" in
1043 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1044 which are considered "static configuration", may be used (this
1045 includes e.g. <varname>$USER</varname>, but not
1046 <varname>$TERM</varname>).</para>
1047
1048 <para>Note that shell command lines are not directly supported. If
1049 shell command lines are to be used, they need to be passed
1050 explicitly to a shell implementation of some kind. Example:</para>
1051 <programlisting>ExecStart=/bin/sh -c 'dmesg | tac'</programlisting>
1052
1053 <para>Example:</para>
1054
1055 <programlisting>ExecStart=/bin/echo one ; /bin/echo "two two"</programlisting>
1056
1057 <para>This will execute <command>/bin/echo</command> two times,
1058 each time with one argument: <literal>one</literal> and
1059 <literal>two two</literal>, respectively. Because two commands are
1060 specified, <varname>Type=oneshot</varname> must be used.</para>
1061
1062 <para>Example:</para>
1063
1064 <programlisting>ExecStart=/bin/echo / &gt;/dev/null &amp; \; \
30d88d54
ZJS
1065/bin/ls</programlisting>
1066
798d3a52
ZJS
1067 <para>This will execute <command>/bin/echo</command>
1068 with five arguments: <literal>/</literal>,
1069 <literal>&gt;/dev/null</literal>,
1070 <literal>&amp;</literal>, <literal>;</literal>, and
1071 <literal>/bin/ls</literal>.</para>
1072
1073 <table>
1074 <title>C escapes supported in command lines and environment variables</title>
1075 <tgroup cols='2'>
1076 <colspec colname='escape' />
1077 <colspec colname='meaning' />
1078 <thead>
1079 <row>
1080 <entry>Literal</entry>
1081 <entry>Actual value</entry>
1082 </row>
1083 </thead>
1084 <tbody>
1085 <row>
1086 <entry><literal>\a</literal></entry>
1087 <entry>bell</entry>
1088 </row>
1089 <row>
1090 <entry><literal>\b</literal></entry>
1091 <entry>backspace</entry>
1092 </row>
1093 <row>
1094 <entry><literal>\f</literal></entry>
1095 <entry>form feed</entry>
1096 </row>
1097 <row>
1098 <entry><literal>\n</literal></entry>
1099 <entry>newline</entry>
1100 </row>
1101 <row>
1102 <entry><literal>\r</literal></entry>
1103 <entry>carriage return</entry>
1104 </row>
1105 <row>
1106 <entry><literal>\t</literal></entry>
1107 <entry>tab</entry>
1108 </row>
1109 <row>
1110 <entry><literal>\v</literal></entry>
1111 <entry>vertical tab</entry>
1112 </row>
1113 <row>
1114 <entry><literal>\\</literal></entry>
1115 <entry>backslash</entry>
1116 </row>
1117 <row>
1118 <entry><literal>\"</literal></entry>
1119 <entry>double quotation mark</entry>
1120 </row>
1121 <row>
1122 <entry><literal>\'</literal></entry>
1123 <entry>single quotation mark</entry>
1124 </row>
1125 <row>
1126 <entry><literal>\s</literal></entry>
1127 <entry>space</entry>
1128 </row>
1129 <row>
1130 <entry><literal>\x<replaceable>xx</replaceable></literal></entry>
1131 <entry>character number <replaceable>xx</replaceable> in hexadecimal encoding</entry>
1132 </row>
1133 <row>
1134 <entry><literal>\<replaceable>nnn</replaceable></literal></entry>
1135 <entry>character number <replaceable>nnn</replaceable> in octal encoding</entry>
1136 </row>
1137 </tbody>
1138 </tgroup>
1139 </table>
1140 </refsect1>
1141
1142 <refsect1>
1143 <title>Examples</title>
1144
1145 <example>
1146 <title>Simple service</title>
1147
1148 <para>The following unit file creates a service that will
1149 execute <filename>/usr/sbin/foo-daemon</filename>. Since no
1150 <varname>Type=</varname> is specified, the default
1151 <varname>Type=</varname><option>simple</option> will be assumed.
1152 systemd will assume the unit to be started immediately after the
1153 program has begun executing.</para>
1154
1155 <programlisting>[Unit]
d44efb62
CS
1156Description=Foo
1157
1158[Service]
1159ExecStart=/usr/sbin/foo-daemon
1160
1161[Install]
1162WantedBy=multi-user.target</programlisting>
1163
798d3a52
ZJS
1164 <para>Note that systemd assumes here that the process started by
1165 systemd will continue running until the service terminates. If
1166 the program daemonizes itself (i.e. forks), please use
1167 <varname>Type=</varname><option>forking</option> instead.</para>
1168
1169 <para>Since no <varname>ExecStop=</varname> was specified,
1170 systemd will send SIGTERM to all processes started from this
1171 service, and after a timeout also SIGKILL. This behavior can be
1172 modified, see
1173 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1174 for details.</para>
1175
1176 <para>Note that this unit type does not include any type of
1177 notification when a service has completed initialization. For
1178 this, you should use other unit types, such as
1179 <varname>Type=</varname><option>notify</option> if the service
1180 understands systemd's notification protocol,
1181 <varname>Type=</varname><option>forking</option> if the service
1182 can background itself or
1183 <varname>Type=</varname><option>dbus</option> if the unit
1184 acquires a DBus name once initialization is complete. See
1185 below.</para>
1186 </example>
1187
1188 <example>
1189 <title>Oneshot service</title>
1190
b938cb90 1191 <para>Sometimes, units should just execute an action without
798d3a52
ZJS
1192 keeping active processes, such as a filesystem check or a
1193 cleanup action on boot. For this,
1194 <varname>Type=</varname><option>oneshot</option> exists. Units
1195 of this type will wait until the process specified terminates
1196 and then fall back to being inactive. The following unit will
ff9b60f3 1197 perform a cleanup action:</para>
798d3a52
ZJS
1198
1199 <programlisting>[Unit]
d44efb62
CS
1200Description=Cleanup old Foo data
1201
1202[Service]
1203Type=oneshot
1204ExecStart=/usr/sbin/foo-cleanup
1205
1206[Install]
1207WantedBy=multi-user.target</programlisting>
1208
798d3a52 1209 <para>Note that systemd will consider the unit to be in the
b938cb90 1210 state "starting" until the program has terminated, so ordered
798d3a52 1211 dependencies will wait for the program to finish before starting
b938cb90
JE
1212 themselves. The unit will revert to the "inactive" state after
1213 the execution is done, never reaching the "active" state. That
798d3a52
ZJS
1214 means another request to start the unit will perform the action
1215 again.</para>
1216
1217 <para><varname>Type=</varname><option>oneshot</option> are the
1218 only service units that may have more than one
1219 <varname>ExecStart=</varname> specified. They will be executed
1220 in order until either they are all successful or one of them
1221 fails.</para>
1222 </example>
1223
1224 <example>
1225 <title>Stoppable oneshot service</title>
1226
1227 <para>Similarly to the oneshot services, there are sometimes
1228 units that need to execute a program to set up something and
1229 then execute another to shut it down, but no process remains
b938cb90 1230 active while they are considered "started". Network
798d3a52 1231 configuration can sometimes fall into this category. Another use
a8eaaee7 1232 case is if a oneshot service shall not be executed each time
798d3a52
ZJS
1233 when they are pulled in as a dependency, but only the first
1234 time.</para>
1235
1236 <para>For this, systemd knows the setting
1237 <varname>RemainAfterExit=</varname><option>yes</option>, which
1238 causes systemd to consider the unit to be active if the start
1239 action exited successfully. This directive can be used with all
1240 types, but is most useful with
1241 <varname>Type=</varname><option>oneshot</option> and
1242 <varname>Type=</varname><option>simple</option>. With
b938cb90 1243 <varname>Type=</varname><option>oneshot</option>, systemd waits
798d3a52
ZJS
1244 until the start action has completed before it considers the
1245 unit to be active, so dependencies start only after the start
1246 action has succeeded. With
b938cb90 1247 <varname>Type=</varname><option>simple</option>, dependencies
798d3a52
ZJS
1248 will start immediately after the start action has been
1249 dispatched. The following unit provides an example for a simple
1250 static firewall.</para>
1251
1252 <programlisting>[Unit]
d44efb62
CS
1253Description=Simple firewall
1254
1255[Service]
1256Type=oneshot
1257RemainAfterExit=yes
1258ExecStart=/usr/local/sbin/simple-firewall-start
1259ExecStop=/usr/local/sbin/simple-firewall-stop
1260
1261[Install]
1262WantedBy=multi-user.target</programlisting>
1263
798d3a52
ZJS
1264 <para>Since the unit is considered to be running after the start
1265 action has exited, invoking <command>systemctl start</command>
1266 on that unit again will cause no action to be taken.</para>
1267 </example>
1268
1269 <example>
1270 <title>Traditional forking services</title>
1271
1272 <para>Many traditional daemons/services background (i.e. fork,
1273 daemonize) themselves when starting. Set
1274 <varname>Type=</varname><option>forking</option> in the
1275 service's unit file to support this mode of operation. systemd
1276 will consider the service to be in the process of initialization
1277 while the original program is still running. Once it exits
1278 successfully and at least a process remains (and
1279 <varname>RemainAfterExit=</varname><option>no</option>), the
1280 service is considered started.</para>
1281
b938cb90 1282 <para>Often, a traditional daemon only consists of one process.
798d3a52
ZJS
1283 Therefore, if only one process is left after the original
1284 process terminates, systemd will consider that process the main
1285 process of the service. In that case, the
1286 <varname>$MAINPID</varname> variable will be available in
1287 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1288 etc.</para>
1289
1290 <para>In case more than one process remains, systemd will be
1291 unable to determine the main process, so it will not assume
1292 there is one. In that case, <varname>$MAINPID</varname> will not
1293 expand to anything. However, if the process decides to write a
1294 traditional PID file, systemd will be able to read the main PID
1295 from there. Please set <varname>PIDFile=</varname> accordingly.
1296 Note that the daemon should write that file before finishing
b938cb90 1297 with its initialization. Otherwise, systemd might try to read the
798d3a52
ZJS
1298 file before it exists.</para>
1299
1300 <para>The following example shows a simple daemon that forks and
1301 just starts one process in the background:</para>
1302
1303 <programlisting>[Unit]
d44efb62
CS
1304Description=Some simple daemon
1305
1306[Service]
1307Type=forking
1308ExecStart=/usr/sbin/my-simple-daemon -d
1309
1310[Install]
1311WantedBy=multi-user.target</programlisting>
1312
798d3a52
ZJS
1313 <para>Please see
1314 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1315 for details on how you can influence the way systemd terminates
1316 the service.</para>
1317 </example>
1318
1319 <example>
1320 <title>DBus services</title>
1321
1322 <para>For services that acquire a name on the DBus system bus,
1323 use <varname>Type=</varname><option>dbus</option> and set
1324 <varname>BusName=</varname> accordingly. The service should not
1325 fork (daemonize). systemd will consider the service to be
1326 initialized once the name has been acquired on the system bus.
1327 The following example shows a typical DBus service:</para>
1328
1329 <programlisting>[Unit]
d44efb62
CS
1330Description=Simple DBus service
1331
1332[Service]
1333Type=dbus
1334BusName=org.example.simple-dbus-service
1335ExecStart=/usr/sbin/simple-dbus-service
1336
1337[Install]
1338WantedBy=multi-user.target</programlisting>
1339
7ca41557 1340 <para>For <emphasis>bus-activatable</emphasis> services, do not
798d3a52
ZJS
1341 include a <literal>[Install]</literal> section in the systemd
1342 service file, but use the <varname>SystemdService=</varname>
1343 option in the corresponding DBus service file, for example
1344 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
d44efb62 1345
798d3a52 1346 <programlisting>[D-BUS Service]
d44efb62
CS
1347Name=org.example.simple-dbus-service
1348Exec=/usr/sbin/simple-dbus-service
1349User=root
1350SystemdService=simple-dbus-service.service</programlisting>
1351
798d3a52
ZJS
1352 <para>Please see
1353 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1354 for details on how you can influence the way systemd terminates
1355 the service.</para>
1356 </example>
1357
1358 <example>
1359 <title>Services that notify systemd about their initialization</title>
1360
1361 <para><varname>Type=</varname><option>simple</option> services
1362 are really easy to write, but have the major disadvantage of
1363 systemd not being able to tell when initialization of the given
1364 service is complete. For this reason, systemd supports a simple
1365 notification protocol that allows daemons to make systemd aware
1366 that they are done initializing. Use
1367 <varname>Type=</varname><option>notify</option> for this. A
1368 typical service file for such a daemon would look like
1369 this:</para>
1370
1371 <programlisting>[Unit]
d44efb62
CS
1372Description=Simple notifying service
1373
1374[Service]
1375Type=notify
1376ExecStart=/usr/sbin/simple-notifying-service
1377
1378[Install]
1379WantedBy=multi-user.target</programlisting>
1380
798d3a52 1381 <para>Note that the daemon has to support systemd's notification
7ca41557 1382 protocol, else systemd will think the service has not started yet
798d3a52
ZJS
1383 and kill it after a timeout. For an example of how to update
1384 daemons to support this protocol transparently, take a look at
1385 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1386 systemd will consider the unit to be in the 'starting' state
1387 until a readiness notification has arrived.</para>
1388
1389 <para>Please see
1390 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1391 for details on how you can influence the way systemd terminates
1392 the service.</para>
1393 </example>
1394 </refsect1>
1395
1396 <refsect1>
1397 <title>See Also</title>
1398 <para>
1399 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1400 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1401 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1402 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1403 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1404 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1405 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
1406 </para>
1407 </refsect1>
d1ab0ca0
LP
1408
1409</refentry>