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