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