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