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