Documentation
¶
Overview ¶
Package service registers a program with the host's init system so it starts again after a reboot and is restarted when it exits, and starts, stops and restarts it there.
A detached child process is correct for the session it was started in and gone the next time the machine comes up, and nothing reports that. Callers that need a program to stay running register it here instead, and fall back to a detached process only on ErrUnsupported.
Three implementations, one per init system: systemd on Linux, launchd on macOS, the task scheduler on Windows. Each shells out to the platform's own tool rather than linking a library: these are the commands an administrator would run by hand, so a failure is diagnosable by running the printed command, and no dependency ships for a code path that only executes on one GOOS.
Scope ¶
The default is a USER service, and that is load-bearing rather than timid. An installer that runs as whoever launched it and asks for no privileges would break that promise at the last step if registering the service suddenly needed root. ScopeSystem exists for a host genuinely being provisioned as a server, and the caller has to ask for it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotInstalled = errors.New("service: service is not installed")
ErrNotInstalled reports that no service by this name is registered.
var ErrUnsupported = errors.New("service: no service manager for this platform")
ErrUnsupported reports that this GOOS has no implementation. Callers treat it as "leave the process detached" rather than as a failed install — a program running without boot persistence is worse than one with it, and much better than none.
Functions ¶
func Install ¶
Install writes the unit, reloads systemd and enables the service so it comes back at boot. It is idempotent: an existing unit of the same name is overwritten and re-enabled.
func Restart ¶
Restart stops and starts the unit in one systemd job, which is what a binary swap needs: systemd execs the new file on the start half.
Types ¶
type Backend ¶
type Backend interface {
Install(Spec) error
Uninstall(Spec) error
Query(Spec) (Status, error)
Start(Spec) error
Stop(Spec) error
Restart(Spec) error
}
Backend is the set of service-manager operations, as an interface so code that drives a service (ota's restart strategies) can be tested without a real service manager.
type Scope ¶
type Scope string
Scope selects which init-system domain the service is registered in.
const ( // ScopeUser registers the service for the invoking user and starts it at // login: `systemctl --user`, ~/Library/LaunchAgents, a task that runs ONLOGON. ScopeUser Scope = "user" // ScopeSystem registers a machine-wide daemon that starts at boot with no // user logged in. Requires root/Administrator. ScopeSystem Scope = "system" )
type Spec ¶
type Spec struct {
// Name is the service identifier. It appears in a systemd unit filename, a
// launchd label and a scheduled task name, so it is constrained to what all
// three accept: see Validate.
Name string
// Description is the human-readable label shown by the service manager.
// Empty means Name.
Description string
// LabelPrefix is prepended to Name to form the launchd label, which is
// reverse-DNS by convention (e.g. "io.example."). Empty means the label is
// Name alone. Ignored by the other service managers.
LabelPrefix string
// BinaryPath is the absolute path to the executable. Absolute
// because an init system starts the service with a working directory and an
// environment that are not the installer's — a relative path resolves
// against something the caller never saw.
BinaryPath string
// Args are passed to BinaryPath, e.g. {"serve", "-config", path}.
Args []string
// WorkingDir is the service's working directory. Empty means the platform
// default.
WorkingDir string
// LogPath receives the service's stdout and stderr. Empty means the
// platform's own journal.
LogPath string
// Scope selects user or system registration. Empty means ScopeUser.
Scope Scope
}
Spec describes the daemon to register. The same value identifies a service for Install, Uninstall and Query, so a caller that can install can always name what it installed.
func (Spec) Validate ¶
Validate rejects a Spec before any of it reaches exec or a generated unit file.
Name is the field that matters: it is interpolated into a filename, into a systemd unit, into an XML plist and into a schtasks argument. A name carrying a path separator escapes the directory the unit was meant to land in, and one carrying a quote or an angle bracket breaks out of the plist it is written into. Constraining it to one conservative alphabet is cheaper than four correct context-specific escapes, and does not cost anything real — these are service names.
type Status ¶
type Status struct {
// Installed is whether a unit/plist/task by this name is registered.
Installed bool
// Running is whether the service manager currently has it up. A service can
// be installed and not running — that is a crash loop, and reporting it as
// simply "installed" is how the old detached-process check lied.
Running bool
// Detail carries the service manager's own words, for surfacing a failure
// the caller cannot interpret.
Detail string
}
Status reports what the service manager currently knows about the service.