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