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