]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.service.xml
Merge pull request #2587 from haraldh/tpmv3
[thirdparty/systemd.git] / man / systemd.service.xml
CommitLineData
c129bd5d 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
d1ab0ca0 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
12b42c76 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
d1ab0ca0
LP
4
5<!--
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
d1ab0ca0
LP
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 18 Lesser General Public License for more details.
d1ab0ca0 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
d1ab0ca0
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22-->
23
24<refentry id="systemd.service">
798d3a52
ZJS
25 <refentryinfo>
26 <title>systemd.service</title>
27 <productname>systemd</productname>
28
29 <authorgroup>
30 <author>
31 <contrib>Developer</contrib>
32 <firstname>Lennart</firstname>
33 <surname>Poettering</surname>
34 <email>lennart@poettering.net</email>
35 </author>
36 </authorgroup>
37 </refentryinfo>
38
39 <refmeta>
40 <refentrytitle>systemd.service</refentrytitle>
41 <manvolnum>5</manvolnum>
42 </refmeta>
43
44 <refnamediv>
45 <refname>systemd.service</refname>
46 <refpurpose>Service unit configuration</refpurpose>
47 </refnamediv>
48
49 <refsynopsisdiv>
50 <para><filename><replaceable>service</replaceable>.service</filename></para>
51 </refsynopsisdiv>
52
53 <refsect1>
54 <title>Description</title>
55
56 <para>A unit configuration file whose name ends in
57 <filename>.service</filename> encodes information about a process
58 controlled and supervised by systemd.</para>
59
60 <para>This man page lists the configuration options specific to
61 this unit type. See
62 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
63 for the common options of all unit configuration files. The common
64 configuration items are configured in the generic
65 <literal>[Unit]</literal> and <literal>[Install]</literal>
66 sections. The service specific configuration options are
67 configured in the <literal>[Service]</literal> section.</para>
68
69 <para>Additional options are listed in
70 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
71 which define the execution environment the commands are executed
72 in, and in
73 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
74 which define the way the processes of the service are terminated,
75 and in
76 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
77 which configure resource control settings for the processes of the
78 service.</para>
79
798d3a52
ZJS
80 <para>If a service is requested under a certain name but no unit
81 configuration file is found, systemd looks for a SysV init script
82 by the same name (with the <filename>.service</filename> suffix
83 removed) and dynamically creates a service unit from that script.
84 This is useful for compatibility with SysV. Note that this
85 compatibility is quite comprehensive but not 100%. For details
86 about the incompatibilities, see the <ulink
87 url="http://www.freedesktop.org/wiki/Software/systemd/Incompatibilities">Incompatibilities
c129bd5d
LP
88 with SysV</ulink> document.</para>
89 </refsect1>
90
91 <refsect1>
92 <title>Automatic Dependencies</title>
93
94 <para>Services with <varname>Type=dbus</varname> set automatically
95 acquire dependencies of type <varname>Requires=</varname> and
96 <varname>After=</varname> on
97 <filename>dbus.socket</filename>.</para>
98
99 <para>Socket activated service are automatically ordered after
100 their activated <filename>.socket</filename> units via an
101 automatic <varname>After=</varname> dependency.</para>
102
103 <para>Unless <varname>DefaultDependencies=</varname> is set to
104 <option>false</option>, service units will implicitly have
105 dependencies of type <varname>Requires=</varname> and
106 <varname>After=</varname> on <filename>sysinit.target</filename>,
107 a dependency of type <varname>After=</varname> on
108 <filename>basic.target</filename> as well as dependencies of
109 type <varname>Conflicts=</varname> and <varname>Before=</varname>
110 on <filename>shutdown.target</filename>. These ensure that normal
111 service units pull in basic system initialization, and are
112 terminated cleanly prior to system shutdown. Only services
113 involved with early boot or late system shutdown should disable
114 this option.</para>
115
ae0a5fb1
LP
116 <para>Instanced service units (i.e. service units with an <literal>@</literal> in their name) are assigned by
117 default a per-template slice unit (see
118 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>), named after the
119 template unit, containing all instances of the specific template. This slice is normally stopped at shutdown,
120 together with all template instances. If that is not desired, set <varname>DefaultDependencies=no</varname> in the
121 template unit, and either define your own per-template slice unit file that also sets
122 <varname>DefaultDependencies=no</varname>, or set <varname>Slice=system.slice</varname> (or another suitable slice)
123 in the template unit. Also see
124 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
125
c129bd5d
LP
126 <para>Additional implicit dependencies may be added as result of
127 execution and resource control parameters as documented in
128 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
129 and
130 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
798d3a52
ZJS
131 </refsect1>
132
133 <refsect1>
134 <title>Options</title>
135
136 <para>Service files must include a <literal>[Service]</literal>
137 section, which carries information about the service and the
138 process it supervises. A number of options that may be used in
139 this section are shared with other unit types. These options are
140 documented in
141 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
142 and
143 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
144 The options specific to the <literal>[Service]</literal> section
145 of service units are the following:</para>
146
147 <variablelist class='unit-directives'>
148 <varlistentry>
149 <term><varname>Type=</varname></term>
150
151 <listitem><para>Configures the process start-up type for this
152 service unit. One of
153 <option>simple</option>,
154 <option>forking</option>,
155 <option>oneshot</option>,
156 <option>dbus</option>,
157 <option>notify</option> or
158 <option>idle</option>.</para>
159
160 <para>If set to <option>simple</option> (the default if
161 neither <varname>Type=</varname> nor
162 <varname>BusName=</varname>, but <varname>ExecStart=</varname>
163 are specified), it is expected that the process configured
164 with <varname>ExecStart=</varname> is the main process of the
165 service. In this mode, if the process offers functionality to
166 other processes on the system, its communication channels
167 should be installed before the daemon is started up (e.g.
168 sockets set up by systemd, via socket activation), as systemd
169 will immediately proceed starting follow-up units.</para>
170
171 <para>If set to <option>forking</option>, it is expected that
172 the process configured with <varname>ExecStart=</varname> will
173 call <function>fork()</function> as part of its start-up. The
174 parent process is expected to exit when start-up is complete
175 and all communication channels are set up. The child continues
176 to run as the main daemon process. This is the behavior of
177 traditional UNIX daemons. If this setting is used, it is
178 recommended to also use the <varname>PIDFile=</varname>
179 option, so that systemd can identify the main process of the
180 daemon. systemd will proceed with starting follow-up units as
181 soon as the parent process exits.</para>
182
183 <para>Behavior of <option>oneshot</option> is similar to
184 <option>simple</option>; however, it is expected that the
185 process has to exit before systemd starts follow-up units.
186 <varname>RemainAfterExit=</varname> is particularly useful for
187 this type of service. This is the implied default if neither
188 <varname>Type=</varname> or <varname>ExecStart=</varname> are
189 specified.</para>
190
191 <para>Behavior of <option>dbus</option> is similar to
192 <option>simple</option>; however, it is expected that the
193 daemon acquires a name on the D-Bus bus, as configured by
194 <varname>BusName=</varname>. systemd will proceed with
195 starting follow-up units after the D-Bus bus name has been
196 acquired. Service units with this option configured implicitly
197 gain dependencies on the <filename>dbus.socket</filename>
198 unit. This type is the default if <varname>BusName=</varname>
199 is specified.</para>
200
201 <para>Behavior of <option>notify</option> is similar to
202 <option>simple</option>; however, it is expected that the
203 daemon sends a notification message via
204 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
205 or an equivalent call when it has finished starting up.
206 systemd will proceed with starting follow-up units after this
207 notification message has been sent. If this option is used,
208 <varname>NotifyAccess=</varname> (see below) should be set to
209 open access to the notification socket provided by systemd. If
210 <varname>NotifyAccess=</varname> is not set, it will be
211 implicitly set to <option>main</option>. Note that currently
212 <varname>Type=</varname><option>notify</option> will not work
213 if used in combination with
214 <varname>PrivateNetwork=</varname><option>yes</option>.</para>
215
216 <para>Behavior of <option>idle</option> is very similar to
217 <option>simple</option>; however, actual execution of the
218 service binary is delayed until all jobs are dispatched. This
219 may be used to avoid interleaving of output of shell services
220 with the status output on the console.</para>
221 </listitem>
222 </varlistentry>
223
224 <varlistentry>
225 <term><varname>RemainAfterExit=</varname></term>
226
227 <listitem><para>Takes a boolean value that specifies whether
228 the service shall be considered active even when all its
229 processes exited. Defaults to <option>no</option>.</para>
230 </listitem>
231 </varlistentry>
232
233 <varlistentry>
234 <term><varname>GuessMainPID=</varname></term>
235
236 <listitem><para>Takes a boolean value that specifies whether
237 systemd should try to guess the main PID of a service if it
238 cannot be determined reliably. This option is ignored unless
239 <option>Type=forking</option> is set and
240 <option>PIDFile=</option> is unset because for the other types
241 or with an explicitly configured PID file, the main PID is
242 always known. The guessing algorithm might come to incorrect
243 conclusions if a daemon consists of more than one process. If
244 the main PID cannot be determined, failure detection and
245 automatic restarting of a service will not work reliably.
246 Defaults to <option>yes</option>.</para>
247 </listitem>
248 </varlistentry>
249
250 <varlistentry>
251 <term><varname>PIDFile=</varname></term>
252
253 <listitem><para>Takes an absolute file name pointing to the
254 PID file of this daemon. Use of this option is recommended for
255 services where <varname>Type=</varname> is set to
256 <option>forking</option>. systemd will read the PID of the
257 main process of the daemon after start-up of the service.
341db20b
FS
258 systemd will not write to the file configured here, although
259 it will remove the file after the service has shut down if it
260 still exists.
261 </para>
798d3a52
ZJS
262 </listitem>
263 </varlistentry>
264
265 <varlistentry>
266 <term><varname>BusName=</varname></term>
267
268 <listitem><para>Takes a D-Bus bus name that this service is
269 reachable as. This option is mandatory for services where
270 <varname>Type=</varname> is set to
271 <option>dbus</option>.</para>
272 </listitem>
273 </varlistentry>
274
275 <varlistentry>
276 <term><varname>BusPolicy=</varname></term>
277
b8332e7a 278 <listitem><para>If specified, a custom kdbus
798d3a52
ZJS
279 endpoint will be created and installed as the default bus node
280 for the service. Such a custom endpoint can hold an own set of
281 policy rules that are enforced on top of the bus-wide ones.
282 The custom endpoint is named after the service it was created
283 for, and its node will be bind-mounted over the default bus
284 node location, so the service can only access the bus through
285 its own endpoint. Note that custom bus endpoints default to a
b938cb90 286 "deny all" policy. Hence, if at least one
798d3a52
ZJS
287 <varname>BusPolicy=</varname> directive is given, you have to
288 make sure to add explicit rules for everything the service
289 should be able to do.</para>
290 <para>The value of this directive is comprised
291 of two parts; the bus name, and a verb to
292 specify to granted access, which is one of
293 <option>see</option>,
294 <option>talk</option>, or
295 <option>own</option>.
296 <option>talk</option> implies
297 <option>see</option>, and <option>own</option>
298 implies both <option>talk</option> and
299 <option>see</option>.
300 If multiple access levels are specified for the
301 same bus name, the most powerful one takes
302 effect.
303 </para>
304 <para>Examples:</para>
305 <programlisting>BusPolicy=org.freedesktop.systemd1 talk</programlisting>
306 <programlisting>BusPolicy=org.foo.bar see</programlisting>
307 <para>This option is only available on kdbus enabled systems.</para>
308 </listitem>
309 </varlistentry>
310
311 <varlistentry>
312 <term><varname>ExecStart=</varname></term>
313 <listitem><para>Commands with their arguments that are
314 executed when this service is started. The value is split into
a8eaaee7 315 zero or more command lines according to the rules described
798d3a52
ZJS
316 below (see section "Command Lines" below).
317 </para>
318
22065311 319 <para>When <varname>Type=</varname> is not
798d3a52
ZJS
320 <option>oneshot</option>, only one command may and must be
321 given. When <varname>Type=oneshot</varname> is used, zero or
322 more commands may be specified. This can be specified by
323 providing multiple command lines in the same directive, or
324 alternatively, this directive may be specified more than once
325 with the same effect. If the empty string is assigned to this
326 option, the list of commands to start is reset, prior
327 assignments of this option will have no effect. If no
328 <varname>ExecStart=</varname> is specified, then the service
329 must have <varname>RemainAfterExit=yes</varname> set.</para>
330
331 <para>For each of the specified commands, the first argument
332 must be an absolute path to an executable. Optionally, if this
333 file name is prefixed with <literal>@</literal>, the second
334 token will be passed as <literal>argv[0]</literal> to the
335 executed process, followed by the further arguments specified.
336 If the absolute filename is prefixed with
337 <literal>-</literal>, an exit code of the command normally
338 considered a failure (i.e. non-zero exit status or abnormal
339 exit due to signal) is ignored and considered success. If both
340 <literal>-</literal> and <literal>@</literal> are used, they
341 can appear in either order.</para>
342
343 <para>If more than one command is specified, the commands are
344 invoked sequentially in the order they appear in the unit
345 file. If one of the commands fails (and is not prefixed with
346 <literal>-</literal>), other lines are not executed, and the
347 unit is considered failed.</para>
348
349 <para>Unless <varname>Type=forking</varname> is set, the
350 process started via this command line will be considered the
351 main process of the daemon.</para>
352 </listitem>
353 </varlistentry>
354
355 <varlistentry>
356 <term><varname>ExecStartPre=</varname></term>
357 <term><varname>ExecStartPost=</varname></term>
358 <listitem><para>Additional commands that are executed before
359 or after the command in <varname>ExecStart=</varname>,
360 respectively. Syntax is the same as for
361 <varname>ExecStart=</varname>, except that multiple command
362 lines are allowed and the commands are executed one after the
363 other, serially.</para>
364
365 <para>If any of those commands (not prefixed with
366 <literal>-</literal>) fail, the rest are not executed and the
367 unit is considered failed.</para>
b481de3b 368
12e2683d
RM
369 <para><varname>ExecStart=</varname> commands are only run after
370 all <varname>ExecStartPre=</varname> commands that were not prefixed
371 with a <literal>-</literal> exit successfully.</para>
372
373 <para><varname>ExecStartPost=</varname> commands are only run after
1917c8ca 374 the service has started successfully, as determined by <varname>Type=</varname>
a8eaaee7 375 (i.e. the process has been started for <varname>Type=simple</varname>
12e2683d
RM
376 or <varname>Type=idle</varname>, the process exits successfully for
377 <varname>Type=oneshot</varname>, the initial process exits successfully
378 for <varname>Type=forking</varname>, <literal>READY=1</literal> is sent
379 for <varname>Type=notify</varname>, or the <varname>BusName=</varname>
380 has been taken for <varname>Type=dbus</varname>).</para>
381
b481de3b
LP
382 <para>Note that <varname>ExecStartPre=</varname> may not be
383 used to start long-running processes. All processes forked
384 off by processes invoked via <varname>ExecStartPre=</varname> will
385 be killed before the next service process is run.</para>
ce359e98
LP
386
387 <para>Note that if any of the commands specified in <varname>ExecStartPre=</varname>,
388 <varname>ExecStart=</varname>, or <varname>ExecStartPost=</varname> fail (and are not prefixed with
389 <literal>-</literal>, see above) or time out before the service is fully up, execution continues with commands
390 specified in <varname>ExecStopPost=</varname>, the commands in <varname>ExecStop=</varname> are skipped.</para>
798d3a52
ZJS
391 </listitem>
392 </varlistentry>
393
394 <varlistentry>
395 <term><varname>ExecReload=</varname></term>
396 <listitem><para>Commands to execute to trigger a configuration
397 reload in the service. This argument takes multiple command
398 lines, following the same scheme as described for
399 <varname>ExecStart=</varname> above. Use of this setting is
400 optional. Specifier and environment variable substitution is
401 supported here following the same scheme as for
402 <varname>ExecStart=</varname>.</para>
403
404 <para>One additional, special environment variable is set: if
405 known, <varname>$MAINPID</varname> is set to the main process
406 of the daemon, and may be used for command lines like the
407 following:</para>
408
409 <programlisting>/bin/kill -HUP $MAINPID</programlisting>
410
411 <para>Note however that reloading a daemon by sending a signal
412 (as with the example line above) is usually not a good choice,
413 because this is an asynchronous operation and hence not
414 suitable to order reloads of multiple services against each
415 other. It is strongly recommended to set
416 <varname>ExecReload=</varname> to a command that not only
417 triggers a configuration reload of the daemon, but also
418 synchronously waits for it to complete.</para>
419 </listitem>
420 </varlistentry>
421
422 <varlistentry>
423 <term><varname>ExecStop=</varname></term>
424 <listitem><para>Commands to execute to stop the service
425 started via <varname>ExecStart=</varname>. This argument takes
426 multiple command lines, following the same scheme as described
427 for <varname>ExecStart=</varname> above. Use of this setting
428 is optional. After the commands configured in this option are
429 run, all processes remaining for a service are terminated
430 according to the <varname>KillMode=</varname> setting (see
431 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
cceb20c7
LP
432 If this option is not specified, the process is terminated by
433 sending the signal specified in <varname>KillSignal=</varname>
434 when service stop is requested. Specifier and environment
435 variable substitution is supported (including
436 <varname>$MAINPID</varname>, see above).</para>
437
438 <para>Note that it is usually not sufficient to specify a
439 command for this setting that only asks the service to
b938cb90 440 terminate (for example, by queuing some form of termination
cceb20c7
LP
441 signal for it), but does not wait for it to do so. Since the
442 remaining processes of the services are killed using
443 <constant>SIGKILL</constant> immediately after the command
b938cb90 444 exited, this would not result in a clean stop. The specified
cceb20c7 445 command should hence be a synchronous operation, not an
ce359e98
LP
446 asynchronous one.</para>
447
448 <para>Note that the commands specified in <varname>ExecStop=</varname> are only executed when the service
449 started successfuly first. They are not invoked if the service was never started at all, or in case its
450 start-up failed, for example because any of the commands specified in <varname>ExecStart=</varname>,
451 <varname>ExecStartPre=</varname> or <varname>ExecStartPost=</varname> failed (and weren't prefixed with
452 <literal>-</literal>, see above) or timed out. Use <varname>ExecStopPost=</varname> to invoke commands when a
453 service failed to start up correctly and is shut down again.</para>
454
455 <para>It is recommended to use this setting for commands that communicate with the service requesting clean
456 termination. When the commands specified with this option are executed it should be assumed that the service is
457 still fully up and is able to react correctly to all commands. For post-mortem clean-up steps use
458 <varname>ExecStopPost=</varname> instead.</para></listitem>
798d3a52
ZJS
459 </varlistentry>
460
461 <varlistentry>
462 <term><varname>ExecStopPost=</varname></term>
ce359e98
LP
463 <listitem><para>Additional commands that are executed after the service is stopped. This includes cases where
464 the commands configured in <varname>ExecStop=</varname> were used, where the service does not have any
465 <varname>ExecStop=</varname> defined, or where the service exited unexpectedly. This argument takes multiple
466 command lines, following the same scheme as described for <varname>ExecStart=</varname>. Use of these settings
467 is optional. Specifier and environment variable substitution is supported. Note that – unlike
468 <varname>ExecStop=</varname> – commands specified with this setting are invoked when a service failed to start
469 up correctly and is shut down again.</para>
470
471 <para>It is recommended to use this setting for clean-up operations that shall be executed even when the
472 service failed to start up correctly. Commands configured with this setting need to be able to operate even if
473 the service failed starting up half-way and left incompletely initialized data around. As the service's
474 processes have been terminated already when the commands specified with this setting are executed they should
475 not attempt to communicate with them.</para></listitem>
798d3a52
ZJS
476 </varlistentry>
477
478 <varlistentry>
479 <term><varname>RestartSec=</varname></term>
480 <listitem><para>Configures the time to sleep before restarting
481 a service (as configured with <varname>Restart=</varname>).
482 Takes a unit-less value in seconds, or a time span value such
483 as "5min 20s". Defaults to 100ms.</para></listitem>
484 </varlistentry>
485
486 <varlistentry>
487 <term><varname>TimeoutStartSec=</varname></term>
488 <listitem><para>Configures the time to wait for start-up. If a
489 daemon service does not signal start-up completion within the
490 configured time, the service will be considered failed and
491 will be shut down again. Takes a unit-less value in seconds,
492 or a time span value such as "5min 20s". Pass
2c29d332 493 <literal>infinity</literal> to disable the timeout logic. Defaults to
798d3a52
ZJS
494 <varname>DefaultTimeoutStartSec=</varname> from the manager
495 configuration file, except when
496 <varname>Type=oneshot</varname> is used, in which case the
497 timeout is disabled by default (see
498 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
499 </para></listitem>
500 </varlistentry>
501
502 <varlistentry>
503 <term><varname>TimeoutStopSec=</varname></term>
504 <listitem><para>Configures the time to wait for stop. If a
505 service is asked to stop, but does not terminate in the
506 specified time, it will be terminated forcibly via
507 <constant>SIGTERM</constant>, and after another timeout of
508 equal duration with <constant>SIGKILL</constant> (see
509 <varname>KillMode=</varname> in
510 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
511 Takes a unit-less value in seconds, or a time span value such
2c29d332 512 as "5min 20s". Pass <literal>infinity</literal> to disable the
798d3a52
ZJS
513 timeout logic. Defaults to
514 <varname>DefaultTimeoutStopSec=</varname> from the manager
515 configuration file (see
516 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
517 </para></listitem>
518 </varlistentry>
519
520 <varlistentry>
521 <term><varname>TimeoutSec=</varname></term>
522 <listitem><para>A shorthand for configuring both
523 <varname>TimeoutStartSec=</varname> and
524 <varname>TimeoutStopSec=</varname> to the specified value.
525 </para></listitem>
526 </varlistentry>
527
2c29d332
LP
528 <varlistentry>
529 <term><varname>RuntimeMaxSec=</varname></term>
530
531 <listitem><para>Configures a maximum time for the service to run. If this is used and the service has been
532 active for longer than the specified time it is terminated and put into a failure state. Note that this setting
533 does not have any effect on <varname>Type=oneshot</varname> services, as they terminate immediately after
534 activation completed. Pass <literal>infinity</literal> (the default) to configure no runtime
535 limit.</para></listitem>
536 </varlistentry>
537
798d3a52
ZJS
538 <varlistentry>
539 <term><varname>WatchdogSec=</varname></term>
540 <listitem><para>Configures the watchdog timeout for a service.
541 The watchdog is activated when the start-up is completed. The
542 service must call
543 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
544 regularly with <literal>WATCHDOG=1</literal> (i.e. the
545 "keep-alive ping"). If the time between two such calls is
546 larger than the configured time, then the service is placed in
547 a failed state and it will be terminated with
22065311 548 <constant>SIGABRT</constant>. By setting
a0533c6d
EV
549 <varname>Restart=</varname> to <option>on-failure</option>,
550 <option>on-watchdog</option>, <option>on-abnormal</option> or
798d3a52
ZJS
551 <option>always</option>, the service will be automatically
552 restarted. The time configured here will be passed to the
553 executed service process in the
554 <varname>WATCHDOG_USEC=</varname> environment variable. This
555 allows daemons to automatically enable the keep-alive pinging
556 logic if watchdog support is enabled for the service. If this
557 option is used, <varname>NotifyAccess=</varname> (see below)
558 should be set to open access to the notification socket
559 provided by systemd. If <varname>NotifyAccess=</varname> is
560 not set, it will be implicitly set to <option>main</option>.
582f2fcb
EV
561 Defaults to 0, which disables this feature. The service can
562 check whether the service manager expects watchdog keep-alive
563 notifications. See
564 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
a0533c6d
EV
565 for details.
566 <citerefentry><refentrytitle>sd_event_set_watchdog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
567 may be used to enable automatic watchdog notification support.
582f2fcb 568 </para></listitem>
798d3a52
ZJS
569 </varlistentry>
570
571 <varlistentry>
572 <term><varname>Restart=</varname></term>
573 <listitem><para>Configures whether the service shall be
574 restarted when the service process exits, is killed, or a
575 timeout is reached. The service process may be the main
576 service process, but it may also be one of the processes
577 specified with <varname>ExecStartPre=</varname>,
578 <varname>ExecStartPost=</varname>,
579 <varname>ExecStop=</varname>,
580 <varname>ExecStopPost=</varname>, or
581 <varname>ExecReload=</varname>. When the death of the process
582 is a result of systemd operation (e.g. service stop or
583 restart), the service will not be restarted. Timeouts include
584 missing the watchdog "keep-alive ping" deadline and a service
585 start, reload, and stop operation timeouts.</para>
586
587 <para>Takes one of
588 <option>no</option>,
589 <option>on-success</option>,
590 <option>on-failure</option>,
591 <option>on-abnormal</option>,
592 <option>on-watchdog</option>,
593 <option>on-abort</option>, or
594 <option>always</option>.
595 If set to <option>no</option> (the default), the service will
596 not be restarted. If set to <option>on-success</option>, it
597 will be restarted only when the service process exits cleanly.
598 In this context, a clean exit means an exit code of 0, or one
599 of the signals
600 <constant>SIGHUP</constant>,
601 <constant>SIGINT</constant>,
602 <constant>SIGTERM</constant> or
603 <constant>SIGPIPE</constant>, and
604 additionally, exit statuses and signals specified in
605 <varname>SuccessExitStatus=</varname>. If set to
606 <option>on-failure</option>, the service will be restarted
607 when the process exits with a non-zero exit code, is
608 terminated by a signal (including on core dump, but excluding
ff9b60f3 609 the aforementioned four signals), when an operation (such as
798d3a52
ZJS
610 service reload) times out, and when the configured watchdog
611 timeout is triggered. If set to <option>on-abnormal</option>,
612 the service will be restarted when the process is terminated
613 by a signal (including on core dump, excluding the
614 aforementioned four signals), when an operation times out, or
615 when the watchdog timeout is triggered. If set to
616 <option>on-abort</option>, the service will be restarted only
617 if the service process exits due to an uncaught signal not
618 specified as a clean exit status. If set to
619 <option>on-watchdog</option>, the service will be restarted
620 only if the watchdog timeout for the service expires. If set
621 to <option>always</option>, the service will be restarted
622 regardless of whether it exited cleanly or not, got terminated
623 abnormally by a signal, or hit a timeout.</para>
624
625 <table>
626 <title>Exit causes and the effect of the <varname>Restart=</varname> settings on them</title>
627
628 <tgroup cols='2'>
629 <colspec colname='path' />
630 <colspec colname='expl' />
631 <thead>
632 <row>
633 <entry>Restart settings/Exit causes</entry>
634 <entry><option>no</option></entry>
635 <entry><option>always</option></entry>
636 <entry><option>on-success</option></entry>
637 <entry><option>on-failure</option></entry>
638 <entry><option>on-abnormal</option></entry>
639 <entry><option>on-abort</option></entry>
640 <entry><option>on-watchdog</option></entry>
641 </row>
642 </thead>
643 <tbody>
644 <row>
645 <entry>Clean exit code or signal</entry>
646 <entry/>
647 <entry>X</entry>
648 <entry>X</entry>
649 <entry/>
650 <entry/>
651 <entry/>
652 <entry/>
653 </row>
654 <row>
655 <entry>Unclean exit code</entry>
656 <entry/>
657 <entry>X</entry>
658 <entry/>
659 <entry>X</entry>
660 <entry/>
661 <entry/>
662 <entry/>
663 </row>
664 <row>
665 <entry>Unclean signal</entry>
666 <entry/>
667 <entry>X</entry>
668 <entry/>
669 <entry>X</entry>
670 <entry>X</entry>
671 <entry>X</entry>
672 <entry/>
673 </row>
674 <row>
675 <entry>Timeout</entry>
676 <entry/>
677 <entry>X</entry>
678 <entry/>
679 <entry>X</entry>
680 <entry>X</entry>
681 <entry/>
682 <entry/>
683 </row>
684 <row>
685 <entry>Watchdog</entry>
686 <entry/>
687 <entry>X</entry>
688 <entry/>
689 <entry>X</entry>
690 <entry>X</entry>
691 <entry/>
692 <entry>X</entry>
693 </row>
694 </tbody>
695 </tgroup>
696 </table>
697
b938cb90 698 <para>As exceptions to the setting above, the service will not
798d3a52
ZJS
699 be restarted if the exit code or signal is specified in
700 <varname>RestartPreventExitStatus=</varname> (see below).
701 Also, the services will always be restarted if the exit code
702 or signal is specified in
703 <varname>RestartForceExitStatus=</varname> (see below).</para>
704
705 <para>Setting this to <option>on-failure</option> is the
706 recommended choice for long-running services, in order to
707 increase reliability by attempting automatic recovery from
708 errors. For services that shall be able to terminate on their
709 own choice (and avoid immediate restarting),
710 <option>on-abnormal</option> is an alternative choice.</para>
711 </listitem>
712 </varlistentry>
713
714 <varlistentry>
715 <term><varname>SuccessExitStatus=</varname></term>
b938cb90
JE
716 <listitem><para>Takes a list of exit status definitions that,
717 when returned by the main service process, will be considered
798d3a52
ZJS
718 successful termination, in addition to the normal successful
719 exit code 0 and the signals <constant>SIGHUP</constant>,
720 <constant>SIGINT</constant>, <constant>SIGTERM</constant>, and
721 <constant>SIGPIPE</constant>. Exit status definitions can
722 either be numeric exit codes or termination signal names,
723 separated by spaces. For example:
dc83f27a
LP
724
725 <programlisting>SuccessExitStatus=1 2 8 SIGKILL</programlisting>
726
727 ensures that exit codes 1, 2, 8 and
798d3a52
ZJS
728 the termination signal <constant>SIGKILL</constant> are
729 considered clean service terminations.
730 </para>
731
732 <para>Note that if a process has a signal handler installed
733 and exits by calling
734 <citerefentry><refentrytitle>_exit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
735 in response to a signal, the information about the signal is
736 lost. Programs should instead perform cleanup and kill
737 themselves with the same signal instead. See
738 <ulink url="http://www.cons.org/cracauer/sigint.html">Proper
739 handling of SIGINT/SIGQUIT — How to be a proper
740 program</ulink>.</para>
741
742 <para>This option may appear more than once, in which case the
743 list of successful exit statuses is merged. If the empty
744 string is assigned to this option, the list is reset, all
745 prior assignments of this option will have no
746 effect.</para></listitem>
747 </varlistentry>
748
749 <varlistentry>
750 <term><varname>RestartPreventExitStatus=</varname></term>
b938cb90
JE
751 <listitem><para>Takes a list of exit status definitions that,
752 when returned by the main service process, will prevent
798d3a52
ZJS
753 automatic service restarts, regardless of the restart setting
754 configured with <varname>Restart=</varname>. Exit status
755 definitions can either be numeric exit codes or termination
756 signal names, and are separated by spaces. Defaults to the
757 empty list, so that, by default, no exit status is excluded
758 from the configured restart logic. For example:
dc83f27a
LP
759
760 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting>
761
762 ensures that exit codes 1 and 6 and the termination signal
763 <constant>SIGABRT</constant> will not result in automatic
764 service restarting. This option may appear more than once, in
765 which case the list of restart-preventing statuses is
766 merged. If the empty string is assigned to this option, the
767 list is reset and all prior assignments of this option will
768 have no effect.</para></listitem>
798d3a52
ZJS
769 </varlistentry>
770
771 <varlistentry>
772 <term><varname>RestartForceExitStatus=</varname></term>
b938cb90
JE
773 <listitem><para>Takes a list of exit status definitions that,
774 when returned by the main service process, will force automatic
798d3a52
ZJS
775 service restarts, regardless of the restart setting configured
776 with <varname>Restart=</varname>. The argument format is
777 similar to
778 <varname>RestartPreventExitStatus=</varname>.</para></listitem>
779 </varlistentry>
780
781 <varlistentry>
782 <term><varname>PermissionsStartOnly=</varname></term>
783 <listitem><para>Takes a boolean argument. If true, the
784 permission-related execution options, as configured with
785 <varname>User=</varname> and similar options (see
786 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
787 for more information), are only applied to the process started
788 with
789 <varname>ExecStart=</varname>, and not to the various other
790 <varname>ExecStartPre=</varname>,
791 <varname>ExecStartPost=</varname>,
792 <varname>ExecReload=</varname>,
793 <varname>ExecStop=</varname>, and
794 <varname>ExecStopPost=</varname>
795 commands. If false, the setting is applied to all configured
796 commands the same way. Defaults to false.</para></listitem>
797 </varlistentry>
798
799 <varlistentry>
800 <term><varname>RootDirectoryStartOnly=</varname></term>
801 <listitem><para>Takes a boolean argument. If true, the root
802 directory, as configured with the
803 <varname>RootDirectory=</varname> option (see
804 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
805 for more information), is only applied to the process started
806 with <varname>ExecStart=</varname>, and not to the various
807 other <varname>ExecStartPre=</varname>,
808 <varname>ExecStartPost=</varname>,
809 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
810 and <varname>ExecStopPost=</varname> commands. If false, the
811 setting is applied to all configured commands the same way.
812 Defaults to false.</para></listitem>
813 </varlistentry>
814
815 <varlistentry>
816 <term><varname>NonBlocking=</varname></term>
817 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag
818 for all file descriptors passed via socket-based activation.
819 If true, all file descriptors >= 3 (i.e. all except stdin,
820 stdout, and stderr) will have the
821 <constant>O_NONBLOCK</constant> flag set and hence are in
822 non-blocking mode. This option is only useful in conjunction
823 with a socket unit, as described in
824 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
825 Defaults to false.</para></listitem>
826 </varlistentry>
827
828 <varlistentry>
829 <term><varname>NotifyAccess=</varname></term>
830 <listitem><para>Controls access to the service status
831 notification socket, as accessible via the
832 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
833 call. Takes one of <option>none</option> (the default),
834 <option>main</option> or <option>all</option>. If
835 <option>none</option>, no daemon status updates are accepted
836 from the service processes, all status update messages are
837 ignored. If <option>main</option>, only service updates sent
838 from the main process of the service are accepted. If
839 <option>all</option>, all services updates from all members of
840 the service's control group are accepted. This option should
841 be set to open access to the notification socket when using
842 <varname>Type=notify</varname> or
843 <varname>WatchdogSec=</varname> (see above). If those options
844 are used but <varname>NotifyAccess=</varname> is not
845 configured, it will be implicitly set to
846 <option>main</option>.</para></listitem>
847 </varlistentry>
848
849 <varlistentry>
850 <term><varname>Sockets=</varname></term>
851 <listitem><para>Specifies the name of the socket units this
852 service shall inherit socket file descriptors from when the
b938cb90
JE
853 service is started. Normally, it should not be necessary to use
854 this setting, as all socket file descriptors whose unit shares
798d3a52
ZJS
855 the same name as the service (subject to the different unit
856 name suffix of course) are passed to the spawned
857 process.</para>
858
859 <para>Note that the same socket file descriptors may be passed
860 to multiple processes simultaneously. Also note that a
861 different service may be activated on incoming socket traffic
862 than the one which is ultimately configured to inherit the
b938cb90 863 socket file descriptors. Or, in other words: the
798d3a52
ZJS
864 <varname>Service=</varname> setting of
865 <filename>.socket</filename> units does not have to match the
866 inverse of the <varname>Sockets=</varname> setting of the
867 <filename>.service</filename> it refers to.</para>
868
869 <para>This option may appear more than once, in which case the
870 list of socket units is merged. If the empty string is
871 assigned to this option, the list of sockets is reset, and all
872 prior uses of this setting will have no
873 effect.</para></listitem>
874 </varlistentry>
875
798d3a52
ZJS
876 <varlistentry>
877 <term><varname>FailureAction=</varname></term>
6bf0f408
LP
878 <listitem><para>Configure the action to take when the service enters a failed state. Takes the same values as
879 the unit setting <varname>StartLimitAction=</varname> and executes the same actions (see
880 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>). Defaults to
881 <option>none</option>. </para></listitem>
798d3a52
ZJS
882 </varlistentry>
883
884 <varlistentry>
885 <term><varname>FileDescriptorStoreMax=</varname></term>
886 <listitem><para>Configure how many file descriptors may be
887 stored in the service manager for the service using
888 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
889 <literal>FDSTORE=1</literal> messages. This is useful for
890 implementing service restart schemes where the state is
891 serialized to <filename>/run</filename> and the file
892 descriptors passed to the service manager, to allow restarts
893 without losing state. Defaults to 0, i.e. no file descriptors
894 may be stored in the service manager by default. All file
895 descriptors passed to the service manager from a specific
896 service are passed back to the service's main process on the
897 next service restart. Any file descriptors passed to the
898 service manager are automatically closed when POLLHUP or
899 POLLERR is seen on them, or when the service is fully stopped
900 and no job queued or being executed for it.</para></listitem>
901 </varlistentry>
902
8c7c9839
PS
903 <varlistentry>
904 <term><varname>USBFunctionDescriptors=</varname></term>
3d314510
LP
905 <listitem><para>Configure the location of a file containing
906 <ulink
907 url="https://www.kernel.org/doc/Documentation/usb/functionfs.txt">USB
908 FunctionFS</ulink> descriptors, for implementation of USB
a8eaaee7 909 gadget functions. This is used only in conjunction with a
3d314510 910 socket unit with <varname>ListenUSBFunction=</varname>
a8eaaee7 911 configured. The contents of this file are written to the
3d314510
LP
912 <filename>ep0</filename> file after it is
913 opened.</para></listitem>
8c7c9839
PS
914 </varlistentry>
915
916 <varlistentry>
917 <term><varname>USBFunctionStrings=</varname></term>
3d314510
LP
918 <listitem><para>Configure the location of a file containing
919 USB FunctionFS strings. Behavior is similar to
920 <varname>USBFunctionDescriptors=</varname>
921 above.</para></listitem>
8c7c9839
PS
922 </varlistentry>
923
798d3a52
ZJS
924 </variablelist>
925
926 <para>Check
927 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
928 and
929 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
930 for more settings.</para>
931
932 </refsect1>
933
934 <refsect1>
935 <title>Command lines</title>
936
937 <para>This section describes command line parsing and
ff9b60f3 938 variable and specifier substitutions for
798d3a52
ZJS
939 <varname>ExecStart=</varname>,
940 <varname>ExecStartPre=</varname>,
941 <varname>ExecStartPost=</varname>,
942 <varname>ExecReload=</varname>,
943 <varname>ExecStop=</varname>, and
944 <varname>ExecStopPost=</varname> options.</para>
945
946 <para>Multiple command lines may be concatenated in a single
947 directive by separating them with semicolons (these semicolons
948 must be passed as separate words). Lone semicolons may be escaped
949 as <literal>\;</literal>.</para>
950
951 <para>Each command line is split on whitespace, with the first
952 item being the command to execute, and the subsequent items being
953 the arguments. Double quotes ("...") and single quotes ('...') may
954 be used, in which case everything until the next matching quote
955 becomes part of the same argument. C-style escapes are also
388a91b0
ZJS
956 supported. The table below contains the list of allowed escape
957 patterns. Only patterns which match the syntax in the table are
958 allowed; others will result in an error, and must be escaped by
959 doubling the backslash. Quotes themselves are removed after
798d3a52
ZJS
960 parsing and escape sequences substituted. In addition, a trailing
961 backslash (<literal>\</literal>) may be used to merge lines.
962 </para>
963
964 <para>This syntax is intended to be very similar to shell syntax,
965 but only the meta-characters and expansions described in the
966 following paragraphs are understood. Specifically, redirection
967 using
968 <literal>&lt;</literal>,
969 <literal>&lt;&lt;</literal>,
970 <literal>&gt;</literal>, and
971 <literal>&gt;&gt;</literal>, pipes using
972 <literal>|</literal>, running programs in the background using
973 <literal>&amp;</literal>, and <emphasis>other elements of shell
974 syntax are not supported</emphasis>.</para>
975
388a91b0 976 <para>The command to execute must be an absolute path name. It may
798d3a52
ZJS
977 contain spaces, but control characters are not allowed.</para>
978
979 <para>The command line accepts <literal>%</literal> specifiers as
980 described in
981 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
982 Note that the first argument of the command line (i.e. the program
983 to execute) may not include specifiers.</para>
984
985 <para>Basic environment variable substitution is supported. Use
986 <literal>${FOO}</literal> as part of a word, or as a word of its
987 own, on the command line, in which case it will be replaced by the
988 value of the environment variable including all whitespace it
989 contains, resulting in a single argument. Use
990 <literal>$FOO</literal> as a separate word on the command line, in
991 which case it will be replaced by the value of the environment
b938cb90 992 variable split at whitespace, resulting in zero or more arguments.
3faf145d 993 For this type of expansion, quotes are respected when splitting
798d3a52
ZJS
994 into words, and afterwards removed.</para>
995
996 <para>Example:</para>
997
998 <programlisting>Environment="ONE=one" 'TWO=two two'
5d9a2698
ZJS
999ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
1000
798d3a52
ZJS
1001 <para>This will execute <command>/bin/echo</command> with four
1002 arguments: <literal>one</literal>, <literal>two</literal>,
1003 <literal>two</literal>, and <literal>two two</literal>.</para>
5d9a2698 1004
798d3a52
ZJS
1005 <para>Example:</para>
1006 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
5d9a2698
ZJS
1007ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1008ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
798d3a52
ZJS
1009 <para>This results in <filename>echo</filename> being
1010 called twice, the first time with arguments
1011 <literal>'one'</literal>,
1012 <literal>'two two' too</literal>, <literal></literal>,
1013 and the second time with arguments
1014 <literal>one</literal>, <literal>two two</literal>,
1015 <literal>too</literal>.
1016 </para>
1017
1018 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1019 Variables whose value is not known at expansion time are treated
1020 as empty strings. Note that the first argument (i.e. the program
1021 to execute) may not be a variable.</para>
1022
1023 <para>Variables to be used in this fashion may be defined through
1024 <varname>Environment=</varname> and
1025 <varname>EnvironmentFile=</varname>. In addition, variables listed
1026 in the section "Environment variables in spawned processes" in
1027 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1028 which are considered "static configuration", may be used (this
1029 includes e.g. <varname>$USER</varname>, but not
1030 <varname>$TERM</varname>).</para>
1031
1032 <para>Note that shell command lines are not directly supported. If
1033 shell command lines are to be used, they need to be passed
1034 explicitly to a shell implementation of some kind. Example:</para>
1035 <programlisting>ExecStart=/bin/sh -c 'dmesg | tac'</programlisting>
1036
1037 <para>Example:</para>
1038
1039 <programlisting>ExecStart=/bin/echo one ; /bin/echo "two two"</programlisting>
1040
1041 <para>This will execute <command>/bin/echo</command> two times,
1042 each time with one argument: <literal>one</literal> and
1043 <literal>two two</literal>, respectively. Because two commands are
1044 specified, <varname>Type=oneshot</varname> must be used.</para>
1045
1046 <para>Example:</para>
1047
1048 <programlisting>ExecStart=/bin/echo / &gt;/dev/null &amp; \; \
30d88d54
ZJS
1049/bin/ls</programlisting>
1050
798d3a52
ZJS
1051 <para>This will execute <command>/bin/echo</command>
1052 with five arguments: <literal>/</literal>,
1053 <literal>&gt;/dev/null</literal>,
1054 <literal>&amp;</literal>, <literal>;</literal>, and
1055 <literal>/bin/ls</literal>.</para>
1056
1057 <table>
1058 <title>C escapes supported in command lines and environment variables</title>
1059 <tgroup cols='2'>
1060 <colspec colname='escape' />
1061 <colspec colname='meaning' />
1062 <thead>
1063 <row>
1064 <entry>Literal</entry>
1065 <entry>Actual value</entry>
1066 </row>
1067 </thead>
1068 <tbody>
1069 <row>
1070 <entry><literal>\a</literal></entry>
1071 <entry>bell</entry>
1072 </row>
1073 <row>
1074 <entry><literal>\b</literal></entry>
1075 <entry>backspace</entry>
1076 </row>
1077 <row>
1078 <entry><literal>\f</literal></entry>
1079 <entry>form feed</entry>
1080 </row>
1081 <row>
1082 <entry><literal>\n</literal></entry>
1083 <entry>newline</entry>
1084 </row>
1085 <row>
1086 <entry><literal>\r</literal></entry>
1087 <entry>carriage return</entry>
1088 </row>
1089 <row>
1090 <entry><literal>\t</literal></entry>
1091 <entry>tab</entry>
1092 </row>
1093 <row>
1094 <entry><literal>\v</literal></entry>
1095 <entry>vertical tab</entry>
1096 </row>
1097 <row>
1098 <entry><literal>\\</literal></entry>
1099 <entry>backslash</entry>
1100 </row>
1101 <row>
1102 <entry><literal>\"</literal></entry>
1103 <entry>double quotation mark</entry>
1104 </row>
1105 <row>
1106 <entry><literal>\'</literal></entry>
1107 <entry>single quotation mark</entry>
1108 </row>
1109 <row>
1110 <entry><literal>\s</literal></entry>
1111 <entry>space</entry>
1112 </row>
1113 <row>
1114 <entry><literal>\x<replaceable>xx</replaceable></literal></entry>
1115 <entry>character number <replaceable>xx</replaceable> in hexadecimal encoding</entry>
1116 </row>
1117 <row>
1118 <entry><literal>\<replaceable>nnn</replaceable></literal></entry>
1119 <entry>character number <replaceable>nnn</replaceable> in octal encoding</entry>
1120 </row>
1121 </tbody>
1122 </tgroup>
1123 </table>
1124 </refsect1>
1125
1126 <refsect1>
1127 <title>Examples</title>
1128
1129 <example>
1130 <title>Simple service</title>
1131
1132 <para>The following unit file creates a service that will
1133 execute <filename>/usr/sbin/foo-daemon</filename>. Since no
1134 <varname>Type=</varname> is specified, the default
1135 <varname>Type=</varname><option>simple</option> will be assumed.
1136 systemd will assume the unit to be started immediately after the
1137 program has begun executing.</para>
1138
1139 <programlisting>[Unit]
d44efb62
CS
1140Description=Foo
1141
1142[Service]
1143ExecStart=/usr/sbin/foo-daemon
1144
1145[Install]
1146WantedBy=multi-user.target</programlisting>
1147
798d3a52
ZJS
1148 <para>Note that systemd assumes here that the process started by
1149 systemd will continue running until the service terminates. If
1150 the program daemonizes itself (i.e. forks), please use
1151 <varname>Type=</varname><option>forking</option> instead.</para>
1152
1153 <para>Since no <varname>ExecStop=</varname> was specified,
1154 systemd will send SIGTERM to all processes started from this
1155 service, and after a timeout also SIGKILL. This behavior can be
1156 modified, see
1157 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1158 for details.</para>
1159
1160 <para>Note that this unit type does not include any type of
1161 notification when a service has completed initialization. For
1162 this, you should use other unit types, such as
1163 <varname>Type=</varname><option>notify</option> if the service
1164 understands systemd's notification protocol,
1165 <varname>Type=</varname><option>forking</option> if the service
1166 can background itself or
1167 <varname>Type=</varname><option>dbus</option> if the unit
1168 acquires a DBus name once initialization is complete. See
1169 below.</para>
1170 </example>
1171
1172 <example>
1173 <title>Oneshot service</title>
1174
b938cb90 1175 <para>Sometimes, units should just execute an action without
798d3a52
ZJS
1176 keeping active processes, such as a filesystem check or a
1177 cleanup action on boot. For this,
1178 <varname>Type=</varname><option>oneshot</option> exists. Units
1179 of this type will wait until the process specified terminates
1180 and then fall back to being inactive. The following unit will
ff9b60f3 1181 perform a cleanup action:</para>
798d3a52
ZJS
1182
1183 <programlisting>[Unit]
d44efb62
CS
1184Description=Cleanup old Foo data
1185
1186[Service]
1187Type=oneshot
1188ExecStart=/usr/sbin/foo-cleanup
1189
1190[Install]
1191WantedBy=multi-user.target</programlisting>
1192
798d3a52 1193 <para>Note that systemd will consider the unit to be in the
b938cb90 1194 state "starting" until the program has terminated, so ordered
798d3a52 1195 dependencies will wait for the program to finish before starting
b938cb90
JE
1196 themselves. The unit will revert to the "inactive" state after
1197 the execution is done, never reaching the "active" state. That
798d3a52
ZJS
1198 means another request to start the unit will perform the action
1199 again.</para>
1200
1201 <para><varname>Type=</varname><option>oneshot</option> are the
1202 only service units that may have more than one
1203 <varname>ExecStart=</varname> specified. They will be executed
1204 in order until either they are all successful or one of them
1205 fails.</para>
1206 </example>
1207
1208 <example>
1209 <title>Stoppable oneshot service</title>
1210
1211 <para>Similarly to the oneshot services, there are sometimes
1212 units that need to execute a program to set up something and
1213 then execute another to shut it down, but no process remains
b938cb90 1214 active while they are considered "started". Network
798d3a52 1215 configuration can sometimes fall into this category. Another use
a8eaaee7 1216 case is if a oneshot service shall not be executed each time
798d3a52
ZJS
1217 when they are pulled in as a dependency, but only the first
1218 time.</para>
1219
1220 <para>For this, systemd knows the setting
1221 <varname>RemainAfterExit=</varname><option>yes</option>, which
1222 causes systemd to consider the unit to be active if the start
1223 action exited successfully. This directive can be used with all
1224 types, but is most useful with
1225 <varname>Type=</varname><option>oneshot</option> and
1226 <varname>Type=</varname><option>simple</option>. With
b938cb90 1227 <varname>Type=</varname><option>oneshot</option>, systemd waits
798d3a52
ZJS
1228 until the start action has completed before it considers the
1229 unit to be active, so dependencies start only after the start
1230 action has succeeded. With
b938cb90 1231 <varname>Type=</varname><option>simple</option>, dependencies
798d3a52
ZJS
1232 will start immediately after the start action has been
1233 dispatched. The following unit provides an example for a simple
1234 static firewall.</para>
1235
1236 <programlisting>[Unit]
d44efb62
CS
1237Description=Simple firewall
1238
1239[Service]
1240Type=oneshot
1241RemainAfterExit=yes
1242ExecStart=/usr/local/sbin/simple-firewall-start
1243ExecStop=/usr/local/sbin/simple-firewall-stop
1244
1245[Install]
1246WantedBy=multi-user.target</programlisting>
1247
798d3a52
ZJS
1248 <para>Since the unit is considered to be running after the start
1249 action has exited, invoking <command>systemctl start</command>
1250 on that unit again will cause no action to be taken.</para>
1251 </example>
1252
1253 <example>
1254 <title>Traditional forking services</title>
1255
1256 <para>Many traditional daemons/services background (i.e. fork,
1257 daemonize) themselves when starting. Set
1258 <varname>Type=</varname><option>forking</option> in the
1259 service's unit file to support this mode of operation. systemd
1260 will consider the service to be in the process of initialization
1261 while the original program is still running. Once it exits
1262 successfully and at least a process remains (and
1263 <varname>RemainAfterExit=</varname><option>no</option>), the
1264 service is considered started.</para>
1265
b938cb90 1266 <para>Often, a traditional daemon only consists of one process.
798d3a52
ZJS
1267 Therefore, if only one process is left after the original
1268 process terminates, systemd will consider that process the main
1269 process of the service. In that case, the
1270 <varname>$MAINPID</varname> variable will be available in
1271 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1272 etc.</para>
1273
1274 <para>In case more than one process remains, systemd will be
1275 unable to determine the main process, so it will not assume
1276 there is one. In that case, <varname>$MAINPID</varname> will not
1277 expand to anything. However, if the process decides to write a
1278 traditional PID file, systemd will be able to read the main PID
1279 from there. Please set <varname>PIDFile=</varname> accordingly.
1280 Note that the daemon should write that file before finishing
b938cb90 1281 with its initialization. Otherwise, systemd might try to read the
798d3a52
ZJS
1282 file before it exists.</para>
1283
1284 <para>The following example shows a simple daemon that forks and
1285 just starts one process in the background:</para>
1286
1287 <programlisting>[Unit]
d44efb62
CS
1288Description=Some simple daemon
1289
1290[Service]
1291Type=forking
1292ExecStart=/usr/sbin/my-simple-daemon -d
1293
1294[Install]
1295WantedBy=multi-user.target</programlisting>
1296
798d3a52
ZJS
1297 <para>Please see
1298 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1299 for details on how you can influence the way systemd terminates
1300 the service.</para>
1301 </example>
1302
1303 <example>
1304 <title>DBus services</title>
1305
1306 <para>For services that acquire a name on the DBus system bus,
1307 use <varname>Type=</varname><option>dbus</option> and set
1308 <varname>BusName=</varname> accordingly. The service should not
1309 fork (daemonize). systemd will consider the service to be
1310 initialized once the name has been acquired on the system bus.
1311 The following example shows a typical DBus service:</para>
1312
1313 <programlisting>[Unit]
d44efb62
CS
1314Description=Simple DBus service
1315
1316[Service]
1317Type=dbus
1318BusName=org.example.simple-dbus-service
1319ExecStart=/usr/sbin/simple-dbus-service
1320
1321[Install]
1322WantedBy=multi-user.target</programlisting>
1323
7ca41557 1324 <para>For <emphasis>bus-activatable</emphasis> services, do not
798d3a52
ZJS
1325 include a <literal>[Install]</literal> section in the systemd
1326 service file, but use the <varname>SystemdService=</varname>
1327 option in the corresponding DBus service file, for example
1328 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
d44efb62 1329
798d3a52 1330 <programlisting>[D-BUS Service]
d44efb62
CS
1331Name=org.example.simple-dbus-service
1332Exec=/usr/sbin/simple-dbus-service
1333User=root
1334SystemdService=simple-dbus-service.service</programlisting>
1335
798d3a52
ZJS
1336 <para>Please see
1337 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1338 for details on how you can influence the way systemd terminates
1339 the service.</para>
1340 </example>
1341
1342 <example>
1343 <title>Services that notify systemd about their initialization</title>
1344
1345 <para><varname>Type=</varname><option>simple</option> services
1346 are really easy to write, but have the major disadvantage of
1347 systemd not being able to tell when initialization of the given
1348 service is complete. For this reason, systemd supports a simple
1349 notification protocol that allows daemons to make systemd aware
1350 that they are done initializing. Use
1351 <varname>Type=</varname><option>notify</option> for this. A
1352 typical service file for such a daemon would look like
1353 this:</para>
1354
1355 <programlisting>[Unit]
d44efb62
CS
1356Description=Simple notifying service
1357
1358[Service]
1359Type=notify
1360ExecStart=/usr/sbin/simple-notifying-service
1361
1362[Install]
1363WantedBy=multi-user.target</programlisting>
1364
798d3a52 1365 <para>Note that the daemon has to support systemd's notification
7ca41557 1366 protocol, else systemd will think the service has not started yet
798d3a52
ZJS
1367 and kill it after a timeout. For an example of how to update
1368 daemons to support this protocol transparently, take a look at
1369 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1370 systemd will consider the unit to be in the 'starting' state
1371 until a readiness notification has arrived.</para>
1372
1373 <para>Please see
1374 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1375 for details on how you can influence the way systemd terminates
1376 the service.</para>
1377 </example>
1378 </refsect1>
1379
1380 <refsect1>
1381 <title>See Also</title>
1382 <para>
1383 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1384 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1385 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1386 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1387 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1388 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1389 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
1390 </para>
1391 </refsect1>
d1ab0ca0
LP
1392
1393</refentry>