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