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