]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.service.xml
Merge pull request #1966 from evverx/fix-initrd-searching-on-debian
[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
798d3a52
ZJS
509 <varname>Restart=</varname> to <option>on-failure</option> or
510 <option>always</option>, the service will be automatically
511 restarted. The time configured here will be passed to the
512 executed service process in the
513 <varname>WATCHDOG_USEC=</varname> environment variable. This
514 allows daemons to automatically enable the keep-alive pinging
515 logic if watchdog support is enabled for the service. If this
516 option is used, <varname>NotifyAccess=</varname> (see below)
517 should be set to open access to the notification socket
518 provided by systemd. If <varname>NotifyAccess=</varname> is
519 not set, it will be implicitly set to <option>main</option>.
582f2fcb
EV
520 Defaults to 0, which disables this feature. The service can
521 check whether the service manager expects watchdog keep-alive
522 notifications. See
523 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
524 for details.
525 </para></listitem>
798d3a52
ZJS
526 </varlistentry>
527
528 <varlistentry>
529 <term><varname>Restart=</varname></term>
530 <listitem><para>Configures whether the service shall be
531 restarted when the service process exits, is killed, or a
532 timeout is reached. The service process may be the main
533 service process, but it may also be one of the processes
534 specified with <varname>ExecStartPre=</varname>,
535 <varname>ExecStartPost=</varname>,
536 <varname>ExecStop=</varname>,
537 <varname>ExecStopPost=</varname>, or
538 <varname>ExecReload=</varname>. When the death of the process
539 is a result of systemd operation (e.g. service stop or
540 restart), the service will not be restarted. Timeouts include
541 missing the watchdog "keep-alive ping" deadline and a service
542 start, reload, and stop operation timeouts.</para>
543
544 <para>Takes one of
545 <option>no</option>,
546 <option>on-success</option>,
547 <option>on-failure</option>,
548 <option>on-abnormal</option>,
549 <option>on-watchdog</option>,
550 <option>on-abort</option>, or
551 <option>always</option>.
552 If set to <option>no</option> (the default), the service will
553 not be restarted. If set to <option>on-success</option>, it
554 will be restarted only when the service process exits cleanly.
555 In this context, a clean exit means an exit code of 0, or one
556 of the signals
557 <constant>SIGHUP</constant>,
558 <constant>SIGINT</constant>,
559 <constant>SIGTERM</constant> or
560 <constant>SIGPIPE</constant>, and
561 additionally, exit statuses and signals specified in
562 <varname>SuccessExitStatus=</varname>. If set to
563 <option>on-failure</option>, the service will be restarted
564 when the process exits with a non-zero exit code, is
565 terminated by a signal (including on core dump, but excluding
ff9b60f3 566 the aforementioned four signals), when an operation (such as
798d3a52
ZJS
567 service reload) times out, and when the configured watchdog
568 timeout is triggered. If set to <option>on-abnormal</option>,
569 the service will be restarted when the process is terminated
570 by a signal (including on core dump, excluding the
571 aforementioned four signals), when an operation times out, or
572 when the watchdog timeout is triggered. If set to
573 <option>on-abort</option>, the service will be restarted only
574 if the service process exits due to an uncaught signal not
575 specified as a clean exit status. If set to
576 <option>on-watchdog</option>, the service will be restarted
577 only if the watchdog timeout for the service expires. If set
578 to <option>always</option>, the service will be restarted
579 regardless of whether it exited cleanly or not, got terminated
580 abnormally by a signal, or hit a timeout.</para>
581
582 <table>
583 <title>Exit causes and the effect of the <varname>Restart=</varname> settings on them</title>
584
585 <tgroup cols='2'>
586 <colspec colname='path' />
587 <colspec colname='expl' />
588 <thead>
589 <row>
590 <entry>Restart settings/Exit causes</entry>
591 <entry><option>no</option></entry>
592 <entry><option>always</option></entry>
593 <entry><option>on-success</option></entry>
594 <entry><option>on-failure</option></entry>
595 <entry><option>on-abnormal</option></entry>
596 <entry><option>on-abort</option></entry>
597 <entry><option>on-watchdog</option></entry>
598 </row>
599 </thead>
600 <tbody>
601 <row>
602 <entry>Clean exit code or signal</entry>
603 <entry/>
604 <entry>X</entry>
605 <entry>X</entry>
606 <entry/>
607 <entry/>
608 <entry/>
609 <entry/>
610 </row>
611 <row>
612 <entry>Unclean exit code</entry>
613 <entry/>
614 <entry>X</entry>
615 <entry/>
616 <entry>X</entry>
617 <entry/>
618 <entry/>
619 <entry/>
620 </row>
621 <row>
622 <entry>Unclean signal</entry>
623 <entry/>
624 <entry>X</entry>
625 <entry/>
626 <entry>X</entry>
627 <entry>X</entry>
628 <entry>X</entry>
629 <entry/>
630 </row>
631 <row>
632 <entry>Timeout</entry>
633 <entry/>
634 <entry>X</entry>
635 <entry/>
636 <entry>X</entry>
637 <entry>X</entry>
638 <entry/>
639 <entry/>
640 </row>
641 <row>
642 <entry>Watchdog</entry>
643 <entry/>
644 <entry>X</entry>
645 <entry/>
646 <entry>X</entry>
647 <entry>X</entry>
648 <entry/>
649 <entry>X</entry>
650 </row>
651 </tbody>
652 </tgroup>
653 </table>
654
b938cb90 655 <para>As exceptions to the setting above, the service will not
798d3a52
ZJS
656 be restarted if the exit code or signal is specified in
657 <varname>RestartPreventExitStatus=</varname> (see below).
658 Also, the services will always be restarted if the exit code
659 or signal is specified in
660 <varname>RestartForceExitStatus=</varname> (see below).</para>
661
662 <para>Setting this to <option>on-failure</option> is the
663 recommended choice for long-running services, in order to
664 increase reliability by attempting automatic recovery from
665 errors. For services that shall be able to terminate on their
666 own choice (and avoid immediate restarting),
667 <option>on-abnormal</option> is an alternative choice.</para>
668 </listitem>
669 </varlistentry>
670
671 <varlistentry>
672 <term><varname>SuccessExitStatus=</varname></term>
b938cb90
JE
673 <listitem><para>Takes a list of exit status definitions that,
674 when returned by the main service process, will be considered
798d3a52
ZJS
675 successful termination, in addition to the normal successful
676 exit code 0 and the signals <constant>SIGHUP</constant>,
677 <constant>SIGINT</constant>, <constant>SIGTERM</constant>, and
678 <constant>SIGPIPE</constant>. Exit status definitions can
679 either be numeric exit codes or termination signal names,
680 separated by spaces. For example:
dc83f27a
LP
681
682 <programlisting>SuccessExitStatus=1 2 8 SIGKILL</programlisting>
683
684 ensures that exit codes 1, 2, 8 and
798d3a52
ZJS
685 the termination signal <constant>SIGKILL</constant> are
686 considered clean service terminations.
687 </para>
688
689 <para>Note that if a process has a signal handler installed
690 and exits by calling
691 <citerefentry><refentrytitle>_exit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
692 in response to a signal, the information about the signal is
693 lost. Programs should instead perform cleanup and kill
694 themselves with the same signal instead. See
695 <ulink url="http://www.cons.org/cracauer/sigint.html">Proper
696 handling of SIGINT/SIGQUIT — How to be a proper
697 program</ulink>.</para>
698
699 <para>This option may appear more than once, in which case the
700 list of successful exit statuses is merged. If the empty
701 string is assigned to this option, the list is reset, all
702 prior assignments of this option will have no
703 effect.</para></listitem>
704 </varlistentry>
705
706 <varlistentry>
707 <term><varname>RestartPreventExitStatus=</varname></term>
b938cb90
JE
708 <listitem><para>Takes a list of exit status definitions that,
709 when returned by the main service process, will prevent
798d3a52
ZJS
710 automatic service restarts, regardless of the restart setting
711 configured with <varname>Restart=</varname>. Exit status
712 definitions can either be numeric exit codes or termination
713 signal names, and are separated by spaces. Defaults to the
714 empty list, so that, by default, no exit status is excluded
715 from the configured restart logic. For example:
dc83f27a
LP
716
717 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting>
718
719 ensures that exit codes 1 and 6 and the termination signal
720 <constant>SIGABRT</constant> will not result in automatic
721 service restarting. This option may appear more than once, in
722 which case the list of restart-preventing statuses is
723 merged. If the empty string is assigned to this option, the
724 list is reset and all prior assignments of this option will
725 have no effect.</para></listitem>
798d3a52
ZJS
726 </varlistentry>
727
728 <varlistentry>
729 <term><varname>RestartForceExitStatus=</varname></term>
b938cb90
JE
730 <listitem><para>Takes a list of exit status definitions that,
731 when returned by the main service process, will force automatic
798d3a52
ZJS
732 service restarts, regardless of the restart setting configured
733 with <varname>Restart=</varname>. The argument format is
734 similar to
735 <varname>RestartPreventExitStatus=</varname>.</para></listitem>
736 </varlistentry>
737
738 <varlistentry>
739 <term><varname>PermissionsStartOnly=</varname></term>
740 <listitem><para>Takes a boolean argument. If true, the
741 permission-related execution options, as configured with
742 <varname>User=</varname> and similar options (see
743 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
744 for more information), are only applied to the process started
745 with
746 <varname>ExecStart=</varname>, and not to the various other
747 <varname>ExecStartPre=</varname>,
748 <varname>ExecStartPost=</varname>,
749 <varname>ExecReload=</varname>,
750 <varname>ExecStop=</varname>, and
751 <varname>ExecStopPost=</varname>
752 commands. If false, the setting is applied to all configured
753 commands the same way. Defaults to false.</para></listitem>
754 </varlistentry>
755
756 <varlistentry>
757 <term><varname>RootDirectoryStartOnly=</varname></term>
758 <listitem><para>Takes a boolean argument. If true, the root
759 directory, as configured with the
760 <varname>RootDirectory=</varname> option (see
761 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
762 for more information), is only applied to the process started
763 with <varname>ExecStart=</varname>, and not to the various
764 other <varname>ExecStartPre=</varname>,
765 <varname>ExecStartPost=</varname>,
766 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
767 and <varname>ExecStopPost=</varname> commands. If false, the
768 setting is applied to all configured commands the same way.
769 Defaults to false.</para></listitem>
770 </varlistentry>
771
772 <varlistentry>
773 <term><varname>NonBlocking=</varname></term>
774 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag
775 for all file descriptors passed via socket-based activation.
776 If true, all file descriptors >= 3 (i.e. all except stdin,
777 stdout, and stderr) will have the
778 <constant>O_NONBLOCK</constant> flag set and hence are in
779 non-blocking mode. This option is only useful in conjunction
780 with a socket unit, as described in
781 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
782 Defaults to false.</para></listitem>
783 </varlistentry>
784
785 <varlistentry>
786 <term><varname>NotifyAccess=</varname></term>
787 <listitem><para>Controls access to the service status
788 notification socket, as accessible via the
789 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
790 call. Takes one of <option>none</option> (the default),
791 <option>main</option> or <option>all</option>. If
792 <option>none</option>, no daemon status updates are accepted
793 from the service processes, all status update messages are
794 ignored. If <option>main</option>, only service updates sent
795 from the main process of the service are accepted. If
796 <option>all</option>, all services updates from all members of
797 the service's control group are accepted. This option should
798 be set to open access to the notification socket when using
799 <varname>Type=notify</varname> or
800 <varname>WatchdogSec=</varname> (see above). If those options
801 are used but <varname>NotifyAccess=</varname> is not
802 configured, it will be implicitly set to
803 <option>main</option>.</para></listitem>
804 </varlistentry>
805
806 <varlistentry>
807 <term><varname>Sockets=</varname></term>
808 <listitem><para>Specifies the name of the socket units this
809 service shall inherit socket file descriptors from when the
b938cb90
JE
810 service is started. Normally, it should not be necessary to use
811 this setting, as all socket file descriptors whose unit shares
798d3a52
ZJS
812 the same name as the service (subject to the different unit
813 name suffix of course) are passed to the spawned
814 process.</para>
815
816 <para>Note that the same socket file descriptors may be passed
817 to multiple processes simultaneously. Also note that a
818 different service may be activated on incoming socket traffic
819 than the one which is ultimately configured to inherit the
b938cb90 820 socket file descriptors. Or, in other words: the
798d3a52
ZJS
821 <varname>Service=</varname> setting of
822 <filename>.socket</filename> units does not have to match the
823 inverse of the <varname>Sockets=</varname> setting of the
824 <filename>.service</filename> it refers to.</para>
825
826 <para>This option may appear more than once, in which case the
827 list of socket units is merged. If the empty string is
828 assigned to this option, the list of sockets is reset, and all
829 prior uses of this setting will have no
830 effect.</para></listitem>
831 </varlistentry>
832
833 <varlistentry>
834 <term><varname>StartLimitInterval=</varname></term>
835 <term><varname>StartLimitBurst=</varname></term>
836
837 <listitem><para>Configure service start rate limiting. By
838 default, services which are started more than 5 times within
839 10 seconds are not permitted to start any more times until the
840 10 second interval ends. With these two options, this rate
841 limiting may be modified. Use
842 <varname>StartLimitInterval=</varname> to configure the
843 checking interval (defaults to
844 <varname>DefaultStartLimitInterval=</varname> in manager
845 configuration file, set to 0 to disable any kind of rate
846 limiting). Use <varname>StartLimitBurst=</varname> to
847 configure how many starts per interval are allowed (defaults
848 to <varname>DefaultStartLimitBurst=</varname> in manager
849 configuration file). These configuration options are
850 particularly useful in conjunction with
851 <varname>Restart=</varname>; however, they apply to all kinds
852 of starts (including manual), not just those triggered by the
853 <varname>Restart=</varname> logic. Note that units which are
854 configured for <varname>Restart=</varname> and which reach the
855 start limit are not attempted to be restarted anymore;
856 however, they may still be restarted manually at a later
857 point, from which point on, the restart logic is again
858 activated. Note that <command>systemctl reset-failed</command>
859 will cause the restart rate counter for a service to be
860 flushed, which is useful if the administrator wants to
861 manually start a service and the start limit interferes with
862 that.</para></listitem>
863 </varlistentry>
864
865 <varlistentry>
866 <term><varname>StartLimitAction=</varname></term>
867
868 <listitem><para>Configure the action to take if the rate limit
869 configured with <varname>StartLimitInterval=</varname> and
870 <varname>StartLimitBurst=</varname> is hit. Takes one of
871 <option>none</option>,
872 <option>reboot</option>,
873 <option>reboot-force</option>,
874 <option>reboot-immediate</option>,
875 <option>poweroff</option>,
876 <option>poweroff-force</option> or
877 <option>poweroff-immediate</option>. If
878 <option>none</option> is set, hitting the rate limit will
879 trigger no action besides that the start will not be
880 permitted. <option>reboot</option> causes a reboot following
881 the normal shutdown procedure (i.e. equivalent to
882 <command>systemctl reboot</command>).
883 <option>reboot-force</option> causes a forced reboot which
884 will terminate all processes forcibly but should cause no
885 dirty file systems on reboot (i.e. equivalent to
886 <command>systemctl reboot -f</command>) and
887 <option>reboot-immediate</option> causes immediate execution
888 of the
889 <citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
a8eaaee7 890 system call, which might result in data loss. Similarly,
798d3a52
ZJS
891 <option>poweroff</option>, <option>poweroff-force</option>,
892 <option>poweroff-immediate</option> have the effect of
893 powering down the system with similar semantics. Defaults to
894 <option>none</option>.</para></listitem>
895 </varlistentry>
896
897 <varlistentry>
898 <term><varname>FailureAction=</varname></term>
899 <listitem><para>Configure the action to take when the service
900 enters a failed state. Takes the same values as
901 <varname>StartLimitAction=</varname> and executes the same
902 actions. Defaults to <option>none</option>. </para></listitem>
903 </varlistentry>
904
905 <varlistentry>
906 <term><varname>RebootArgument=</varname></term>
907 <listitem><para>Configure the optional argument for the
908 <citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
909 system call if <varname>StartLimitAction=</varname> or
910 <varname>FailureAction=</varname> is a reboot action. This
911 works just like the optional argument to <command>systemctl
912 reboot</command> command.</para></listitem>
913 </varlistentry>
914
915 <varlistentry>
916 <term><varname>FileDescriptorStoreMax=</varname></term>
917 <listitem><para>Configure how many file descriptors may be
918 stored in the service manager for the service using
919 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
920 <literal>FDSTORE=1</literal> messages. This is useful for
921 implementing service restart schemes where the state is
922 serialized to <filename>/run</filename> and the file
923 descriptors passed to the service manager, to allow restarts
924 without losing state. Defaults to 0, i.e. no file descriptors
925 may be stored in the service manager by default. All file
926 descriptors passed to the service manager from a specific
927 service are passed back to the service's main process on the
928 next service restart. Any file descriptors passed to the
929 service manager are automatically closed when POLLHUP or
930 POLLERR is seen on them, or when the service is fully stopped
931 and no job queued or being executed for it.</para></listitem>
932 </varlistentry>
933
8c7c9839
PS
934 <varlistentry>
935 <term><varname>USBFunctionDescriptors=</varname></term>
3d314510
LP
936 <listitem><para>Configure the location of a file containing
937 <ulink
938 url="https://www.kernel.org/doc/Documentation/usb/functionfs.txt">USB
939 FunctionFS</ulink> descriptors, for implementation of USB
a8eaaee7 940 gadget functions. This is used only in conjunction with a
3d314510 941 socket unit with <varname>ListenUSBFunction=</varname>
a8eaaee7 942 configured. The contents of this file are written to the
3d314510
LP
943 <filename>ep0</filename> file after it is
944 opened.</para></listitem>
8c7c9839
PS
945 </varlistentry>
946
947 <varlistentry>
948 <term><varname>USBFunctionStrings=</varname></term>
3d314510
LP
949 <listitem><para>Configure the location of a file containing
950 USB FunctionFS strings. Behavior is similar to
951 <varname>USBFunctionDescriptors=</varname>
952 above.</para></listitem>
8c7c9839
PS
953 </varlistentry>
954
798d3a52
ZJS
955 </variablelist>
956
957 <para>Check
958 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
959 and
960 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
961 for more settings.</para>
962
963 </refsect1>
964
965 <refsect1>
966 <title>Command lines</title>
967
968 <para>This section describes command line parsing and
ff9b60f3 969 variable and specifier substitutions for
798d3a52
ZJS
970 <varname>ExecStart=</varname>,
971 <varname>ExecStartPre=</varname>,
972 <varname>ExecStartPost=</varname>,
973 <varname>ExecReload=</varname>,
974 <varname>ExecStop=</varname>, and
975 <varname>ExecStopPost=</varname> options.</para>
976
977 <para>Multiple command lines may be concatenated in a single
978 directive by separating them with semicolons (these semicolons
979 must be passed as separate words). Lone semicolons may be escaped
980 as <literal>\;</literal>.</para>
981
982 <para>Each command line is split on whitespace, with the first
983 item being the command to execute, and the subsequent items being
984 the arguments. Double quotes ("...") and single quotes ('...') may
985 be used, in which case everything until the next matching quote
986 becomes part of the same argument. C-style escapes are also
388a91b0
ZJS
987 supported. The table below contains the list of allowed escape
988 patterns. Only patterns which match the syntax in the table are
989 allowed; others will result in an error, and must be escaped by
990 doubling the backslash. Quotes themselves are removed after
798d3a52
ZJS
991 parsing and escape sequences substituted. In addition, a trailing
992 backslash (<literal>\</literal>) may be used to merge lines.
993 </para>
994
995 <para>This syntax is intended to be very similar to shell syntax,
996 but only the meta-characters and expansions described in the
997 following paragraphs are understood. Specifically, redirection
998 using
999 <literal>&lt;</literal>,
1000 <literal>&lt;&lt;</literal>,
1001 <literal>&gt;</literal>, and
1002 <literal>&gt;&gt;</literal>, pipes using
1003 <literal>|</literal>, running programs in the background using
1004 <literal>&amp;</literal>, and <emphasis>other elements of shell
1005 syntax are not supported</emphasis>.</para>
1006
388a91b0 1007 <para>The command to execute must be an absolute path name. It may
798d3a52
ZJS
1008 contain spaces, but control characters are not allowed.</para>
1009
1010 <para>The command line accepts <literal>%</literal> specifiers as
1011 described in
1012 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
1013 Note that the first argument of the command line (i.e. the program
1014 to execute) may not include specifiers.</para>
1015
1016 <para>Basic environment variable substitution is supported. Use
1017 <literal>${FOO}</literal> as part of a word, or as a word of its
1018 own, on the command line, in which case it will be replaced by the
1019 value of the environment variable including all whitespace it
1020 contains, resulting in a single argument. Use
1021 <literal>$FOO</literal> as a separate word on the command line, in
1022 which case it will be replaced by the value of the environment
b938cb90 1023 variable split at whitespace, resulting in zero or more arguments.
3faf145d 1024 For this type of expansion, quotes are respected when splitting
798d3a52
ZJS
1025 into words, and afterwards removed.</para>
1026
1027 <para>Example:</para>
1028
1029 <programlisting>Environment="ONE=one" 'TWO=two two'
5d9a2698
ZJS
1030ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
1031
798d3a52
ZJS
1032 <para>This will execute <command>/bin/echo</command> with four
1033 arguments: <literal>one</literal>, <literal>two</literal>,
1034 <literal>two</literal>, and <literal>two two</literal>.</para>
5d9a2698 1035
798d3a52
ZJS
1036 <para>Example:</para>
1037 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE=
5d9a2698
ZJS
1038ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
1039ExecStart=/bin/echo $ONE $TWO $THREE</programlisting>
798d3a52
ZJS
1040 <para>This results in <filename>echo</filename> being
1041 called twice, the first time with arguments
1042 <literal>'one'</literal>,
1043 <literal>'two two' too</literal>, <literal></literal>,
1044 and the second time with arguments
1045 <literal>one</literal>, <literal>two two</literal>,
1046 <literal>too</literal>.
1047 </para>
1048
1049 <para>To pass a literal dollar sign, use <literal>$$</literal>.
1050 Variables whose value is not known at expansion time are treated
1051 as empty strings. Note that the first argument (i.e. the program
1052 to execute) may not be a variable.</para>
1053
1054 <para>Variables to be used in this fashion may be defined through
1055 <varname>Environment=</varname> and
1056 <varname>EnvironmentFile=</varname>. In addition, variables listed
1057 in the section "Environment variables in spawned processes" in
1058 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1059 which are considered "static configuration", may be used (this
1060 includes e.g. <varname>$USER</varname>, but not
1061 <varname>$TERM</varname>).</para>
1062
1063 <para>Note that shell command lines are not directly supported. If
1064 shell command lines are to be used, they need to be passed
1065 explicitly to a shell implementation of some kind. Example:</para>
1066 <programlisting>ExecStart=/bin/sh -c 'dmesg | tac'</programlisting>
1067
1068 <para>Example:</para>
1069
1070 <programlisting>ExecStart=/bin/echo one ; /bin/echo "two two"</programlisting>
1071
1072 <para>This will execute <command>/bin/echo</command> two times,
1073 each time with one argument: <literal>one</literal> and
1074 <literal>two two</literal>, respectively. Because two commands are
1075 specified, <varname>Type=oneshot</varname> must be used.</para>
1076
1077 <para>Example:</para>
1078
1079 <programlisting>ExecStart=/bin/echo / &gt;/dev/null &amp; \; \
30d88d54
ZJS
1080/bin/ls</programlisting>
1081
798d3a52
ZJS
1082 <para>This will execute <command>/bin/echo</command>
1083 with five arguments: <literal>/</literal>,
1084 <literal>&gt;/dev/null</literal>,
1085 <literal>&amp;</literal>, <literal>;</literal>, and
1086 <literal>/bin/ls</literal>.</para>
1087
1088 <table>
1089 <title>C escapes supported in command lines and environment variables</title>
1090 <tgroup cols='2'>
1091 <colspec colname='escape' />
1092 <colspec colname='meaning' />
1093 <thead>
1094 <row>
1095 <entry>Literal</entry>
1096 <entry>Actual value</entry>
1097 </row>
1098 </thead>
1099 <tbody>
1100 <row>
1101 <entry><literal>\a</literal></entry>
1102 <entry>bell</entry>
1103 </row>
1104 <row>
1105 <entry><literal>\b</literal></entry>
1106 <entry>backspace</entry>
1107 </row>
1108 <row>
1109 <entry><literal>\f</literal></entry>
1110 <entry>form feed</entry>
1111 </row>
1112 <row>
1113 <entry><literal>\n</literal></entry>
1114 <entry>newline</entry>
1115 </row>
1116 <row>
1117 <entry><literal>\r</literal></entry>
1118 <entry>carriage return</entry>
1119 </row>
1120 <row>
1121 <entry><literal>\t</literal></entry>
1122 <entry>tab</entry>
1123 </row>
1124 <row>
1125 <entry><literal>\v</literal></entry>
1126 <entry>vertical tab</entry>
1127 </row>
1128 <row>
1129 <entry><literal>\\</literal></entry>
1130 <entry>backslash</entry>
1131 </row>
1132 <row>
1133 <entry><literal>\"</literal></entry>
1134 <entry>double quotation mark</entry>
1135 </row>
1136 <row>
1137 <entry><literal>\'</literal></entry>
1138 <entry>single quotation mark</entry>
1139 </row>
1140 <row>
1141 <entry><literal>\s</literal></entry>
1142 <entry>space</entry>
1143 </row>
1144 <row>
1145 <entry><literal>\x<replaceable>xx</replaceable></literal></entry>
1146 <entry>character number <replaceable>xx</replaceable> in hexadecimal encoding</entry>
1147 </row>
1148 <row>
1149 <entry><literal>\<replaceable>nnn</replaceable></literal></entry>
1150 <entry>character number <replaceable>nnn</replaceable> in octal encoding</entry>
1151 </row>
1152 </tbody>
1153 </tgroup>
1154 </table>
1155 </refsect1>
1156
1157 <refsect1>
1158 <title>Examples</title>
1159
1160 <example>
1161 <title>Simple service</title>
1162
1163 <para>The following unit file creates a service that will
1164 execute <filename>/usr/sbin/foo-daemon</filename>. Since no
1165 <varname>Type=</varname> is specified, the default
1166 <varname>Type=</varname><option>simple</option> will be assumed.
1167 systemd will assume the unit to be started immediately after the
1168 program has begun executing.</para>
1169
1170 <programlisting>[Unit]
d44efb62
CS
1171Description=Foo
1172
1173[Service]
1174ExecStart=/usr/sbin/foo-daemon
1175
1176[Install]
1177WantedBy=multi-user.target</programlisting>
1178
798d3a52
ZJS
1179 <para>Note that systemd assumes here that the process started by
1180 systemd will continue running until the service terminates. If
1181 the program daemonizes itself (i.e. forks), please use
1182 <varname>Type=</varname><option>forking</option> instead.</para>
1183
1184 <para>Since no <varname>ExecStop=</varname> was specified,
1185 systemd will send SIGTERM to all processes started from this
1186 service, and after a timeout also SIGKILL. This behavior can be
1187 modified, see
1188 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1189 for details.</para>
1190
1191 <para>Note that this unit type does not include any type of
1192 notification when a service has completed initialization. For
1193 this, you should use other unit types, such as
1194 <varname>Type=</varname><option>notify</option> if the service
1195 understands systemd's notification protocol,
1196 <varname>Type=</varname><option>forking</option> if the service
1197 can background itself or
1198 <varname>Type=</varname><option>dbus</option> if the unit
1199 acquires a DBus name once initialization is complete. See
1200 below.</para>
1201 </example>
1202
1203 <example>
1204 <title>Oneshot service</title>
1205
b938cb90 1206 <para>Sometimes, units should just execute an action without
798d3a52
ZJS
1207 keeping active processes, such as a filesystem check or a
1208 cleanup action on boot. For this,
1209 <varname>Type=</varname><option>oneshot</option> exists. Units
1210 of this type will wait until the process specified terminates
1211 and then fall back to being inactive. The following unit will
ff9b60f3 1212 perform a cleanup action:</para>
798d3a52
ZJS
1213
1214 <programlisting>[Unit]
d44efb62
CS
1215Description=Cleanup old Foo data
1216
1217[Service]
1218Type=oneshot
1219ExecStart=/usr/sbin/foo-cleanup
1220
1221[Install]
1222WantedBy=multi-user.target</programlisting>
1223
798d3a52 1224 <para>Note that systemd will consider the unit to be in the
b938cb90 1225 state "starting" until the program has terminated, so ordered
798d3a52 1226 dependencies will wait for the program to finish before starting
b938cb90
JE
1227 themselves. The unit will revert to the "inactive" state after
1228 the execution is done, never reaching the "active" state. That
798d3a52
ZJS
1229 means another request to start the unit will perform the action
1230 again.</para>
1231
1232 <para><varname>Type=</varname><option>oneshot</option> are the
1233 only service units that may have more than one
1234 <varname>ExecStart=</varname> specified. They will be executed
1235 in order until either they are all successful or one of them
1236 fails.</para>
1237 </example>
1238
1239 <example>
1240 <title>Stoppable oneshot service</title>
1241
1242 <para>Similarly to the oneshot services, there are sometimes
1243 units that need to execute a program to set up something and
1244 then execute another to shut it down, but no process remains
b938cb90 1245 active while they are considered "started". Network
798d3a52 1246 configuration can sometimes fall into this category. Another use
a8eaaee7 1247 case is if a oneshot service shall not be executed each time
798d3a52
ZJS
1248 when they are pulled in as a dependency, but only the first
1249 time.</para>
1250
1251 <para>For this, systemd knows the setting
1252 <varname>RemainAfterExit=</varname><option>yes</option>, which
1253 causes systemd to consider the unit to be active if the start
1254 action exited successfully. This directive can be used with all
1255 types, but is most useful with
1256 <varname>Type=</varname><option>oneshot</option> and
1257 <varname>Type=</varname><option>simple</option>. With
b938cb90 1258 <varname>Type=</varname><option>oneshot</option>, systemd waits
798d3a52
ZJS
1259 until the start action has completed before it considers the
1260 unit to be active, so dependencies start only after the start
1261 action has succeeded. With
b938cb90 1262 <varname>Type=</varname><option>simple</option>, dependencies
798d3a52
ZJS
1263 will start immediately after the start action has been
1264 dispatched. The following unit provides an example for a simple
1265 static firewall.</para>
1266
1267 <programlisting>[Unit]
d44efb62
CS
1268Description=Simple firewall
1269
1270[Service]
1271Type=oneshot
1272RemainAfterExit=yes
1273ExecStart=/usr/local/sbin/simple-firewall-start
1274ExecStop=/usr/local/sbin/simple-firewall-stop
1275
1276[Install]
1277WantedBy=multi-user.target</programlisting>
1278
798d3a52
ZJS
1279 <para>Since the unit is considered to be running after the start
1280 action has exited, invoking <command>systemctl start</command>
1281 on that unit again will cause no action to be taken.</para>
1282 </example>
1283
1284 <example>
1285 <title>Traditional forking services</title>
1286
1287 <para>Many traditional daemons/services background (i.e. fork,
1288 daemonize) themselves when starting. Set
1289 <varname>Type=</varname><option>forking</option> in the
1290 service's unit file to support this mode of operation. systemd
1291 will consider the service to be in the process of initialization
1292 while the original program is still running. Once it exits
1293 successfully and at least a process remains (and
1294 <varname>RemainAfterExit=</varname><option>no</option>), the
1295 service is considered started.</para>
1296
b938cb90 1297 <para>Often, a traditional daemon only consists of one process.
798d3a52
ZJS
1298 Therefore, if only one process is left after the original
1299 process terminates, systemd will consider that process the main
1300 process of the service. In that case, the
1301 <varname>$MAINPID</varname> variable will be available in
1302 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>,
1303 etc.</para>
1304
1305 <para>In case more than one process remains, systemd will be
1306 unable to determine the main process, so it will not assume
1307 there is one. In that case, <varname>$MAINPID</varname> will not
1308 expand to anything. However, if the process decides to write a
1309 traditional PID file, systemd will be able to read the main PID
1310 from there. Please set <varname>PIDFile=</varname> accordingly.
1311 Note that the daemon should write that file before finishing
b938cb90 1312 with its initialization. Otherwise, systemd might try to read the
798d3a52
ZJS
1313 file before it exists.</para>
1314
1315 <para>The following example shows a simple daemon that forks and
1316 just starts one process in the background:</para>
1317
1318 <programlisting>[Unit]
d44efb62
CS
1319Description=Some simple daemon
1320
1321[Service]
1322Type=forking
1323ExecStart=/usr/sbin/my-simple-daemon -d
1324
1325[Install]
1326WantedBy=multi-user.target</programlisting>
1327
798d3a52
ZJS
1328 <para>Please see
1329 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1330 for details on how you can influence the way systemd terminates
1331 the service.</para>
1332 </example>
1333
1334 <example>
1335 <title>DBus services</title>
1336
1337 <para>For services that acquire a name on the DBus system bus,
1338 use <varname>Type=</varname><option>dbus</option> and set
1339 <varname>BusName=</varname> accordingly. The service should not
1340 fork (daemonize). systemd will consider the service to be
1341 initialized once the name has been acquired on the system bus.
1342 The following example shows a typical DBus service:</para>
1343
1344 <programlisting>[Unit]
d44efb62
CS
1345Description=Simple DBus service
1346
1347[Service]
1348Type=dbus
1349BusName=org.example.simple-dbus-service
1350ExecStart=/usr/sbin/simple-dbus-service
1351
1352[Install]
1353WantedBy=multi-user.target</programlisting>
1354
7ca41557 1355 <para>For <emphasis>bus-activatable</emphasis> services, do not
798d3a52
ZJS
1356 include a <literal>[Install]</literal> section in the systemd
1357 service file, but use the <varname>SystemdService=</varname>
1358 option in the corresponding DBus service file, for example
1359 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para>
d44efb62 1360
798d3a52 1361 <programlisting>[D-BUS Service]
d44efb62
CS
1362Name=org.example.simple-dbus-service
1363Exec=/usr/sbin/simple-dbus-service
1364User=root
1365SystemdService=simple-dbus-service.service</programlisting>
1366
798d3a52
ZJS
1367 <para>Please see
1368 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1369 for details on how you can influence the way systemd terminates
1370 the service.</para>
1371 </example>
1372
1373 <example>
1374 <title>Services that notify systemd about their initialization</title>
1375
1376 <para><varname>Type=</varname><option>simple</option> services
1377 are really easy to write, but have the major disadvantage of
1378 systemd not being able to tell when initialization of the given
1379 service is complete. For this reason, systemd supports a simple
1380 notification protocol that allows daemons to make systemd aware
1381 that they are done initializing. Use
1382 <varname>Type=</varname><option>notify</option> for this. A
1383 typical service file for such a daemon would look like
1384 this:</para>
1385
1386 <programlisting>[Unit]
d44efb62
CS
1387Description=Simple notifying service
1388
1389[Service]
1390Type=notify
1391ExecStart=/usr/sbin/simple-notifying-service
1392
1393[Install]
1394WantedBy=multi-user.target</programlisting>
1395
798d3a52 1396 <para>Note that the daemon has to support systemd's notification
7ca41557 1397 protocol, else systemd will think the service has not started yet
798d3a52
ZJS
1398 and kill it after a timeout. For an example of how to update
1399 daemons to support this protocol transparently, take a look at
1400 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1401 systemd will consider the unit to be in the 'starting' state
1402 until a readiness notification has arrived.</para>
1403
1404 <para>Please see
1405 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>
1406 for details on how you can influence the way systemd terminates
1407 the service.</para>
1408 </example>
1409 </refsect1>
1410
1411 <refsect1>
1412 <title>See Also</title>
1413 <para>
1414 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1415 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
1416 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1417 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1418 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1419 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1420 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
1421 </para>
1422 </refsect1>
d1ab0ca0
LP
1423
1424</refentry>