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