Service Templates¶
Name
systemd.service — Service unit configuration
Synopsis
service
.service
Description¶
A unit configuration file whose name ends in ".service
" encodes information about a process controlled and supervised by systemd.
This man page lists the configuration options specific to this unit type. See systemd.unit(5) for the common options of all unit configuration files. The common configuration items are configured in the generic [Unit] and [Install] sections. The service specific configuration options are configured in the [Service] section.
Additional options are listed in systemd.exec(5), which define the execution environment the commands are executed in, and in systemd.kill(5), which define the way the processes of the service are terminated, and in systemd.resource-control(5), which configure resource control settings for the processes of the service.
If SysV init compat is enabled, systemd automatically creates service units that wrap SysV init scripts (the service name is the same as the name of the script, with a ".service
" suffix added); see systemd-sysv-generator(8).
The systemd-run(1) command allows creating .service
and .scope
units dynamically and transiently from the command line.
It is possible for systemd services to take a single argument via the "
" syntax. Such services are called "instantiated" services, while the unit definition without the service
@argument
.serviceargument
parameter is called a "template". An example could be a [email protected]
service template which takes a network interface as a parameter to form an instantiated service. Within the service file, this parameter or "instance name" can be accessed with %-specifiers. See systemd.unit(5) for details.
Automatic Dependencies¶
Implicit Dependencies¶
The following dependencies are implicitly added:
-
Services with
Type=dbus
set automatically acquire dependencies of typeRequires=
andAfter=
ondbus.socket
. -
Socket activated services are automatically ordered after their activating
.socket
units via an automaticAfter=
dependency. Services also pull in all.socket
units listed inSockets=
via automaticWants=
andAfter=
dependencies.
Additional implicit dependencies may be added as result of execution and resource control parameters as documented in systemd.exec(5) and systemd.resource-control(5).
Default Dependencies¶
The following dependencies are added unless DefaultDependencies=no
is set:
-
Service units will have dependencies of type
Requires=
andAfter=
onsysinit.target
, a dependency of typeAfter=
onbasic.target
as well as dependencies of typeConflicts=
andBefore=
onshutdown.target
. These ensure that normal service units pull in basic system initialization, and are terminated cleanly prior to system shutdown. Only services involved with early boot or late system shutdown should disable this option. -
Instanced service units (i.e. service units with an "
@
" in their name) are assigned by default a per-template slice unit (see systemd.slice(5)), named after the template unit, containing all instances of the specific template. This slice is normally stopped at shutdown, together with all template instances. If that is not desired, setDefaultDependencies=no
in the template unit, and either define your own per-template slice unit file that also setsDefaultDependencies=no
, or setSlice=system.slice
(or another suitable slice) in the template unit. Also see systemd.resource-control(5).
Options¶
Service unit files may include [Unit] and [Install] sections, which are described in systemd.unit(5).
Service unit files must include a [Service] section, which carries information about the service and the process it supervises. A number of options that may be used in this section are shared with other unit types. These options are documented in systemd.exec(5), systemd.kill(5) and systemd.resource-control(5). The options specific to the [Service] section of service units are the following:
Type=
¶
Configures the process start-up type for this service unit. One of simple
, exec
, forking
, oneshot
, dbus
, notify
, notify-reload
or idle
:
-
If set to
simple
(the default ifExecStart=
is specified but neitherType=
norBusName=
are), the service manager will consider the unit started immediately after the main service process has been forked off. It is expected that the process configured withExecStart=
is the main process of the service. In this mode, if the process offers functionality to other processes on the system, its communication channels should be installed before the service is started up (e.g. sockets set up by systemd, via socket activation), as the service manager will immediately proceed starting follow-up units, right after creating the main service process, and before executing the service's binary. Note that this means systemctl start command lines forsimple
services will report success even if the service's binary cannot be invoked successfully (for example because the selectedUser=
doesn't exist, or the service binary is missing). -
The
exec
type is similar tosimple
, but the service manager will consider the unit started immediately after the main service binary has been executed. The service manager will delay starting of follow-up units until that point. (Or in other words:simple
proceeds with further jobs right afterfork()
returns, whileexec
will not proceed before bothfork()
andexecve()
in the service process succeeded.) Note that this means systemctl start command lines forexec
services will report failure when the service's binary cannot be invoked successfully (for example because the selectedUser=
doesn't exist, or the service binary is missing). -
If set to
forking
, it is expected that the process configured withExecStart=
will callfork()
as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is recommended to also use thePIDFile=
option, so that systemd can reliably identify the main process of the service. systemd will proceed with starting follow-up units as soon as the parent process exits. -
Behavior of
oneshot
is similar tosimple
; however, the service manager will consider the unit up after the main process exits. It will then start follow-up units.RemainAfterExit=
is particularly useful for this type of service.Type=
oneshot
is the implied default if neitherType=
norExecStart=
are specified. Note that if this option is used withoutRemainAfterExit=
the service will never enter "active
" unit state, but directly transition from "activating
" to "deactivating
" or "dead
" since no process is configured that shall run continuously. In particular this means that after a service of this type ran (and which hasRemainAfterExit=
not set) it will not show up as started afterwards, but as dead. -
Behavior of
dbus
is similar tosimple
; however, it is expected that the service acquires a name on the D-Bus bus, as configured byBusName=
. systemd will proceed with starting follow-up units after the D-Bus bus name has been acquired. Service units with this option configured implicitly gain dependencies on thedbus.socket
unit. This type is the default ifBusName=
is specified. A service unit of this type is considered to be in the activating state until the specified bus name is acquired. It is considered activated while the bus name is taken. Once the bus name is released the service is considered being no longer functional which has the effect that the service manager attempts to terminate any remaining processes belonging to the service. Services that drop their bus name as part of their shutdown logic thus should be prepared to receive aSIGTERM
(or whichever signal is configured inKillSignal=
) as result. -
Behavior of
notify
is similar toexec
; however, it is expected that the service sends a "READY=1
" notification message via sd_notify(3) or an equivalent call when it has finished starting up. systemd will proceed with starting follow-up units after this notification message has been sent. If this option is used,NotifyAccess=
(see below) should be set to open access to the notification socket provided by systemd. IfNotifyAccess=
is missing or set tonone
, it will be forcibly set tomain
. -
Behavior of
notify-reload
is identical tonotify
. However, it extends the logic in one way: theSIGHUP
UNIX process signal is sent to the service's main process when the service is asked to reload. (The signal to send can be tweaked viaReloadSignal=
, see below.) When initiating the reload process the service is then expected to reply with a notification message via sd_notify(3) that contains the "RELOADING=1
" field in combination with "MONOTONIC_USEC=
" set to the current monotonic time (i.e.CLOCK_MONOTONIC
in clock_gettime(2)) in μs, formatted as decimal string. Once reloading is complete another notification message must be sent, containing "READY=1
". Using this service type and implementing this reload protocol is an efficient alternative to providing anExecReload=
command for reloading of the service's configuration. -
Behavior of
idle
is very similar tosimple
; however, actual execution of the service program is delayed until all active jobs are dispatched. This may be used to avoid interleaving of output of shell services with the status output on the console. Note that this type is useful only to improve console output, it is not useful as a general unit ordering tool, and the effect of this service type is subject to a 5s timeout, after which the service program is invoked anyway.
It is generally recommended to use Type=
simple
for long-running services whenever possible, as it is the simplest and fastest option. However, as this service type won't propagate service start-up failures and doesn't allow ordering of other units against completion of initialization of the service (which for example is useful if clients need to connect to the service through some form of IPC, and the IPC channel is only established by the service itself — in contrast to doing this ahead of time through socket or bus activation or similar), it might not be sufficient for many cases. If so, notify
, notify-reload
or dbus
(the latter only in case the service provides a D-Bus interface) are the preferred options as they allow service program code to precisely schedule when to consider the service started up successfully and when to proceed with follow-up units. The notify
/notify-reload
service types require explicit support in the service codebase (as sd_notify()
or an equivalent API needs to be invoked by the service at the appropriate time) — if it's not supported, then forking
is an alternative: it supports the traditional UNIX service start-up protocol. Finally, exec
might be an option for cases where it is enough to ensure the service binary is invoked, and where the service binary itself executes no or little initialization on its own (and its initialization is unlikely to fail). Note that using any type other than simple
possibly delays the boot process, as the service manager needs to wait for service initialization to complete. It is hence recommended not to needlessly use any types other than simple
. (Also note it is generally not recommended to use idle
or oneshot
for long-running services.)
ExitType=
¶
Specifies when the manager should consider the service to be finished. One of main
or cgroup
:
-
If set to
main
(the default), the service manager will consider the unit stopped when the main process, which is determined according to theType=
, exits. Consequently, it cannot be used withType=
oneshot
. -
If set to
cgroup
, the service will be considered running as long as at least one process in the cgroup has not exited.
It is generally recommended to use ExitType=
main
when a service has a known forking model and a main process can reliably be determined. ExitType=
cgroup
is meant for applications whose forking model is not known ahead of time and which might not have a specific main process. It is well suited for transient or automatically gen