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