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