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