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