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