When creating snapcraft.yaml to build a new snap, a snap’s executable component can be either exposed as a command or run as a background service or daemon.
For details on how to expose an executable from its constituent parts, see Defining a command.
A snap daemon or service behaves the same as a native daemon or service, and will either start automatically at boot time and end when the machine is shutdown, or start and stop on demand through socket activation.
Snap confinement prohibits a system’s users and groups from running as traditional services might, such as under a user’s ownership. But a daemon user and group can alternatively be created within a snap to provide similar user and group level control outside of a snap’s confinement. See System usernames for more details.
See Service management for details on starting and stopping services from the snap command. Services and daemons can also be managed from within a snap, such as via a hook, with the snapctl.
To set memory and CPU resource limits for a service or daemon, see Quota groups
If you need to add user configurable options to your service or daemon, such as which port it should use, see Adding snap configuration.
To define an executable as a daemon or service, add daemon: simple
to its apps stanza:
apps:
part-os-release:
command: bin/os-release.sh
daemon: simple
The value for daemon:
can be one of the following:
fork()
as part of its start-up and the parent process is then expected to exit when start-up is complete. This isn’t the recommended behaviour on a modern Linux system.In addition to the above types of daemon or service, the following can be set to help manage how a service is run, how it can be stopped, and what should happen after it stops:
enable
(default) or disable
.restart
, endure
, (do not restart) or ignore-running
(does not refresh running services to facilitate the refresh app awareness feature). Defaults to restart .snap restart --reload
command. reload-command: sbin/nginx -s reload
[on-failure|on-success|on-abnormal|on-abort|on-watchdog|always|never]
.10ns
, 10us
, 10ms
, 10s
, 10m
.10ns
, 10us
, 10ms
, 10s
, 10m
.sigterm
, sigterm-all
, sighup
, sighup-all
, sigusr1
, sigusr1-all
, sigusr2
, sigusr2-all
, sigint
and sigint-all
.10ns
, 10us
, 10ms
, 10s
, 10m
. Termination is via SIGTERM
(and SIGKILL
if that doesn’t work).10ns
, 10us
, 10ms
, 10s
, 10m
.For further details, see Snapcraft app and service metadata.
Daemons can configured to interact with D-Bus in a number of ways. D-Bus can be used to indicate to systemd that a daemon is running, it can be used as the mechanism to activate a daemon, and it can be used generally to expose services to applications.
D-Bus activation can only be used for services on the system bus.
The daemon
keyword is used to specify the type of a daemon and the mechanism it uses to inform systemd that it is running.
A daemon can be configured to use D-Bus to notify systemd that it is running by claiming a D-Bus name. This behaviour is enabled by setting the daemon
keyword to a value of dbus
in the app metadata.
Daemons that use D-Bus in other ways that do not need this feature can set the daemon
type to a value other than dbus
. This enables other methods to be used to indicate to systemd that they are running.
If the dbus
type is used, either the bus-name
keyword or activates-on
keyword must be used to define a bus name for the daemon. If both keywords are defined, the bus name takes precedence. If only the activates-on
keyword is defined, the last name in its list of slots is used as the bus name.
The activates-on
keyword is used to define a list of names that will be exposed via D-Bus. These names are automatically added to the slots for the snap.
This provides a way for a daemon to be started on a D-Bus method call. When a method on any of the names is invoked, the daemon’s command
is run to start the daemon.
A daemon that needs to provide services to applications can be configured to use a bus name by setting its bus-name
keyword. This enables the system bus to be used for communication, as with regular system daemons.
As noted above, the daemon
keyword does not need to specify the dbus
type for this use case, unless it is convenient to notify systemd about start-up by claiming a D-Bus name.
Last updated 9 months ago.