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