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