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