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