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