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