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