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