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