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