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