1<?xml version='1.0'?> 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> 4<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> 5 6<refentry id="systemd.service"> 7 <refentryinfo> 8 <title>systemd.service</title> 9 <productname>systemd</productname> 10 </refentryinfo> 11 12 <refmeta> 13 <refentrytitle>systemd.service</refentrytitle> 14 <manvolnum>5</manvolnum> 15 </refmeta> 16 17 <refnamediv> 18 <refname>systemd.service</refname> 19 <refpurpose>Service unit configuration</refpurpose> 20 </refnamediv> 21 22 <refsynopsisdiv> 23 <para><filename><replaceable>service</replaceable>.service</filename></para> 24 </refsynopsisdiv> 25 26 <refsect1> 27 <title>Description</title> 28 29 <para>A unit configuration file whose name ends in 30 <literal>.service</literal> encodes information about a process 31 controlled and supervised by systemd.</para> 32 33 <para>This man page lists the configuration options specific to 34 this unit type. See 35 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> 36 for the common options of all unit configuration files. The common 37 configuration items are configured in the generic 38 [Unit] and [Install] 39 sections. The service specific configuration options are 40 configured in the [Service] section.</para> 41 42 <para>Additional options are listed in 43 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 44 which define the execution environment the commands are executed 45 in, and in 46 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 47 which define the way the processes of the service are terminated, 48 and in 49 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 50 which configure resource control settings for the processes of the 51 service.</para> 52 53 <para>If SysV init compat is enabled, systemd automatically creates service units that wrap SysV init 54 scripts (the service name is the same as the name of the script, with a <literal>.service</literal> 55 suffix added); see 56 <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>. 57 </para> 58 59 <para>The <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry> 60 command allows creating <filename>.service</filename> and <filename>.scope</filename> units dynamically 61 and transiently from the command line.</para> 62 </refsect1> 63 64 <refsect1> 65 <title>Service Templates</title> 66 67 <para>It is possible for <command>systemd</command> services to take a single argument via the 68 <literal><replaceable>service</replaceable>@<replaceable>argument</replaceable>.service</literal> 69 syntax. Such services are called "instantiated" services, while the unit definition without the 70 <replaceable>argument</replaceable> parameter is called a "template". An example could be a 71 <filename>dhcpcd@.service</filename> service template which takes a network interface as a 72 parameter to form an instantiated service. Within the service file, this parameter or "instance 73 name" can be accessed with %-specifiers. See 74 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> 75 for details.</para> 76 </refsect1> 77 78 <refsect1> 79 <title>Automatic Dependencies</title> 80 81 <refsect2> 82 <title>Implicit Dependencies</title> 83 84 <para>The following dependencies are implicitly added:</para> 85 86 <itemizedlist> 87 <listitem><para>Services with <varname>Type=dbus</varname> set automatically 88 acquire dependencies of type <varname>Requires=</varname> and 89 <varname>After=</varname> on 90 <filename>dbus.socket</filename>.</para></listitem> 91 92 <listitem><para>Socket activated services are automatically ordered after 93 their activating <filename>.socket</filename> units via an 94 automatic <varname>After=</varname> dependency. 95 Services also pull in all <filename>.socket</filename> units 96 listed in <varname>Sockets=</varname> via automatic 97 <varname>Wants=</varname> and <varname>After=</varname> dependencies.</para></listitem> 98 </itemizedlist> 99 100 <para>Additional implicit dependencies may be added as result of 101 execution and resource control parameters as documented in 102 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> 103 and 104 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para> 105 </refsect2> 106 107 <refsect2> 108 <title>Default Dependencies</title> 109 110 <para>The following dependencies are added unless <varname>DefaultDependencies=no</varname> is set:</para> 111 112 <itemizedlist> 113 <listitem><para>Service units will have dependencies of type <varname>Requires=</varname> and 114 <varname>After=</varname> on <filename>sysinit.target</filename>, a dependency of type <varname>After=</varname> on 115 <filename>basic.target</filename> as well as dependencies of type <varname>Conflicts=</varname> and 116 <varname>Before=</varname> on <filename>shutdown.target</filename>. These ensure that normal service units pull in 117 basic system initialization, and are terminated cleanly prior to system shutdown. Only services involved with early 118 boot or late system shutdown should disable this option.</para></listitem> 119 120 <listitem><para>Instanced service units (i.e. service units with an <literal>@</literal> in their name) are assigned by 121 default a per-template slice unit (see 122 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>), named after the 123 template unit, containing all instances of the specific template. This slice is normally stopped at shutdown, 124 together with all template instances. If that is not desired, set <varname>DefaultDependencies=no</varname> in the 125 template unit, and either define your own per-template slice unit file that also sets 126 <varname>DefaultDependencies=no</varname>, or set <varname>Slice=system.slice</varname> (or another suitable slice) 127 in the template unit. Also see 128 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>. 129 </para></listitem> 130 </itemizedlist> 131 </refsect2> 132 </refsect1> 133 134 <refsect1> 135 <title>Options</title> 136 137 <para>Service unit files may include [Unit] and [Install] sections, which are described in 138 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. 139 </para> 140 141 <para>Service unit files must include a [Service] 142 section, which carries information about the service and the 143 process it supervises. A number of options that may be used in 144 this section are shared with other unit types. These options are 145 documented in 146 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 147 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> 148 and 149 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>. 150 The options specific to the [Service] section 151 of service units are the following:</para> 152 153 <variablelist class='unit-directives'> 154 <varlistentry> 155 <term><varname>Type=</varname></term> 156 157 <listitem> 158 <para>Configures the process start-up type for this service unit. One of <option>simple</option>, 159 <option>exec</option>, <option>forking</option>, <option>oneshot</option>, <option>dbus</option>, 160 <option>notify</option> or <option>idle</option>:</para> 161 162 <itemizedlist> 163 <listitem><para>If set to <option>simple</option> (the default if <varname>ExecStart=</varname> is 164 specified but neither <varname>Type=</varname> nor <varname>BusName=</varname> are), the service manager 165 will consider the unit started immediately after the main service process has been forked off. It is 166 expected that the process configured with <varname>ExecStart=</varname> is the main process of the 167 service. In this mode, if the process offers functionality to other processes on the system, its 168 communication channels should be installed before the service is started up (e.g. sockets set up by 169 systemd, via socket activation), as the service manager will immediately proceed starting follow-up units, 170 right after creating the main service process, and before executing the service's binary. Note that this 171 means <command>systemctl start</command> command lines for <option>simple</option> services will report 172 success even if the service's binary cannot be invoked successfully (for example because the selected 173 <varname>User=</varname> doesn't exist, or the service binary is missing).</para></listitem> 174 175 <listitem><para>The <option>exec</option> type is similar to <option>simple</option>, but the service 176 manager will consider the unit started immediately after the main service binary has been executed. The service 177 manager will delay starting of follow-up units until that point. (Or in other words: 178 <option>simple</option> proceeds with further jobs right after <function>fork()</function> returns, while 179 <option>exec</option> will not proceed before both <function>fork()</function> and 180 <function>execve()</function> in the service process succeeded.) Note that this means <command>systemctl 181 start</command> command lines for <option>exec</option> services will report failure when the service's 182 binary cannot be invoked successfully (for example because the selected <varname>User=</varname> doesn't 183 exist, or the service binary is missing).</para></listitem> 184 185 <listitem><para>If set to <option>forking</option>, it is expected that the process configured with 186 <varname>ExecStart=</varname> will call <function>fork()</function> as part of its start-up. The parent 187 process is expected to exit when start-up is complete and all communication channels are set up. The child 188 continues to run as the main service process, and the service manager will consider the unit started when 189 the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is 190 recommended to also use the <varname>PIDFile=</varname> option, so that systemd can reliably identify the 191 main process of the service. systemd will proceed with starting follow-up units as soon as the parent 192 process exits.</para></listitem> 193 194 <listitem><para>Behavior of <option>oneshot</option> is similar to <option>simple</option>; 195 however, the service manager will consider the unit up after the main process exits. It will then 196 start follow-up units. <varname>RemainAfterExit=</varname> is particularly useful for this type 197 of service. <varname>Type=</varname><option>oneshot</option> is the implied default if neither 198 <varname>Type=</varname> nor <varname>ExecStart=</varname> are specified. Note that if this 199 option is used without <varname>RemainAfterExit=</varname> the service will never enter 200 <literal>active</literal> unit state, but directly transition from <literal>activating</literal> 201 to <literal>deactivating</literal> or <literal>dead</literal> since no process is configured that 202 shall run continuously. In particular this means that after a service of this type ran (and which 203 has <varname>RemainAfterExit=</varname> not set) it will not show up as started afterwards, but 204 as dead.</para></listitem> 205 206 <listitem><para>Behavior of <option>dbus</option> is similar to <option>simple</option>; however, 207 it is expected that the service acquires a name on the D-Bus bus, as configured by 208 <varname>BusName=</varname>. systemd will proceed with starting follow-up units after the D-Bus 209 bus name has been acquired. Service units with this option configured implicitly gain 210 dependencies on the <filename>dbus.socket</filename> unit. This type is the default if 211 <varname>BusName=</varname> is specified. A service unit of this type is considered to be in the 212 activating state until the specified bus name is acquired. It is considered activated while the 213 bus name is taken. Once the bus name is released the service is considered being no longer 214 functional which has the effect that the service manager attempts to terminate any remaining 215 processes belonging to the service. Services that drop their bus name as part of their shutdown 216 logic thus should be prepared to receive a <constant>SIGTERM</constant> (or whichever signal is 217 configured in <varname>KillSignal=</varname>) as result.</para></listitem> 218 219 <listitem><para>Behavior of <option>notify</option> is similar to <option>exec</option>; however, it is 220 expected that the service sends a notification message via 221 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> or an 222 equivalent call when it has finished starting up. systemd will proceed with starting follow-up units after 223 this notification message has been sent. If this option is used, <varname>NotifyAccess=</varname> (see 224 below) should be set to open access to the notification socket provided by systemd. If 225 <varname>NotifyAccess=</varname> is missing or set to <option>none</option>, it will be forcibly set to 226 <option>main</option>.</para></listitem> 227 228 <listitem><para>Behavior of <option>idle</option> is very similar to <option>simple</option>; however, 229 actual execution of the service program is delayed until all active jobs are dispatched. This may be used 230 to avoid interleaving of output of shell services with the status output on the console. Note that this 231 type is useful only to improve console output, it is not useful as a general unit ordering tool, and the 232 effect of this service type is subject to a 5s timeout, after which the service program is invoked 233 anyway.</para></listitem> 234 </itemizedlist> 235 236 <para>It is generally recommended to use <varname>Type=</varname><option>simple</option> for long-running 237 services whenever possible, as it is the simplest and fastest option. However, as this service type won't 238 propagate service start-up failures and doesn't allow ordering of other units against completion of 239 initialization of the service (which for example is useful if clients need to connect to the service through 240 some form of IPC, and the IPC channel is only established by the service itself — in contrast to doing this 241 ahead of time through socket or bus activation or similar), it might not be sufficient for many cases. If so, 242 <option>notify</option> or <option>dbus</option> (the latter only in case the service provides a D-Bus 243 interface) are the preferred options as they allow service program code to precisely schedule when to 244 consider the service started up successfully and when to proceed with follow-up units. The 245 <option>notify</option> service type requires explicit support in the service codebase (as 246 <function>sd_notify()</function> or an equivalent API needs to be invoked by the service at the appropriate 247 time) — if it's not supported, then <option>forking</option> is an alternative: it supports the traditional 248 UNIX service start-up protocol. Finally, <option>exec</option> might be an option for cases where it is 249 enough to ensure the service binary is invoked, and where the service binary itself executes no or little 250 initialization on its own (and its initialization is unlikely to fail). Note that using any type other than 251 <option>simple</option> possibly delays the boot process, as the service manager needs to wait for service 252 initialization to complete. It is hence recommended not to needlessly use any types other than 253 <option>simple</option>. (Also note it is generally not recommended to use <option>idle</option> or 254 <option>oneshot</option> for long-running services.)</para> 255 </listitem> 256 </varlistentry> 257 258 <varlistentry> 259 <term><varname>ExitType=</varname></term> 260 261 <listitem> 262 <para>Specifies when the manager should consider the service to be finished. One of <option>main</option> or 263 <option>cgroup</option>:</para> 264 265 <itemizedlist> 266 <listitem><para>If set to <option>main</option> (the default), the service manager 267 will consider the unit stopped when the main process, which is determined according to the 268 <varname>Type=</varname>, exits. Consequently, it cannot be used with 269 <varname>Type=</varname><option>oneshot</option>.</para></listitem> 270 271 <listitem><para>If set to <option>cgroup</option>, the service will be considered running as long as at 272 least one process in the cgroup has not exited.</para></listitem> 273 </itemizedlist> 274 275 <para>It is generally recommended to use <varname>ExitType=</varname><option>main</option> when a service has 276 a known forking model and a main process can reliably be determined. <varname>ExitType=</varname> 277 <option>cgroup</option> is meant for applications whose forking model is not known ahead of time and which 278 might not have a specific main process. It is well suited for transient or automatically generated services, 279 such as graphical applications inside of a desktop environment.</para> 280 </listitem> 281 </varlistentry> 282 283 <varlistentry> 284 <term><varname>RemainAfterExit=</varname></term> 285 286 <listitem><para>Takes a boolean value that specifies whether 287 the service shall be considered active even when all its 288 processes exited. Defaults to <option>no</option>.</para> 289 </listitem> 290 </varlistentry> 291 292 <varlistentry> 293 <term><varname>GuessMainPID=</varname></term> 294 295 <listitem><para>Takes a boolean value that specifies whether 296 systemd should try to guess the main PID of a service if it 297 cannot be determined reliably. This option is ignored unless 298 <option>Type=forking</option> is set and 299 <option>PIDFile=</option> is unset because for the other types 300 or with an explicitly configured PID file, the main PID is 301 always known. The guessing algorithm might come to incorrect 302 conclusions if a daemon consists of more than one process. If 303 the main PID cannot be determined, failure detection and 304 automatic restarting of a service will not work reliably. 305 Defaults to <option>yes</option>.</para> 306 </listitem> 307 </varlistentry> 308 309 <varlistentry> 310 <term><varname>PIDFile=</varname></term> 311 312 <listitem><para>Takes a path referring to the PID file of the service. Usage of this option is recommended for 313 services where <varname>Type=</varname> is set to <option>forking</option>. The path specified typically points 314 to a file below <filename>/run/</filename>. If a relative path is specified it is hence prefixed with 315 <filename>/run/</filename>. The service manager will read the PID of the main process of the service from this 316 file after start-up of the service. The service manager will not write to the file configured here, although it 317 will remove the file after the service has shut down if it still exists. The PID file does not need to be owned 318 by a privileged user, but if it is owned by an unprivileged user additional safety restrictions are enforced: 319 the file may not be a symlink to a file owned by a different user (neither directly nor indirectly), and the 320 PID file must refer to a process already belonging to the service.</para> 321 322 <para>Note that PID files should be avoided in modern projects. Use <option>Type=notify</option> or 323 <option>Type=simple</option> where possible, which does not require use of PID files to determine the 324 main process of a service and avoids needless forking.</para></listitem> 325 </varlistentry> 326 327 <varlistentry> 328 <term><varname>BusName=</varname></term> 329 330 <listitem><para>Takes a D-Bus destination name that this service shall use. This option is mandatory 331 for services where <varname>Type=</varname> is set to <option>dbus</option>. It is recommended to 332 always set this property if known to make it easy to map the service name to the D-Bus destination. 333 In particular, <command>systemctl service-log-level/service-log-target</command> verbs make use of 334 this.</para> 335 </listitem> 336 </varlistentry> 337 338 <varlistentry> 339 <term><varname>ExecStart=</varname></term> 340 <listitem><para>Commands with their arguments that are 341 executed when this service is started. The value is split into 342 zero or more command lines according to the rules described 343 below (see section "Command Lines" below). 344 </para> 345 346 <para>Unless <varname>Type=</varname> is <option>oneshot</option>, exactly one command must be given. When 347 <varname>Type=oneshot</varname> is used, zero or more commands may be specified. Commands may be specified by 348 providing multiple command lines in the same directive, or alternatively, this directive may be specified more 349 than once with the same effect. If the empty string is assigned to this option, the list of commands to start 350 is reset, prior assignments of this option will have no effect. If no <varname>ExecStart=</varname> is 351 specified, then the service must have <varname>RemainAfterExit=yes</varname> and at least one 352 <varname>ExecStop=</varname> line set. (Services lacking both <varname>ExecStart=</varname> and 353 <varname>ExecStop=</varname> are not valid.)</para> 354 355 <para>For each of the specified commands, the first argument must be either an absolute path to an executable 356 or a simple file name without any slashes. Optionally, this filename may be prefixed with a number of special 357 characters:</para> 358 359 <table> 360 <title>Special executable prefixes</title> 361 362 <tgroup cols='2'> 363 <colspec colname='prefix'/> 364 <colspec colname='meaning'/> 365 366 <thead> 367 <row> 368 <entry>Prefix</entry> 369 <entry>Effect</entry> 370 </row> 371 </thead> 372 <tbody> 373 <row> 374 <entry><literal>@</literal></entry> 375 <entry>If the executable path is prefixed with <literal>@</literal>, the second specified token will be passed as <literal>argv[0]</literal> to the executed process (instead of the actual filename), followed by the further arguments specified.</entry> 376 </row> 377 378 <row> 379 <entry><literal>-</literal></entry> 380 <entry>If the executable path is prefixed with <literal>-</literal>, an exit code of the command normally considered a failure (i.e. non-zero exit status or abnormal exit due to signal) is recorded, but has no further effect and is considered equivalent to success.</entry> 381 </row> 382 383 <row> 384 <entry><literal>:</literal></entry> 385 <entry>If the executable path is prefixed with <literal>:</literal>, environment variable substitution (as described by the "Command Lines" section below) is not applied.</entry> 386 </row> 387 388 <row> 389 <entry><literal>+</literal></entry> 390 <entry>If the executable path is prefixed with <literal>+</literal> then the process is executed with full privileges. In this mode privilege restrictions configured with <varname>User=</varname>, <varname>Group=</varname>, <varname>CapabilityBoundingSet=</varname> or the various file system namespacing options (such as <varname>PrivateDevices=</varname>, <varname>PrivateTmp=</varname>) are not applied to the invoked command line (but still affect any other <varname>ExecStart=</varname>, <varname>ExecStop=</varname>, … lines).</entry> 391 </row> 392 393 <row> 394 <entry><literal>!</literal></entry> 395 396 <entry>Similar to the <literal>+</literal> character discussed above this permits invoking command lines with elevated privileges. However, unlike <literal>+</literal> the <literal>!</literal> character exclusively alters the effect of <varname>User=</varname>, <varname>Group=</varname> and <varname>SupplementaryGroups=</varname>, i.e. only the stanzas that affect user and group credentials. Note that this setting may be combined with <varname>DynamicUser=</varname>, in which case a dynamic user/group pair is allocated before the command is invoked, but credential changing is left to the executed process itself.</entry> 397 </row> 398 399 <row> 400 <entry><literal>!!</literal></entry> 401 402 <entry>This prefix is very similar to <literal>!</literal>, however it only has an effect on systems lacking support for ambient process capabilities, i.e. without support for <varname>AmbientCapabilities=</varname>. It's intended to be used for unit files that take benefit of ambient capabilities to run processes with minimal privileges wherever possible while remaining compatible with systems that lack ambient capabilities support. Note that when <literal>!!</literal> is used, and a system lacking ambient capability support is detected any configured <varname>SystemCallFilter=</varname> and <varname>CapabilityBoundingSet=</varname> stanzas are implicitly modified, in order to permit spawned processes to drop credentials and capabilities themselves, even if this is configured to not be allowed. Moreover, if this prefix is used and a system lacking ambient capability support is detected <varname>AmbientCapabilities=</varname> will be skipped and not be applied. On systems supporting ambient capabilities, <literal>!!</literal> has no effect and is redundant.</entry> 403 </row> 404 </tbody> 405 </tgroup> 406 </table> 407 408 <para><literal>@</literal>, <literal>-</literal>, <literal>:</literal>, and one of 409 <literal>+</literal>/<literal>!</literal>/<literal>!!</literal> may be used together and they can appear in any 410 order. However, only one of <literal>+</literal>, <literal>!</literal>, <literal>!!</literal> may be used at a 411 time. Note that these prefixes are also supported for the other command line settings, 412 i.e. <varname>ExecStartPre=</varname>, <varname>ExecStartPost=</varname>, <varname>ExecReload=</varname>, 413 <varname>ExecStop=</varname> and <varname>ExecStopPost=</varname>.</para> 414 415 <para>If more than one command is specified, the commands are 416 invoked sequentially in the order they appear in the unit 417 file. If one of the commands fails (and is not prefixed with 418 <literal>-</literal>), other lines are not executed, and the 419 unit is considered failed.</para> 420 421 <para>Unless <varname>Type=forking</varname> is set, the 422 process started via this command line will be considered the 423 main process of the daemon.</para> 424 </listitem> 425 </varlistentry> 426 427 <varlistentry> 428 <term><varname>ExecStartPre=</varname></term> 429 <term><varname>ExecStartPost=</varname></term> 430 <listitem><para>Additional commands that are executed before 431 or after the command in <varname>ExecStart=</varname>, 432 respectively. Syntax is the same as for 433 <varname>ExecStart=</varname>, except that multiple command 434 lines are allowed and the commands are executed one after the 435 other, serially.</para> 436 437 <para>If any of those commands (not prefixed with 438 <literal>-</literal>) fail, the rest are not executed and the 439 unit is considered failed.</para> 440 441 <para><varname>ExecStart=</varname> commands are only run after 442 all <varname>ExecStartPre=</varname> commands that were not prefixed 443 with a <literal>-</literal> exit successfully.</para> 444 445 <para><varname>ExecStartPost=</varname> commands are only run after the commands specified in 446 <varname>ExecStart=</varname> have been invoked successfully, as determined by <varname>Type=</varname> 447 (i.e. the process has been started for <varname>Type=simple</varname> or <varname>Type=idle</varname>, the last 448 <varname>ExecStart=</varname> process exited successfully for <varname>Type=oneshot</varname>, the initial 449 process exited successfully for <varname>Type=forking</varname>, <literal>READY=1</literal> is sent for 450 <varname>Type=notify</varname>, or the <varname>BusName=</varname> has been taken for 451 <varname>Type=dbus</varname>).</para> 452 453 <para>Note that <varname>ExecStartPre=</varname> may not be 454 used to start long-running processes. All processes forked 455 off by processes invoked via <varname>ExecStartPre=</varname> will 456 be killed before the next service process is run.</para> 457 458 <para>Note that if any of the commands specified in <varname>ExecStartPre=</varname>, 459 <varname>ExecStart=</varname>, or <varname>ExecStartPost=</varname> fail (and are not prefixed with 460 <literal>-</literal>, see above) or time out before the service is fully up, execution continues with commands 461 specified in <varname>ExecStopPost=</varname>, the commands in <varname>ExecStop=</varname> are skipped.</para> 462 463 <para>Note that the execution of <varname>ExecStartPost=</varname> is taken into account for the purpose of 464 <varname>Before=</varname>/<varname>After=</varname> ordering constraints.</para> 465 </listitem> 466 </varlistentry> 467 468 <varlistentry> 469 <term><varname>ExecCondition=</varname></term> 470 <listitem><para>Optional commands that are executed before the command(s) in <varname>ExecStartPre=</varname>. 471 Syntax is the same as for <varname>ExecStart=</varname>, except that multiple command lines are allowed and the 472 commands are executed one after the other, serially.</para> 473 474 <para>The behavior is like an <varname>ExecStartPre=</varname> and condition check hybrid: when an 475 <varname>ExecCondition=</varname> command exits with exit code 1 through 254 (inclusive), the remaining 476 commands are skipped and the unit is <emphasis>not</emphasis> marked as failed. However, if an 477 <varname>ExecCondition=</varname> command exits with 255 or abnormally (e.g. timeout, killed by a 478 signal, etc.), the unit will be considered failed (and remaining commands will be skipped). Exit code of 0 or 479 those matching <varname>SuccessExitStatus=</varname> will continue execution to the next command(s).</para> 480 481 <para>The same recommendations about not running long-running processes in <varname>ExecStartPre=</varname> 482 also applies to <varname>ExecCondition=</varname>. <varname>ExecCondition=</varname> will also run the commands 483 in <varname>ExecStopPost=</varname>, as part of stopping the service, in the case of any non-zero or abnormal 484 exits, like the ones described above.</para> 485 </listitem> 486 </varlistentry> 487 488 <varlistentry> 489 <term><varname>ExecReload=</varname></term> 490 <listitem><para>Commands to execute to trigger a configuration 491 reload in the service. This argument takes multiple command 492 lines, following the same scheme as described for 493 <varname>ExecStart=</varname> above. Use of this setting is 494 optional. Specifier and environment variable substitution is 495 supported here following the same scheme as for 496 <varname>ExecStart=</varname>.</para> 497 498 <para>One additional, special environment variable is set: if 499 known, <varname>$MAINPID</varname> is set to the main process 500 of the daemon, and may be used for command lines like the 501 following:</para> 502 503 <programlisting>ExecReload=kill -HUP $MAINPID</programlisting> 504 505 <para>Note however that reloading a daemon by sending a signal 506 (as with the example line above) is usually not a good choice, 507 because this is an asynchronous operation and hence not 508 suitable to order reloads of multiple services against each 509 other. It is strongly recommended to set 510 <varname>ExecReload=</varname> to a command that not only 511 triggers a configuration reload of the daemon, but also 512 synchronously waits for it to complete. For example, 513 <citerefentry project='mankier'><refentrytitle>dbus-broker</refentrytitle><manvolnum>1</manvolnum></citerefentry> 514 uses the following:</para> 515 516 <programlisting>ExecReload=busctl call org.freedesktop.DBus \ 517 /org/freedesktop/DBus org.freedesktop.DBus \ 518 ReloadConfig 519</programlisting> 520 </listitem> 521 </varlistentry> 522 523 <varlistentry> 524 <term><varname>ExecStop=</varname></term> 525 <listitem><para>Commands to execute to stop the service started via 526 <varname>ExecStart=</varname>. This argument takes multiple command lines, following the same scheme 527 as described for <varname>ExecStart=</varname> above. Use of this setting is optional. After the 528 commands configured in this option are run, it is implied that the service is stopped, and any 529 processes remaining for it are terminated according to the <varname>KillMode=</varname> setting (see 530 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). 531 If this option is not specified, the process is terminated by sending the signal specified in 532 <varname>KillSignal=</varname> or <varname>RestartKillSignal=</varname> when service stop is 533 requested. Specifier and environment variable substitution is supported (including 534 <varname>$MAINPID</varname>, see above).</para> 535 536 <para>Note that it is usually not sufficient to specify a command for this setting that only asks the 537 service to terminate (for example, by sending some form of termination signal to it), but does not 538 wait for it to do so. Since the remaining processes of the services are killed according to 539 <varname>KillMode=</varname> and <varname>KillSignal=</varname> or 540 <varname>RestartKillSignal=</varname> as described above immediately after the command exited, this 541 may not result in a clean stop. The specified command should hence be a synchronous operation, not an 542 asynchronous one.</para> 543 544 <para>Note that the commands specified in <varname>ExecStop=</varname> are only executed when the service 545 started successfully first. They are not invoked if the service was never started at all, or in case its 546 start-up failed, for example because any of the commands specified in <varname>ExecStart=</varname>, 547 <varname>ExecStartPre=</varname> or <varname>ExecStartPost=</varname> failed (and weren't prefixed with 548 <literal>-</literal>, see above) or timed out. Use <varname>ExecStopPost=</varname> to invoke commands when a 549 service failed to start up correctly and is shut down again. Also note that the stop operation is always 550 performed if the service started successfully, even if the processes in the service terminated on their 551 own or were killed. The stop commands must be prepared to deal with that case. <varname>$MAINPID</varname> 552 will be unset if systemd knows that the main process exited by the time the stop commands are called.</para> 553 554 <para>Service restart requests are implemented as stop operations followed by start operations. This 555 means that <varname>ExecStop=</varname> and <varname>ExecStopPost=</varname> are executed during a 556 service restart operation.</para> 557 558 <para>It is recommended to use this setting for commands that communicate with the service requesting 559 clean termination. For post-mortem clean-up steps use <varname>ExecStopPost=</varname> instead. 560 </para></listitem> 561 </varlistentry> 562 563 <varlistentry> 564 <term><varname>ExecStopPost=</varname></term> 565 <listitem><para>Additional commands that are executed after the service is stopped. This includes cases where 566 the commands configured in <varname>ExecStop=</varname> were used, where the service does not have any 567 <varname>ExecStop=</varname> defined, or where the service exited unexpectedly. This argument takes multiple 568 command lines, following the same scheme as described for <varname>ExecStart=</varname>. Use of these settings 569 is optional. Specifier and environment variable substitution is supported. Note that – unlike 570 <varname>ExecStop=</varname> – commands specified with this setting are invoked when a service failed to start 571 up correctly and is shut down again.</para> 572 573 <para>It is recommended to use this setting for clean-up operations that shall be executed even when the 574 service failed to start up correctly. Commands configured with this setting need to be able to operate even if 575 the service failed starting up half-way and left incompletely initialized data around. As the service's 576 processes have been terminated already when the commands specified with this setting are executed they should 577 not attempt to communicate with them.</para> 578 579 <para>Note that all commands that are configured with this setting are invoked with the result code of the 580 service, as well as the main process' exit code and status, set in the <varname>$SERVICE_RESULT</varname>, 581 <varname>$EXIT_CODE</varname> and <varname>$EXIT_STATUS</varname> environment variables, see 582 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for 583 details.</para> 584 585 <para>Note that the execution of <varname>ExecStopPost=</varname> is taken into account for the purpose of 586 <varname>Before=</varname>/<varname>After=</varname> ordering constraints.</para></listitem> 587 </varlistentry> 588 589 <varlistentry> 590 <term><varname>RestartSec=</varname></term> 591 <listitem><para>Configures the time to sleep before restarting 592 a service (as configured with <varname>Restart=</varname>). 593 Takes a unit-less value in seconds, or a time span value such 594 as "5min 20s". Defaults to 100ms.</para></listitem> 595 </varlistentry> 596 597 <varlistentry> 598 <term><varname>TimeoutStartSec=</varname></term> 599 <listitem><para>Configures the time to wait for start-up. If a daemon service does not signal start-up 600 completion within the configured time, the service will be considered failed and will be shut down again. The 601 precise action depends on the <varname>TimeoutStartFailureMode=</varname> option. Takes a unit-less value in 602 seconds, or a time span value such as "5min 20s". Pass <literal>infinity</literal> to disable the timeout logic. 603 Defaults to <varname>DefaultTimeoutStartSec=</varname> from the manager configuration file, except when 604 <varname>Type=oneshot</varname> is used, in which case the timeout is disabled by default (see 605 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>). 606 </para> 607 608 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause 609 the start time to be extended beyond <varname>TimeoutStartSec=</varname>. The first receipt of this message 610 must occur before <varname>TimeoutStartSec=</varname> is exceeded, and once the start time has extended beyond 611 <varname>TimeoutStartSec=</varname>, the service manager will allow the service to continue to start, provided 612 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified until the service 613 startup status is finished by <literal>READY=1</literal>. (see 614 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>). 615 </para></listitem> 616 </varlistentry> 617 618 <varlistentry> 619 <term><varname>TimeoutStopSec=</varname></term> 620 <listitem><para>This option serves two purposes. First, it configures the time to wait for each 621 <varname>ExecStop=</varname> command. If any of them times out, subsequent <varname>ExecStop=</varname> commands 622 are skipped and the service will be terminated by <constant>SIGTERM</constant>. If no <varname>ExecStop=</varname> 623 commands are specified, the service gets the <constant>SIGTERM</constant> immediately. This default behavior 624 can be changed by the <varname>TimeoutStopFailureMode=</varname> option. Second, it configures the time 625 to wait for the service itself to stop. If it doesn't terminate in the specified time, it will be forcibly terminated 626 by <constant>SIGKILL</constant> (see <varname>KillMode=</varname> in 627 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). 628 Takes a unit-less value in seconds, or a time span value such 629 as "5min 20s". Pass <literal>infinity</literal> to disable the 630 timeout logic. Defaults to 631 <varname>DefaultTimeoutStopSec=</varname> from the manager 632 configuration file (see 633 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>). 634 </para> 635 636 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause 637 the stop time to be extended beyond <varname>TimeoutStopSec=</varname>. The first receipt of this message 638 must occur before <varname>TimeoutStopSec=</varname> is exceeded, and once the stop time has extended beyond 639 <varname>TimeoutStopSec=</varname>, the service manager will allow the service to continue to stop, provided 640 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified, or terminates itself 641 (see <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>). 642 </para></listitem> 643 </varlistentry> 644 645 <varlistentry> 646 <term><varname>TimeoutAbortSec=</varname></term> 647 <listitem><para>This option configures the time to wait for the service to terminate when it was aborted due to a 648 watchdog timeout (see <varname>WatchdogSec=</varname>). If the service has a short <varname>TimeoutStopSec=</varname> 649 this option can be used to give the system more time to write a core dump of the service. Upon expiration the service 650 will be forcibly terminated by <constant>SIGKILL</constant> (see <varname>KillMode=</varname> in 651 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). The core file will 652 be truncated in this case. Use <varname>TimeoutAbortSec=</varname> to set a sensible timeout for the core dumping per 653 service that is large enough to write all expected data while also being short enough to handle the service failure 654 in due time. 655 </para> 656 657 <para>Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass an empty value to skip 658 the dedicated watchdog abort timeout handling and fall back <varname>TimeoutStopSec=</varname>. Pass 659 <literal>infinity</literal> to disable the timeout logic. Defaults to <varname>DefaultTimeoutAbortSec=</varname> from 660 the manager configuration file (see 661 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>). 662 </para> 663 664 <para>If a service of <varname>Type=notify</varname> handles <constant>SIGABRT</constant> itself (instead of relying 665 on the kernel to write a core dump) it can send <literal>EXTEND_TIMEOUT_USEC=…</literal> to 666 extended the abort time beyond <varname>TimeoutAbortSec=</varname>. The first receipt of this message 667 must occur before <varname>TimeoutAbortSec=</varname> is exceeded, and once the abort time has extended beyond 668 <varname>TimeoutAbortSec=</varname>, the service manager will allow the service to continue to abort, provided 669 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified, or terminates itself 670 (see <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>). 671 </para></listitem> 672 </varlistentry> 673 674 <varlistentry> 675 <term><varname>TimeoutSec=</varname></term> 676 <listitem><para>A shorthand for configuring both 677 <varname>TimeoutStartSec=</varname> and 678 <varname>TimeoutStopSec=</varname> to the specified value. 679 </para></listitem> 680 </varlistentry> 681 682 <varlistentry> 683 <term><varname>TimeoutStartFailureMode=</varname></term> 684 <term><varname>TimeoutStopFailureMode=</varname></term> 685 686 <listitem><para>These options configure the action that is taken in case a daemon service does not signal 687 start-up within its configured <varname>TimeoutStartSec=</varname>, respectively if it does not stop within 688 <varname>TimeoutStopSec=</varname>. Takes one of <option>terminate</option>, <option>abort</option> and 689 <option>kill</option>. Both options default to <option>terminate</option>.</para> 690 691 <para>If <option>terminate</option> is set the service will be gracefully terminated by sending the signal 692 specified in <varname>KillSignal=</varname> (defaults to <constant>SIGTERM</constant>, see 693 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If the 694 service does not terminate the <varname>FinalKillSignal=</varname> is sent after 695 <varname>TimeoutStopSec=</varname>. If <option>abort</option> is set, <varname>WatchdogSignal=</varname> is sent 696 instead and <varname>TimeoutAbortSec=</varname> applies before sending <varname>FinalKillSignal=</varname>. 697 This setting may be used to analyze services that fail to start-up or shut-down intermittently. 698 By using <option>kill</option> the service is immediately terminated by sending 699 <varname>FinalKillSignal=</varname> without any further timeout. This setting can be used to expedite the 700 shutdown of failing services. 701 </para></listitem> 702 </varlistentry> 703 704 <varlistentry> 705 <term><varname>RuntimeMaxSec=</varname></term> 706 707 <listitem><para>Configures a maximum time for the service to run. If this is used and the service has been 708 active for longer than the specified time it is terminated and put into a failure state. Note that this setting 709 does not have any effect on <varname>Type=oneshot</varname> services, as they terminate immediately after 710 activation completed. Pass <literal>infinity</literal> (the default) to configure no runtime 711 limit.</para> 712 713 <para>If a service of <varname>Type=notify</varname> sends <literal>EXTEND_TIMEOUT_USEC=…</literal>, this may cause 714 the runtime to be extended beyond <varname>RuntimeMaxSec=</varname>. The first receipt of this message 715 must occur before <varname>RuntimeMaxSec=</varname> is exceeded, and once the runtime has extended beyond 716 <varname>RuntimeMaxSec=</varname>, the service manager will allow the service to continue to run, provided 717 the service repeats <literal>EXTEND_TIMEOUT_USEC=…</literal> within the interval specified until the service 718 shutdown is achieved by <literal>STOPPING=1</literal> (or termination). (see 719 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>). 720 </para></listitem> 721 </varlistentry> 722 723 <varlistentry> 724 <term><varname>RuntimeRandomizedExtraSec=</varname></term> 725 726 <listitem><para>This option modifies <varname>RuntimeMaxSec=</varname> by increasing the maximum runtime by an 727 evenly distributed duration between 0 and the specified value (in seconds). If <varname>RuntimeMaxSec=</varname> is 728 unspecified, then this feature will be disabled. 729 </para></listitem> 730 </varlistentry> 731 732 <varlistentry> 733 <term><varname>WatchdogSec=</varname></term> 734 <listitem><para>Configures the watchdog timeout for a service. 735 The watchdog is activated when the start-up is completed. The 736 service must call 737 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> 738 regularly with <literal>WATCHDOG=1</literal> (i.e. the 739 "keep-alive ping"). If the time between two such calls is 740 larger than the configured time, then the service is placed in 741 a failed state and it will be terminated with 742 <constant>SIGABRT</constant> (or the signal specified by 743 <varname>WatchdogSignal=</varname>). By setting 744 <varname>Restart=</varname> to <option>on-failure</option>, 745 <option>on-watchdog</option>, <option>on-abnormal</option> or 746 <option>always</option>, the service will be automatically 747 restarted. The time configured here will be passed to the 748 executed service process in the 749 <varname>WATCHDOG_USEC=</varname> environment variable. This 750 allows daemons to automatically enable the keep-alive pinging 751 logic if watchdog support is enabled for the service. If this 752 option is used, <varname>NotifyAccess=</varname> (see below) 753 should be set to open access to the notification socket 754 provided by systemd. If <varname>NotifyAccess=</varname> is 755 not set, it will be implicitly set to <option>main</option>. 756 Defaults to 0, which disables this feature. The service can 757 check whether the service manager expects watchdog keep-alive 758 notifications. See 759 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry> 760 for details. 761 <citerefentry><refentrytitle>sd_event_set_watchdog</refentrytitle><manvolnum>3</manvolnum></citerefentry> 762 may be used to enable automatic watchdog notification support. 763 </para></listitem> 764 </varlistentry> 765 766 <varlistentry> 767 <term><varname>Restart=</varname></term> 768 <listitem><para>Configures whether the service shall be 769 restarted when the service process exits, is killed, or a 770 timeout is reached. The service process may be the main 771 service process, but it may also be one of the processes 772 specified with <varname>ExecStartPre=</varname>, 773 <varname>ExecStartPost=</varname>, 774 <varname>ExecStop=</varname>, 775 <varname>ExecStopPost=</varname>, or 776 <varname>ExecReload=</varname>. When the death of the process 777 is a result of systemd operation (e.g. service stop or 778 restart), the service will not be restarted. Timeouts include 779 missing the watchdog "keep-alive ping" deadline and a service 780 start, reload, and stop operation timeouts.</para> 781 782 <para>Takes one of 783 <option>no</option>, 784 <option>on-success</option>, 785 <option>on-failure</option>, 786 <option>on-abnormal</option>, 787 <option>on-watchdog</option>, 788 <option>on-abort</option>, or 789 <option>always</option>. 790 If set to <option>no</option> (the default), the service will 791 not be restarted. If set to <option>on-success</option>, it 792 will be restarted only when the service process exits cleanly. 793 In this context, a clean exit means any of the following: 794 <itemizedlist> 795 <listitem><simpara>exit code of 0;</simpara></listitem> 796 <listitem><simpara>for types other than 797 <varname>Type=oneshot</varname>, one of the signals 798 <constant>SIGHUP</constant>, 799 <constant>SIGINT</constant>, 800 <constant>SIGTERM</constant>, or 801 <constant>SIGPIPE</constant>;</simpara></listitem> 802 <listitem><simpara>exit statuses and signals specified in 803 <varname>SuccessExitStatus=</varname>.</simpara></listitem> 804 </itemizedlist> 805 If set to 806 <option>on-failure</option>, the service will be restarted 807 when the process exits with a non-zero exit code, is 808 terminated by a signal (including on core dump, but excluding 809 the aforementioned four signals), when an operation (such as 810 service reload) times out, and when the configured watchdog 811 timeout is triggered. If set to <option>on-abnormal</option>, 812 the service will be restarted when the process is terminated 813 by a signal (including on core dump, excluding the 814 aforementioned four signals), when an operation times out, or 815 when the watchdog timeout is triggered. If set to 816 <option>on-abort</option>, the service will be restarted only 817 if the service process exits due to an uncaught signal not 818 specified as a clean exit status. If set to 819 <option>on-watchdog</option>, the service will be restarted 820 only if the watchdog timeout for the service expires. If set 821 to <option>always</option>, the service will be restarted 822 regardless of whether it exited cleanly or not, got terminated 823 abnormally by a signal, or hit a timeout.</para> 824 825 <table> 826 <title>Exit causes and the effect of the <varname>Restart=</varname> settings</title> 827 828 <tgroup cols='2'> 829 <colspec colname='path' /> 830 <colspec colname='expl' /> 831 <thead> 832 <row> 833 <entry>Restart settings/Exit causes</entry> 834 <entry><option>no</option></entry> 835 <entry><option>always</option></entry> 836 <entry><option>on-success</option></entry> 837 <entry><option>on-failure</option></entry> 838 <entry><option>on-abnormal</option></entry> 839 <entry><option>on-abort</option></entry> 840 <entry><option>on-watchdog</option></entry> 841 </row> 842 </thead> 843 <tbody> 844 <row> 845 <entry>Clean exit code or signal</entry> 846 <entry/> 847 <entry>X</entry> 848 <entry>X</entry> 849 <entry/> 850 <entry/> 851 <entry/> 852 <entry/> 853 </row> 854 <row> 855 <entry>Unclean exit code</entry> 856 <entry/> 857 <entry>X</entry> 858 <entry/> 859 <entry>X</entry> 860 <entry/> 861 <entry/> 862 <entry/> 863 </row> 864 <row> 865 <entry>Unclean signal</entry> 866 <entry/> 867 <entry>X</entry> 868 <entry/> 869 <entry>X</entry> 870 <entry>X</entry> 871 <entry>X</entry> 872 <entry/> 873 </row> 874 <row> 875 <entry>Timeout</entry> 876 <entry/> 877 <entry>X</entry> 878 <entry/> 879 <entry>X</entry> 880 <entry>X</entry> 881 <entry/> 882 <entry/> 883 </row> 884 <row> 885 <entry>Watchdog</entry> 886 <entry/> 887 <entry>X</entry> 888 <entry/> 889 <entry>X</entry> 890 <entry>X</entry> 891 <entry/> 892 <entry>X</entry> 893 </row> 894 </tbody> 895 </tgroup> 896 </table> 897 898 <para>As exceptions to the setting above, the service will not 899 be restarted if the exit code or signal is specified in 900 <varname>RestartPreventExitStatus=</varname> (see below) or 901 the service is stopped with <command>systemctl stop</command> 902 or an equivalent operation. Also, the services will always be 903 restarted if the exit code or signal is specified in 904 <varname>RestartForceExitStatus=</varname> (see below).</para> 905 906 <para>Note that service restart is subject to unit start rate 907 limiting configured with <varname>StartLimitIntervalSec=</varname> 908 and <varname>StartLimitBurst=</varname>, see 909 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> 910 for details. A restarted service enters the failed state only 911 after the start limits are reached.</para> 912 913 <para>Setting this to <option>on-failure</option> is the 914 recommended choice for long-running services, in order to 915 increase reliability by attempting automatic recovery from 916 errors. For services that shall be able to terminate on their 917 own choice (and avoid immediate restarting), 918 <option>on-abnormal</option> is an alternative choice.</para> 919 </listitem> 920 </varlistentry> 921 922 <varlistentry> 923 <term><varname>SuccessExitStatus=</varname></term> 924 925 <listitem><para>Takes a list of exit status definitions that, when returned by the main service 926 process, will be considered successful termination, in addition to the normal successful exit status 927 0 and, except for <varname>Type=oneshot</varname>, the signals <constant>SIGHUP</constant>, <constant>SIGINT</constant>, 928 <constant>SIGTERM</constant>, and <constant>SIGPIPE</constant>. Exit status definitions can be 929 numeric termination statuses, termination status names, or termination signal names, separated by 930 spaces. See the Process Exit Codes section in 931 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for 932 a list of termination status names (for this setting only the part without the 933 <literal>EXIT_</literal> or <literal>EX_</literal> prefix should be used). See <citerefentry 934 project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry> for 935 a list of signal names.</para> 936 937 <para>Note that this setting does not change the mapping between numeric exit statuses and their 938 names, i.e. regardless how this setting is used 0 will still be mapped to <literal>SUCCESS</literal> 939 (and thus typically shown as <literal>0/SUCCESS</literal> in tool outputs) and 1 to 940 <literal>FAILURE</literal> (and thus typically shown as <literal>1/FAILURE</literal>), and so on. It 941 only controls what happens as effect of these exit statuses, and how it propagates to the state of 942 the service as a whole.</para> 943 944 <para>This option may appear more than once, in which case the list of successful exit statuses is 945 merged. If the empty string is assigned to this option, the list is reset, all prior assignments of 946 this option will have no effect.</para> 947 948 <example> 949 <title>A service with the <varname>SuccessExitStatus=</varname> setting</title> 950 951 <programlisting>SuccessExitStatus=TEMPFAIL 250 SIGKILL</programlisting> 952 953 <para>Exit status 75 (<constant>TEMPFAIL</constant>), 250, and the termination signal 954 <constant>SIGKILL</constant> are considered clean service terminations.</para> 955 </example> 956 957 <para>Note: <command>systemd-analyze exit-status</command> may be used to list exit statuses and 958 translate between numerical status values and names.</para></listitem> 959 </varlistentry> 960 961 <varlistentry> 962 <term><varname>RestartPreventExitStatus=</varname></term> 963 964 <listitem><para>Takes a list of exit status definitions that, when returned by the main service 965 process, will prevent automatic service restarts, regardless of the restart setting configured with 966 <varname>Restart=</varname>. Exit status definitions can either be numeric exit codes or termination 967 signal names, and are separated by spaces. Defaults to the empty list, so that, by default, no exit 968 status is excluded from the configured restart logic. For example: 969 970 <programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting> 971 972 ensures that exit codes 1 and 6 and the termination signal <constant>SIGABRT</constant> will not 973 result in automatic service restarting. This option may appear more than once, in which case the list 974 of restart-preventing statuses is merged. If the empty string is assigned to this option, the list is 975 reset and all prior assignments of this option will have no effect.</para> 976 977 <para>Note that this setting has no effect on processes configured via 978 <varname>ExecStartPre=</varname>, <varname>ExecStartPost=</varname>, <varname>ExecStop=</varname>, 979 <varname>ExecStopPost=</varname> or <varname>ExecReload=</varname>, but only on the main service 980 process, i.e. either the one invoked by <varname>ExecStart=</varname> or (depending on 981 <varname>Type=</varname>, <varname>PIDFile=</varname>, …) the otherwise configured main 982 process.</para></listitem> 983 </varlistentry> 984 985 <varlistentry> 986 <term><varname>RestartForceExitStatus=</varname></term> 987 <listitem><para>Takes a list of exit status definitions that, 988 when returned by the main service process, will force automatic 989 service restarts, regardless of the restart setting configured 990 with <varname>Restart=</varname>. The argument format is 991 similar to 992 <varname>RestartPreventExitStatus=</varname>.</para></listitem> 993 </varlistentry> 994 995 <varlistentry> 996 <term><varname>RootDirectoryStartOnly=</varname></term> 997 <listitem><para>Takes a boolean argument. If true, the root 998 directory, as configured with the 999 <varname>RootDirectory=</varname> option (see 1000 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1001 for more information), is only applied to the process started 1002 with <varname>ExecStart=</varname>, and not to the various 1003 other <varname>ExecStartPre=</varname>, 1004 <varname>ExecStartPost=</varname>, 1005 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>, 1006 and <varname>ExecStopPost=</varname> commands. If false, the 1007 setting is applied to all configured commands the same way. 1008 Defaults to false.</para></listitem> 1009 </varlistentry> 1010 1011 <varlistentry> 1012 <term><varname>NonBlocking=</varname></term> 1013 <listitem><para>Set the <constant>O_NONBLOCK</constant> flag for all file descriptors passed via socket-based 1014 activation. If true, all file descriptors >= 3 (i.e. all except stdin, stdout, stderr), excluding those passed 1015 in via the file descriptor storage logic (see <varname>FileDescriptorStoreMax=</varname> for details), will 1016 have the <constant>O_NONBLOCK</constant> flag set and hence are in non-blocking mode. This option is only 1017 useful in conjunction with a socket unit, as described in 1018 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> and has no 1019 effect on file descriptors which were previously saved in the file-descriptor store for example. Defaults to 1020 false.</para></listitem> 1021 </varlistentry> 1022 1023 <varlistentry> 1024 <term><varname>NotifyAccess=</varname></term> 1025 <listitem><para>Controls access to the service status notification socket, as accessible via the 1026 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> call. Takes one 1027 of <option>none</option> (the default), <option>main</option>, <option>exec</option> or 1028 <option>all</option>. If <option>none</option>, no daemon status updates are accepted from the service 1029 processes, all status update messages are ignored. If <option>main</option>, only service updates sent from the 1030 main process of the service are accepted. If <option>exec</option>, only service updates sent from any of the 1031 main or control processes originating from one of the <varname>Exec*=</varname> commands are accepted. If 1032 <option>all</option>, all services updates from all members of the service's control group are accepted. This 1033 option should be set to open access to the notification socket when using <varname>Type=notify</varname> or 1034 <varname>WatchdogSec=</varname> (see above). If those options are used but <varname>NotifyAccess=</varname> is 1035 not configured, it will be implicitly set to <option>main</option>.</para> 1036 1037 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if 1038 either the sending process is still around at the time PID 1 processes the message, or if the sending process 1039 is explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally 1040 forked off the process, i.e. on all processes that match <option>main</option> or 1041 <option>exec</option>. Conversely, if an auxiliary process of the unit sends an 1042 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to 1043 properly attribute the message to the unit, and thus will ignore it, even if 1044 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para> 1045 1046 <para>Hence, to eliminate all race conditions involving lookup of the client's unit and attribution of notifications 1047 to units correctly, <function>sd_notify_barrier()</function> may be used. This call acts as a synchronization point 1048 and ensures all notifications sent before this call have been picked up by the service manager when it returns 1049 successfully. Use of <function>sd_notify_barrier()</function> is needed for clients which are not invoked by the 1050 service manager, otherwise this synchronization mechanism is unnecessary for attribution of notifications to the 1051 unit.</para></listitem> 1052 </varlistentry> 1053 1054 <varlistentry> 1055 <term><varname>Sockets=</varname></term> 1056 <listitem><para>Specifies the name of the socket units this 1057 service shall inherit socket file descriptors from when the 1058 service is started. Normally, it should not be necessary to use 1059 this setting, as all socket file descriptors whose unit shares 1060 the same name as the service (subject to the different unit 1061 name suffix of course) are passed to the spawned 1062 process.</para> 1063 1064 <para>Note that the same socket file descriptors may be passed 1065 to multiple processes simultaneously. Also note that a 1066 different service may be activated on incoming socket traffic 1067 than the one which is ultimately configured to inherit the 1068 socket file descriptors. Or, in other words: the 1069 <varname>Service=</varname> setting of 1070 <filename>.socket</filename> units does not have to match the 1071 inverse of the <varname>Sockets=</varname> setting of the 1072 <filename>.service</filename> it refers to.</para> 1073 1074 <para>This option may appear more than once, in which case the list of socket units is merged. Note 1075 that once set, clearing the list of sockets again (for example, by assigning the empty string to this 1076 option) is not supported.</para></listitem> 1077 </varlistentry> 1078 1079 <varlistentry> 1080 <term><varname>FileDescriptorStoreMax=</varname></term> 1081 <listitem><para>Configure how many file descriptors may be stored in the service manager for the 1082 service using 1083 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s 1084 <literal>FDSTORE=1</literal> messages. This is useful for implementing services that can restart 1085 after an explicit request or a crash without losing state. Any open sockets and other file 1086 descriptors which should not be closed during the restart may be stored this way. Application state 1087 can either be serialized to a file in <filename>/run/</filename>, or better, stored in a 1088 <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> 1089 memory file descriptor. Defaults to 0, i.e. no file descriptors may be stored in the service 1090 manager. All file descriptors passed to the service manager from a specific service are passed back 1091 to the service's main process on the next service restart (see 1092 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry> for 1093 details about the precise protocol used and the order in which the file descriptors are passed). Any 1094 file descriptors passed to the service manager are automatically closed when 1095 <constant>POLLHUP</constant> or <constant>POLLERR</constant> is seen on them, or when the service is 1096 fully stopped and no job is queued or being executed for it. If this option is used, 1097 <varname>NotifyAccess=</varname> (see above) should be set to open access to the notification socket 1098 provided by systemd. If <varname>NotifyAccess=</varname> is not set, it will be implicitly set to 1099 <option>main</option>.</para></listitem> 1100 </varlistentry> 1101 1102 <varlistentry> 1103 <term><varname>USBFunctionDescriptors=</varname></term> 1104 <listitem><para>Configure the location of a file containing 1105 <ulink 1106 url="https://www.kernel.org/doc/Documentation/usb/functionfs.txt">USB 1107 FunctionFS</ulink> descriptors, for implementation of USB 1108 gadget functions. This is used only in conjunction with a 1109 socket unit with <varname>ListenUSBFunction=</varname> 1110 configured. The contents of this file are written to the 1111 <filename>ep0</filename> file after it is 1112 opened.</para></listitem> 1113 </varlistentry> 1114 1115 <varlistentry> 1116 <term><varname>USBFunctionStrings=</varname></term> 1117 <listitem><para>Configure the location of a file containing 1118 USB FunctionFS strings. Behavior is similar to 1119 <varname>USBFunctionDescriptors=</varname> 1120 above.</para></listitem> 1121 </varlistentry> 1122 1123 <varlistentry> 1124 <term><varname>OOMPolicy=</varname></term> 1125 1126 <listitem><para>Configure the out-of-memory (OOM) kernel killer policy. Note that the userspace OOM 1127 killer 1128 <citerefentry><refentrytitle>systemd-oomd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry> 1129 is a more flexible solution that aims to prevent out-of-memory situations for the userspace, not just 1130 the kernel.</para> 1131 1132 <para>On Linux, when memory becomes scarce to the point that the kernel has trouble allocating memory 1133 for itself, it might decide to kill a running process in order to free up memory and reduce memory 1134 pressure. This setting takes one of <constant>continue</constant>, <constant>stop</constant> or 1135 <constant>kill</constant>. If set to <constant>continue</constant> and a process of the service is 1136 killed by the kernel's OOM killer this is logged but the service continues running. If set to 1137 <constant>stop</constant> the event is logged but the service is terminated cleanly by the service 1138 manager. If set to <constant>kill</constant> and one of the service's processes is killed by the OOM 1139 killer the kernel is instructed to kill all remaining processes of the service too, by setting the 1140 <filename>memory.oom.group</filename> attribute to <constant>1</constant>; also see <ulink 1141 url="https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html">kernel documentation</ulink>. 1142 </para> 1143 1144 <para>Defaults to the setting <varname>DefaultOOMPolicy=</varname> in 1145 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1146 is set to, except for services where <varname>Delegate=</varname> is turned on, where it defaults to 1147 <constant>continue</constant>.</para> 1148 1149 <para>Use the <varname>OOMScoreAdjust=</varname> setting to configure whether processes of the unit 1150 shall be considered preferred or less preferred candidates for process termination by the Linux OOM 1151 killer logic. See 1152 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry> for 1153 details.</para> 1154 1155 <para>This setting also applies to <command>systemd-oomd</command>, similar to the kernel OOM kills 1156 this setting determines the state of the service after <command>systemd-oomd</command> kills a cgroup 1157 associated with the service.</para></listitem> 1158 </varlistentry> 1159 1160 </variablelist> 1161 1162 <para id='shared-unit-options'>Check 1163 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1164 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, and 1165 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more 1166 settings.</para> 1167 </refsect1> 1168 1169 <refsect1> 1170 <title>Command lines</title> 1171 1172 <para>This section describes command line parsing and 1173 variable and specifier substitutions for 1174 <varname>ExecStart=</varname>, 1175 <varname>ExecStartPre=</varname>, 1176 <varname>ExecStartPost=</varname>, 1177 <varname>ExecReload=</varname>, 1178 <varname>ExecStop=</varname>, and 1179 <varname>ExecStopPost=</varname> options.</para> 1180 1181 <para>Multiple command lines may be concatenated in a single directive by separating them with semicolons 1182 (these semicolons must be passed as separate words). Lone semicolons may be escaped as 1183 <literal>\;</literal>.</para> 1184 1185 <para>Each command line is unquoted using the rules described in "Quoting" section in 1186 <citerefentry><refentrytitle>systemd.syntax</refentrytitle><manvolnum>7</manvolnum></citerefentry>. The 1187 first item becomes the command to execute, and the subsequent items the arguments.</para> 1188 1189 <para>This syntax is inspired by shell syntax, but only the meta-characters and expansions 1190 described in the following paragraphs are understood, and the expansion of variables is 1191 different. Specifically, redirection using 1192 <literal><</literal>, 1193 <literal><<</literal>, 1194 <literal>></literal>, and 1195 <literal>>></literal>, pipes using 1196 <literal>|</literal>, running programs in the background using 1197 <literal>&</literal>, and <emphasis>other elements of shell 1198 syntax are not supported</emphasis>.</para> 1199 1200 <para>The command to execute may contain spaces, but control characters are not allowed.</para> 1201 1202 <para>The command line accepts <literal>%</literal> specifiers as described in 1203 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para> 1204 1205 <para>Basic environment variable substitution is supported. Use 1206 <literal>${FOO}</literal> as part of a word, or as a word of its 1207 own, on the command line, in which case it will be erased and replaced 1208 by the exact value of the environment variable (if any) including all 1209 whitespace it contains, always resulting in exactly a single argument. 1210 Use <literal>$FOO</literal> as a separate word on the command line, in 1211 which case it will be replaced by the value of the environment 1212 variable split at whitespace, resulting in zero or more arguments. 1213 For this type of expansion, quotes are respected when splitting 1214 into words, and afterwards removed.</para> 1215 1216 <para>If the command is not a full (absolute) path, it will be resolved to a full path using a 1217 fixed search path determined at compilation time. Searched directories include 1218 <filename>/usr/local/bin/</filename>, <filename>/usr/bin/</filename>, <filename>/bin/</filename> 1219 on systems using split <filename>/usr/bin/</filename> and <filename>/bin/</filename> 1220 directories, and their <filename>sbin/</filename> counterparts on systems using split 1221 <filename>bin/</filename> and <filename>sbin/</filename>. It is thus safe to use just the 1222 executable name in case of executables located in any of the "standard" directories, and an 1223 absolute path must be used in other cases. Using an absolute path is recommended to avoid 1224 ambiguity. Hint: this search path may be queried using 1225 <command>systemd-path search-binaries-default</command>.</para> 1226 1227 <para>Example:</para> 1228 1229 <programlisting>Environment="ONE=one" 'TWO=two two' 1230ExecStart=echo $ONE $TWO ${TWO}</programlisting> 1231 1232 <para>This will execute <command>/bin/echo</command> with four 1233 arguments: <literal>one</literal>, <literal>two</literal>, 1234 <literal>two</literal>, and <literal>two two</literal>.</para> 1235 1236 <para>Example:</para> 1237 <programlisting>Environment=ONE='one' "TWO='two two' too" THREE= 1238ExecStart=/bin/echo ${ONE} ${TWO} ${THREE} 1239ExecStart=/bin/echo $ONE $TWO $THREE</programlisting> 1240 <para>This results in <filename>/bin/echo</filename> being 1241 called twice, the first time with arguments 1242 <literal>'one'</literal>, 1243 <literal>'two two' too</literal>, <literal></literal>, 1244 and the second time with arguments 1245 <literal>one</literal>, <literal>two two</literal>, 1246 <literal>too</literal>. 1247 </para> 1248 1249 <para>To pass a literal dollar sign, use <literal>$$</literal>. 1250 Variables whose value is not known at expansion time are treated 1251 as empty strings. Note that the first argument (i.e. the program 1252 to execute) may not be a variable.</para> 1253 1254 <para>Variables to be used in this fashion may be defined through 1255 <varname>Environment=</varname> and 1256 <varname>EnvironmentFile=</varname>. In addition, variables listed 1257 in the section "Environment variables in spawned processes" in 1258 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1259 which are considered "static configuration", may be used (this 1260 includes e.g. <varname>$USER</varname>, but not 1261 <varname>$TERM</varname>).</para> 1262 1263 <para>Note that shell command lines are not directly supported. If 1264 shell command lines are to be used, they need to be passed 1265 explicitly to a shell implementation of some kind. Example:</para> 1266 <programlisting>ExecStart=sh -c 'dmesg | tac'</programlisting> 1267 1268 <para>Example:</para> 1269 1270 <programlisting>ExecStart=echo one ; echo "two two"</programlisting> 1271 1272 <para>This will execute <command>echo</command> two times, 1273 each time with one argument: <literal>one</literal> and 1274 <literal>two two</literal>, respectively. Because two commands are 1275 specified, <varname>Type=oneshot</varname> must be used.</para> 1276 1277 <para>Example:</para> 1278 1279 <programlisting>ExecStart=echo / >/dev/null & \; \ 1280ls</programlisting> 1281 1282 <para>This will execute <command>echo</command> 1283 with five arguments: <literal>/</literal>, 1284 <literal>>/dev/null</literal>, 1285 <literal>&</literal>, <literal>;</literal>, and 1286 <literal>ls</literal>.</para> 1287 </refsect1> 1288 1289 <refsect1> 1290 <title>Examples</title> 1291 1292 <example> 1293 <title>Simple service</title> 1294 1295 <para>The following unit file creates a service that will 1296 execute <filename index="false">/usr/sbin/foo-daemon</filename>. Since no 1297 <varname>Type=</varname> is specified, the default 1298 <varname>Type=</varname><option>simple</option> will be assumed. 1299 systemd will assume the unit to be started immediately after the 1300 program has begun executing.</para> 1301 1302 <programlisting>[Unit] 1303Description=Foo 1304 1305[Service] 1306ExecStart=/usr/sbin/foo-daemon 1307 1308[Install] 1309WantedBy=multi-user.target</programlisting> 1310 1311 <para>Note that systemd assumes here that the process started by 1312 systemd will continue running until the service terminates. If 1313 the program daemonizes itself (i.e. forks), please use 1314 <varname>Type=</varname><option>forking</option> instead.</para> 1315 1316 <para>Since no <varname>ExecStop=</varname> was specified, 1317 systemd will send SIGTERM to all processes started from this 1318 service, and after a timeout also SIGKILL. This behavior can be 1319 modified, see 1320 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1321 for details.</para> 1322 1323 <para>Note that this unit type does not include any type of 1324 notification when a service has completed initialization. For 1325 this, you should use other unit types, such as 1326 <varname>Type=</varname><option>notify</option> if the service 1327 understands systemd's notification protocol, 1328 <varname>Type=</varname><option>forking</option> if the service 1329 can background itself or 1330 <varname>Type=</varname><option>dbus</option> if the unit 1331 acquires a DBus name once initialization is complete. See 1332 below.</para> 1333 </example> 1334 1335 <example> 1336 <title>Oneshot service</title> 1337 1338 <para>Sometimes, units should just execute an action without 1339 keeping active processes, such as a filesystem check or a 1340 cleanup action on boot. For this, 1341 <varname>Type=</varname><option>oneshot</option> exists. Units 1342 of this type will wait until the process specified terminates 1343 and then fall back to being inactive. The following unit will 1344 perform a cleanup action:</para> 1345 1346 <programlisting>[Unit] 1347Description=Cleanup old Foo data 1348 1349[Service] 1350Type=oneshot 1351ExecStart=/usr/sbin/foo-cleanup 1352 1353[Install] 1354WantedBy=multi-user.target</programlisting> 1355 1356 <para>Note that systemd will consider the unit to be in the 1357 state "starting" until the program has terminated, so ordered 1358 dependencies will wait for the program to finish before starting 1359 themselves. The unit will revert to the "inactive" state after 1360 the execution is done, never reaching the "active" state. That 1361 means another request to start the unit will perform the action 1362 again.</para> 1363 1364 <para><varname>Type=</varname><option>oneshot</option> are the 1365 only service units that may have more than one 1366 <varname>ExecStart=</varname> specified. For units with multiple 1367 commands (<varname index="false">Type=oneshot</varname>), all commands will be run again.</para> 1368 <para> For <varname index="false">Type=oneshot</varname>, <varname>Restart=</varname><option>always</option> 1369 and <varname>Restart=</varname><option>on-success</option> are <emphasis>not</emphasis> allowed.</para> 1370 </example> 1371 1372 <example> 1373 <title>Stoppable oneshot service</title> 1374 1375 <para>Similarly to the oneshot services, there are sometimes 1376 units that need to execute a program to set up something and 1377 then execute another to shut it down, but no process remains 1378 active while they are considered "started". Network 1379 configuration can sometimes fall into this category. Another use 1380 case is if a oneshot service shall not be executed each time 1381 when they are pulled in as a dependency, but only the first 1382 time.</para> 1383 1384 <para>For this, systemd knows the setting 1385 <varname>RemainAfterExit=</varname><option>yes</option>, which 1386 causes systemd to consider the unit to be active if the start 1387 action exited successfully. This directive can be used with all 1388 types, but is most useful with 1389 <varname>Type=</varname><option>oneshot</option> and 1390 <varname>Type=</varname><option>simple</option>. With 1391 <varname>Type=</varname><option>oneshot</option>, systemd waits 1392 until the start action has completed before it considers the 1393 unit to be active, so dependencies start only after the start 1394 action has succeeded. With 1395 <varname>Type=</varname><option>simple</option>, dependencies 1396 will start immediately after the start action has been 1397 dispatched. The following unit provides an example for a simple 1398 static firewall.</para> 1399 1400 <programlisting>[Unit] 1401Description=Simple firewall 1402 1403[Service] 1404Type=oneshot 1405RemainAfterExit=yes 1406ExecStart=/usr/local/sbin/simple-firewall-start 1407ExecStop=/usr/local/sbin/simple-firewall-stop 1408 1409[Install] 1410WantedBy=multi-user.target</programlisting> 1411 1412 <para>Since the unit is considered to be running after the start 1413 action has exited, invoking <command>systemctl start</command> 1414 on that unit again will cause no action to be taken.</para> 1415 </example> 1416 1417 <example> 1418 <title>Traditional forking services</title> 1419 1420 <para>Many traditional daemons/services background (i.e. fork, 1421 daemonize) themselves when starting. Set 1422 <varname>Type=</varname><option>forking</option> in the 1423 service's unit file to support this mode of operation. systemd 1424 will consider the service to be in the process of initialization 1425 while the original program is still running. Once it exits 1426 successfully and at least a process remains (and 1427 <varname>RemainAfterExit=</varname><option>no</option>), the 1428 service is considered started.</para> 1429 1430 <para>Often, a traditional daemon only consists of one process. 1431 Therefore, if only one process is left after the original 1432 process terminates, systemd will consider that process the main 1433 process of the service. In that case, the 1434 <varname>$MAINPID</varname> variable will be available in 1435 <varname>ExecReload=</varname>, <varname>ExecStop=</varname>, 1436 etc.</para> 1437 1438 <para>In case more than one process remains, systemd will be 1439 unable to determine the main process, so it will not assume 1440 there is one. In that case, <varname>$MAINPID</varname> will not 1441 expand to anything. However, if the process decides to write a 1442 traditional PID file, systemd will be able to read the main PID 1443 from there. Please set <varname>PIDFile=</varname> accordingly. 1444 Note that the daemon should write that file before finishing 1445 with its initialization. Otherwise, systemd might try to read the 1446 file before it exists.</para> 1447 1448 <para>The following example shows a simple daemon that forks and 1449 just starts one process in the background:</para> 1450 1451 <programlisting>[Unit] 1452Description=Some simple daemon 1453 1454[Service] 1455Type=forking 1456ExecStart=/usr/sbin/my-simple-daemon -d 1457 1458[Install] 1459WantedBy=multi-user.target</programlisting> 1460 1461 <para>Please see 1462 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1463 for details on how you can influence the way systemd terminates 1464 the service.</para> 1465 </example> 1466 1467 <example> 1468 <title>DBus services</title> 1469 1470 <para>For services that acquire a name on the DBus system bus, 1471 use <varname>Type=</varname><option>dbus</option> and set 1472 <varname>BusName=</varname> accordingly. The service should not 1473 fork (daemonize). systemd will consider the service to be 1474 initialized once the name has been acquired on the system bus. 1475 The following example shows a typical DBus service:</para> 1476 1477 <programlisting>[Unit] 1478Description=Simple DBus service 1479 1480[Service] 1481Type=dbus 1482BusName=org.example.simple-dbus-service 1483ExecStart=/usr/sbin/simple-dbus-service 1484 1485[Install] 1486WantedBy=multi-user.target</programlisting> 1487 1488 <para>For <emphasis>bus-activatable</emphasis> services, do not 1489 include a [Install] section in the systemd 1490 service file, but use the <varname>SystemdService=</varname> 1491 option in the corresponding DBus service file, for example 1492 (<filename>/usr/share/dbus-1/system-services/org.example.simple-dbus-service.service</filename>):</para> 1493 1494 <programlisting>[D-BUS Service] 1495Name=org.example.simple-dbus-service 1496Exec=/usr/sbin/simple-dbus-service 1497User=root 1498SystemdService=simple-dbus-service.service</programlisting> 1499 1500 <para>Please see 1501 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1502 for details on how you can influence the way systemd terminates 1503 the service.</para> 1504 </example> 1505 1506 <example> 1507 <title>Services that notify systemd about their initialization</title> 1508 1509 <para><varname>Type=</varname><option>simple</option> services 1510 are really easy to write, but have the major disadvantage of 1511 systemd not being able to tell when initialization of the given 1512 service is complete. For this reason, systemd supports a simple 1513 notification protocol that allows daemons to make systemd aware 1514 that they are done initializing. Use 1515 <varname>Type=</varname><option>notify</option> for this. A 1516 typical service file for such a daemon would look like 1517 this:</para> 1518 1519 <programlisting>[Unit] 1520Description=Simple notifying service 1521 1522[Service] 1523Type=notify 1524ExecStart=/usr/sbin/simple-notifying-service 1525 1526[Install] 1527WantedBy=multi-user.target</programlisting> 1528 1529 <para>Note that the daemon has to support systemd's notification 1530 protocol, else systemd will think the service has not started yet 1531 and kill it after a timeout. For an example of how to update 1532 daemons to support this protocol transparently, take a look at 1533 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>. 1534 systemd will consider the unit to be in the 'starting' state 1535 until a readiness notification has arrived.</para> 1536 1537 <para>Please see 1538 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry> 1539 for details on how you can influence the way systemd terminates 1540 the service.</para> 1541 </example> 1542 </refsect1> 1543 1544 <refsect1> 1545 <title>See Also</title> 1546 <para> 1547 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, 1548 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>, 1549 <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1550 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1551 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1552 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1553 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>, 1554 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>, 1555 <citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry> 1556 </para> 1557 </refsect1> 1558 1559</refentry> 1560