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