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