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