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