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