]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.service.xml
Merge pull request #2561 from msekletar/virtio-blk-by-path
[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
374 the service has started, 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
876 <varlistentry>
877 <term><varname>StartLimitInterval=</varname></term>
878 <term><varname>StartLimitBurst=</varname></term>
879
880 <listitem><para>Configure service start rate limiting. By
881 default, services which are started more than 5 times within
882 10 seconds are not permitted to start any more times until the
883 10 second interval ends. With these two options, this rate
884 limiting may be modified. Use
885 <varname>StartLimitInterval=</varname> to configure the
886 checking interval (defaults to
887 <varname>DefaultStartLimitInterval=</varname> in manager
888 configuration file, set to 0 to disable any kind of rate
889 limiting). Use <varname>StartLimitBurst=</varname> to
890 configure how many starts per interval are allowed (defaults
891 to <varname>DefaultStartLimitBurst=</varname> in manager
892 configuration file). These configuration options are
893 particularly useful in conjunction with
894 <varname>Restart=</varname>; however, they apply to all kinds
895 of starts (including manual), not just those triggered by the
896 <varname>Restart=</varname> logic. Note that units which are
897 configured for <varname>Restart=</varname> and which reach the
898 start limit are not attempted to be restarted anymore;
899 however, they may still be restarted manually at a later
900 point, from which point on, the restart logic is again
901 activated. Note that <command>systemctl reset-failed</command>
902 will cause the restart rate counter for a service to be
903 flushed, which is useful if the administrator wants to
904 manually start a service and the start limit interferes with
905 that.</para></listitem>
906 </varlistentry>
907
908 <varlistentry>
909 <term><varname>StartLimitAction=</varname></term>
910
911 <listitem><para>Configure the action to take if the rate limit
912 configured with <varname>StartLimitInterval=</varname> and
913 <varname>StartLimitBurst=</varname> is hit. Takes one of
914 <option>none</option>,
915 <option>reboot</option>,
916 <option>reboot-force</option>,
917 <option>reboot-immediate</option>,
918 <option>poweroff</option>,
919 <option>poweroff-force</option> or
920 <option>poweroff-immediate</option>. If
921 <option>none</option> is set, hitting the rate limit will
922 trigger no action besides that the start will not be
923 permitted. <option>reboot</option> causes a reboot following
924 the normal shutdown procedure (i.e. equivalent to
925 <command>systemctl reboot</command>).
926 <option>reboot-force</option> causes a forced reboot which
927 will terminate all processes forcibly but should cause no
928 dirty file systems on reboot (i.e. equivalent to
929 <command>systemctl reboot -f</command>) and
930 <option>reboot-immediate</option> causes immediate execution
931 of the
932 <citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
a8eaaee7 933 system call, which might result in data loss. Similarly,
798d3a52
ZJS
934 <option>poweroff</option>, <option>poweroff-force</option>,
935 <option>poweroff-immediate</option> have the effect of
936 powering down the system with similar semantics. Defaults to
937 <option>none</option>.</para></listitem>
938 </varlistentry>
939
940 <varlistentry>
941 <term><varname>FailureAction=</varname></term>
942 <listitem><para>Configure the action to take when the service
943 enters a failed state. Takes the same values as
944 <varname>StartLimitAction=</varname> and executes the same
945 actions. Defaults to <option>none</option>. </para></listitem>
946 </varlistentry>
947
948 <varlistentry>
949 <term><varname>RebootArgument=</varname></term>
950 <listitem><para>Configure the optional argument for the
951 <citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
952 system call if <varname>StartLimitAction=</varname> or
953 <varname>FailureAction=</varname> is a reboot action. This
954 works just like the optional argument to <command>systemctl
955 reboot</command> command.</para></listitem>
956 </varlistentry>
957
958 <varlistentry>
959 <term><varname>FileDescriptorStoreMax=</varname></term>
960 <listitem><para>Configure how many file descriptors may be
961 stored in the service manager for the service using
962 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
963 <literal>FDSTORE=1</literal> messages. This is useful for
964 implementing service restart schemes where the state is
965 serialized to <filename>/run</filename> and the file
966 descriptors passed to the service manager, to allow restarts
967 without losing state. Defaults to 0, i.e. no file descriptors
968 may be stored in the service manager by default. All file
969 descriptors passed to the service manager from a specific
970 service are passed back to the service's main process on the
971 next service restart. Any file descriptors passed to the
972 service manager are automatically closed when POLLHUP or
973 POLLERR is seen on them, or when the service is fully stopped
974 and no job queued or being executed for it.</para></listitem>
975 </varlistentry>
976
8c7c9839
PS
977 <varlistentry>
978 <term><varname>USBFunctionDescriptors=</varname></term>
3d314510
LP
979 <listitem><para>Configure the location of a file containing
980 <ulink
981 url="https://www.kernel.org/doc/Documentation/usb/functionfs.txt">USB
982 FunctionFS</ulink> descriptors, for implementation of USB
a8eaaee7 983 gadget functions. This is used only in conjunction with a
3d314510 984 socket unit with <varname>ListenUSBFunction=</varname>
a8eaaee7 985 configured. The contents of this file are written to the
3d314510
LP
986 <filename>ep0</filename> file after it is
987 opened.</para></listitem>
8c7c9839
PS
988 </varlistentry>
989
990 <varlistentry>
991 <term><varname>USBFunctionStrings=</varname></term>
3d314510
LP
992 <listitem><para>Configure the location of a file containing
993 USB FunctionFS strings. Behavior is similar to
994 <varname>USBFunctionDescriptors=</varname>
995 above.</para></listitem>
8c7c9839
PS
996 </varlistentry>
997
798d3a52
ZJS
998 </variablelist>
999
1000 <para>Check
1001 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1002 and
1003 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1004 for more settings.</para>
1005
1006 </refsect1>
1007
1008 <refsect1>
1009 <title>Command lines</title>
1010
1011 <para>This section describes command line parsing and
ff9b60f3 1012 variable and specifier substitutions for
798d3a52
ZJS
1013 <varname>ExecStart=</varname>,
1014 <varname>ExecStartPre=</varname>,
1015 <varname>ExecStartPost=</varname>,
1016 <varname>ExecReload=</varname>,
1017 <varname>ExecStop=</varname>, and
1018 <varname>ExecStopPost=</varname> options.</para>
1019
1020 <para>Multiple command lines may be concatenated in a single
1021 directive by separating them with semicolons (these semicolons
1022 must be passed as separate words). Lone semicolons may be escaped
1023 as <literal>\;</literal>.</para>
1024
1025 <para>Each command line is split on whitespace, with the first
1026 item being the command to execute, and the subsequent items being
1027 the arguments. Double quotes ("...") and single quotes ('...') may
1028 be used, in which case everything until the next matching quote
1029 becomes part of the same argument. C-style escapes are also
388a91b0
ZJS
1030 supported. The table below contains the list of allowed escape
1031 patterns. Only patterns which match the syntax in the table are
1032 allowed; others will result in an error, and must be escaped by
1033 doubling the backslash. Quotes themselves are removed after
798d3a52
ZJS
1034 parsing and escape sequences substituted. In addition, a trailing
1035 backslash (<literal>\</literal>) may be used to merge lines.
1036 </para>
1037
1038 <para>This syntax is intended to be very similar to shell syntax,
1039 but only the meta-characters and expansions described in the
1040 following paragraphs are understood. Specifically, redirection
1041 using
1042 <literal>&lt;</literal>,
1043 <literal>&lt;&lt;</literal>,
1044 <literal>&gt;</literal>, and
1045 <literal>&gt;&gt;</literal>, pipes using
1046 <literal>|</literal>, running programs in the background using
1047 <literal>&amp;</literal>, and <emphasis>other elements of shell
1048 syntax are not supported</emphasis>.</para>
1049
388a91b0 1050 <para>The command to execute must be an absolute path name. It may
798d3a52
ZJS
1051 contain spaces, but control characters are not allowed.</para>
1052
1053 <para>The command line accepts <literal>%</literal> specifiers as
1054 described in
1055 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
1056 Note that the first argument of the command line (i.e. the program
1057 to execute) may not include specifiers.</para>
1058
1059 <para>Basic environment variable substitution is supported. Use
1060 <literal>${FOO}</literal> as part of a word, or as a word of its
1061 own, on the command line, in which case it will be replaced by the
1062 value of the environment variable including all whitespace it
1063 contains, resulting in a single argument. Use
1064 <literal>$FOO</literal> as a separate word on the command line, in
1065 which case it will be replaced by the value of the environment
b938cb90 1066 variable split at whitespace, resulting in zero or more arguments.
3faf145d 1067 For this type of expansion, quotes are respected when splitting
798d3a52
ZJS
1068 into words, and afterwards removed.</para>
1069
1070 <para>Example:</para>
1071
1072 <programlisting>Environment="ONE=one" 'TWO=two two'
5d9a2698
ZJS
1073ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
1074
798d3a52
ZJS
1075 <para>This will execute <command>/bin/echo</command> with four
1076 arguments: <literal>one</literal>, <literal>two</literal>,
1077 <literal>two</literal>, and <literal>two two</literal>.</para>
5d9a2698 1078
798d3a52
ZJS
1079 <para>Example:</para>
1080 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
5d9a2698
ZJS
1081ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1082ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
798d3a52
ZJS
1083 <para>This results in <filename>echo</filename> being
1084 called twice, the first time with arguments
1085 <literal>'one'</literal>,
1086 <literal>'two two' too</literal>, <literal></literal>,
1087 and the second time with arguments
1088 <literal>one</literal>, <literal>two two</literal>,
1089 <literal>too</literal>.
1090 </para>
1091
1092 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1093 Variables whose value is not known at expansion time are treated
1094 as empty strings. Note that the first argument (i.e. the program
1095 to execute) may not be a variable.</para>
1096
1097 <para>Variables to be used in this fashion may be defined through
1098 <varname>Environment=</varname> and
1099 <varname>EnvironmentFile=</varname>. In addition, variables listed
1100 in the section "Environment variables in spawned processes" in
1101 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1102 which are considered "static configuration", may be used (this
1103 includes e.g. <varname>$USER</varname>, but not
1104 <varname>$TERM</varname>).</para>
1105
1106 <para>Note that shell command lines are not directly supported. If
1107 shell command lines are to be used, they need to be passed
1108 explicitly to a shell implementation of some kind. Example:</para>
1109 <programlisting>ExecStart=/bin/sh -c 'dmesg | tac'</programlisting>
1110
1111 <para>Example:</para>
1112
1113 <programlisting>ExecStart=/bin/echo one ; /bin/echo "two two"</programlisting>
1114
1115 <para>This will execute <command>/bin/echo</command> two times,
1116 each time with one argument: <literal>one</literal> and
1117 <literal>two two</literal>, respectively. Because two commands are
1118 specified, <varname>Type=oneshot</varname> must be used.</para>
1119
1120 <para>Example:</para>
1121
1122 <programlisting>ExecStart=/bin/echo / &gt;/dev/null &amp; \; \
30d88d54
ZJS
1123/bin/ls</programlisting>
1124
798d3a52
ZJS
1125 <para>This will execute <command>/bin/echo</command>
1126 with five arguments: <literal>/</literal>,
1127 <literal>&gt;/dev/null</literal>,
1128 <literal>&amp;</literal>, <literal>;</literal>, and
1129 <literal>/bin/ls</literal>.</para>
1130
1131 <table>
1132 <title>C escapes supported in command lines and environment variables</title>
1133 <tgroup cols='2'>
1134 <colspec colname='escape' />
1135 <colspec colname='meaning' />
1136 <thead>
1137 <row>
1138 <entry>Literal</entry>
1139 <entry>Actual value</entry>
1140 </row>
1141 </thead>
1142 <tbody>
1143 <row>
1144 <entry><literal>\a</literal></entry>
1145 <entry>bell</entry>
1146 </row>
1147 <row>
1148 <entry><literal>\b</literal></entry>
1149 <entry>backspace</entry>
1150 </row>
1151 <row>
1152 <entry><literal>\f</literal></entry>
1153 <entry>form feed</entry>
1154 </row>
1155 <row>
1156 <entry><literal>\n</literal></entry>
1157 <entry>newline</entry>
1158 </row>
1159 <row>
1160 <entry><literal>\r</literal></entry>
1161 <entry>carriage return</entry>
1162 </row>
1163 <row>
1164 <entry><literal>\t</literal></entry>
1165 <entry>tab</entry>
1166 </row>
1167 <row>
1168 <entry><literal>\v</literal></entry>
1169 <entry>vertical tab</entry>
1170 </row>
1171 <row>
1172 <entry><literal>\\</literal></entry>
1173 <entry>backslash</entry>
1174 </row>
1175 <row>
1176 <entry><literal>\"</literal></entry>
1177 <entry>double quotation mark</entry>
1178 </row>
1179 <row>
1180 <entry><literal>\'</literal></entry>
1181 <entry>single quotation mark</entry>
1182 </row>
1183 <row>
1184 <entry><literal>\s</literal></entry>
1185 <entry>space</entry>
1186 </row>
1187 <row>
1188 <entry><literal>\x<replaceable>xx</replaceable></literal></entry>
1189 <entry>character number <replaceable>xx</replaceable> in hexadecimal encoding</entry>
1190 </row>
1191 <row>
1192 <entry><literal>\<replaceable>nnn</replaceable></literal></entry>
1193 <entry>character number <replaceable>nnn</replaceable> in octal encoding</entry>
1194 </row>
1195 </tbody>
1196 </tgroup>
1197 </table>
1198 </refsect1>
1199
1200 <refsect1>
1201 <title>Examples</title>
1202
1203 <example>
1204 <title>Simple service</title>
1205
1206 <para>The following unit file creates a service that will
1207 execute <filename>/usr/sbin/foo-daemon</filename>. Since no
1208 <varname>Type=</varname> is specified, the default
1209 <varname>Type=</varname><option>simple</option> will be assumed.
1210 systemd will assume the unit to be started immediately after the
1211 program has begun executing.</para>
1212
1213 <programlisting>[Unit]
d44efb62
CS
1214Description=Foo
1215
1216[Service]
1217ExecStart=/usr/sbin/foo-daemon
1218
1219[Install]
1220WantedBy=multi-user.target</programlisting>
1221
798d3a52
ZJS
1222 <para>Note that systemd assumes here that the process started by
1223 systemd will continue running until the service terminates. If
1224 the program daemonizes itself (i.e. forks), please use
1225 <varname>Type=</varname><option>forking</option> instead.</para>
1226
1227 <para>Since no <varname>ExecStop=</varname> was specified,
1228 systemd will send SIGTERM to all processes started from this
1229 service, and after a timeout also SIGKILL. This behavior can be
1230 modified, see
1231 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1232 for details.</para>
1233
1234 <para>Note that this unit type does not include any type of
1235 notification when a service has completed initialization. For
1236 this, you should use other unit types, such as
1237 <varname>Type=</varname><option>notify</option> if the service
1238 understands systemd's notification protocol,
1239 <varname>Type=</varname><option>forking</option> if the service
1240 can background itself or
1241 <varname>Type=</varname><option>dbus</option> if the unit
1242 acquires a DBus name once initialization is complete. See
1243 below.</para>
1244 </example>
1245
1246 <example>
1247 <title>Oneshot service</title>
1248
b938cb90 1249 <para>Sometimes, units should just execute an action without
798d3a52
ZJS
1250 keeping active processes, such as a filesystem check or a
1251 cleanup action on boot. For this,
1252 <varname>Type=</varname><option>oneshot</option> exists. Units
1253 of this type will wait until the process specified terminates
1254 and then fall back to being inactive. The following unit will
ff9b60f3 1255 perform a cleanup action:</para>
798d3a52
ZJS
1256
1257 <programlisting>[Unit]
d44efb62
CS
1258Description=Cleanup old Foo data
1259
1260[Service]
1261Type=oneshot
1262ExecStart=/usr/sbin/foo-cleanup
1263
1264[Install]
1265WantedBy=multi-user.target</programlisting>
1266
798d3a52 1267 <para>Note that systemd will consider the unit to be in the
b938cb90 1268 state "starting" until the program has terminated, so ordered
798d3a52 1269 dependencies will wait for the program to finish before starting
b938cb90
JE
1270 themselves. The unit will revert to the "inactive" state after
1271 the execution is done, never reaching the "active" state. That
798d3a52
ZJS
1272 means another request to start the unit will perform the action
1273 again.</para>
1274
1275 <para><varname>Type=</varname><option>oneshot</option> are the
1276 only service units that may have more than one
1277 <varname>ExecStart=</varname> specified. They will be executed
1278 in order until either they are all successful or one of them
1279 fails.</para>
1280 </example>
1281
1282 <example>
1283 <title>Stoppable oneshot service</title>
1284
1285 <para>Similarly to the oneshot services, there are sometimes
1286 units that need to execute a program to set up something and
1287 then execute another to shut it down, but no process remains
b938cb90 1288 active while they are considered "started". Network
798d3a52 1289 configuration can sometimes fall into this category. Another use
a8eaaee7 1290 case is if a oneshot service shall not be executed each time
798d3a52
ZJS
1291 when they are pulled in as a dependency, but only the first
1292 time.</para>
1293
1294 <para>For this, systemd knows the setting
1295 <varname>RemainAfterExit=</varname><option>yes</option>, which
1296 causes systemd to consider the unit to be active if the start
1297 action exited successfully. This directive can be used with all
1298 types, but is most useful with
1299 <varname>Type=</varname><option>oneshot</option> and
1300 <varname>Type=</varname><option>simple</option>. With
b938cb90 1301 <varname>Type=</varname><option>oneshot</option>, systemd waits
798d3a52
ZJS
1302 until the start action has completed before it considers the
1303 unit to be active, so dependencies start only after the start
1304 action has succeeded. With
b938cb90 1305 <varname>Type=</varname><option>simple</option>, dependencies
798d3a52
ZJS
1306 will start immediately after the start action has been
1307 dispatched. The following unit provides an example for a simple
1308 static firewall.</para>
1309
1310 <programlisting>[Unit]
d44efb62
CS
1311Description=Simple firewall
1312
1313[Service]
1314Type=oneshot
1315RemainAfterExit=yes
1316ExecStart=/usr/local/sbin/simple-firewall-start
1317ExecStop=/usr/local/sbin/simple-firewall-stop
1318
1319[Install]
1320WantedBy=multi-user.target</programlisting>
1321
798d3a52
ZJS
1322 <para>Since the unit is considered to be running after the start
1323 action has exited, invoking <command>systemctl start</command>
1324 on that unit again will cause no action to be taken.</para>
1325 </example>
1326
1327 <example>
1328 <title>Traditional forking services</title>
1329
1330 <para>Many traditional daemons/services background (i.e. fork,
1331 daemonize) themselves when starting. Set
1332 <varname>Type=</varname><option>forking</option> in the
1333 service's unit file to support this mode of operation. systemd
1334 will consider the service to be in the process of initialization
1335 while the original program is still running. Once it exits
1336 successfully and at least a process remains (and
1337 <varname>RemainAfterExit=</varname><option>no</option>), the
1338 service is considered started.</para>
1339
b938cb90 1340 <para>Often, a traditional daemon only consists of one process.
798d3a52
ZJS
1341 Therefore, if only one process is left after the original
1342 process terminates, systemd will consider that process the main
1343 process of the service. In that case, the
1344 <varname>$MAINPID</varname> variable will be available in
1345 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1346 etc.</para>
1347
1348 <para>In case more than one process remains, systemd will be
1349 unable to determine the main process, so it will not assume
1350 there is one. In that case, <varname>$MAINPID</varname> will not
1351 expand to anything. However, if the process decides to write a
1352 traditional PID file, systemd will be able to read the main PID
1353 from there. Please set <varname>PIDFile=</varname> accordingly.
1354 Note that the daemon should write that file before finishing
b938cb90 1355 with its initialization. Otherwise, systemd might try to read the
798d3a52
ZJS
1356 file before it exists.</para>
1357
1358 <para>The following example shows a simple daemon that forks and
1359 just starts one process in the background:</para>
1360
1361 <programlisting>[Unit]
d44efb62
CS
1362Description=Some simple daemon
1363
1364[Service]
1365Type=forking
1366ExecStart=/usr/sbin/my-simple-daemon -d
1367
1368[Install]
1369WantedBy=multi-user.target</programlisting>
1370
798d3a52
ZJS
1371 <para>Please see
1372 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1373 for details on how you can influence the way systemd terminates
1374 the service.</para>
1375 </example>
1376
1377 <example>
1378 <title>DBus services</title>
1379
1380 <para>For services that acquire a name on the DBus system bus,
1381 use <varname>Type=</varname><option>dbus</option> and set
1382 <varname>BusName=</varname> accordingly. The service should not
1383 fork (daemonize). systemd will consider the service to be
1384 initialized once the name has been acquired on the system bus.
1385 The following example shows a typical DBus service:</para>
1386
1387 <programlisting>[Unit]
d44efb62
CS
1388Description=Simple DBus service
1389
1390[Service]
1391Type=dbus
1392BusName=org.example.simple-dbus-service
1393ExecStart=/usr/sbin/simple-dbus-service
1394
1395[Install]
1396WantedBy=multi-user.target</programlisting>
1397
7ca41557 1398 <para>For <emphasis>bus-activatable</emphasis> services, do not
798d3a52
ZJS
1399 include a <literal>[Install]</literal> section in the systemd
1400 service file, but use the <varname>SystemdService=</varname>
1401 option in the corresponding DBus service file, for example
1402 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
d44efb62 1403
798d3a52 1404 <programlisting>[D-BUS Service]
d44efb62
CS
1405Name=org.example.simple-dbus-service
1406Exec=/usr/sbin/simple-dbus-service
1407User=root
1408SystemdService=simple-dbus-service.service</programlisting>
1409
798d3a52
ZJS
1410 <para>Please see
1411 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1412 for details on how you can influence the way systemd terminates
1413 the service.</para>
1414 </example>
1415
1416 <example>
1417 <title>Services that notify systemd about their initialization</title>
1418
1419 <para><varname>Type=</varname><option>simple</option> services
1420 are really easy to write, but have the major disadvantage of
1421 systemd not being able to tell when initialization of the given
1422 service is complete. For this reason, systemd supports a simple
1423 notification protocol that allows daemons to make systemd aware
1424 that they are done initializing. Use
1425 <varname>Type=</varname><option>notify</option> for this. A
1426 typical service file for such a daemon would look like
1427 this:</para>
1428
1429 <programlisting>[Unit]
d44efb62
CS
1430Description=Simple notifying service
1431
1432[Service]
1433Type=notify
1434ExecStart=/usr/sbin/simple-notifying-service
1435
1436[Install]
1437WantedBy=multi-user.target</programlisting>
1438
798d3a52 1439 <para>Note that the daemon has to support systemd's notification
7ca41557 1440 protocol, else systemd will think the service has not started yet
798d3a52
ZJS
1441 and kill it after a timeout. For an example of how to update
1442 daemons to support this protocol transparently, take a look at
1443 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1444 systemd will consider the unit to be in the 'starting' state
1445 until a readiness notification has arrived.</para>
1446
1447 <para>Please see
1448 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1449 for details on how you can influence the way systemd terminates
1450 the service.</para>
1451 </example>
1452 </refsect1>
1453
1454 <refsect1>
1455 <title>See Also</title>
1456 <para>
1457 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1458 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1459 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1460 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1461 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1462 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1463 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
1464 </para>
1465 </refsect1>
d1ab0ca0
LP
1466
1467</refentry>