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