service

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ota/service

Register a program with the host's service manager so it comes back after a reboot and is restarted when it exits. Then start, stop, restart and query it.

Platform Backend User scope System scope
Linux systemd systemctl --user /etc/systemd/system (root)
macOS launchd ~/Library/LaunchAgents /Library/LaunchDaemons (root)
Windows Task Scheduler logon trigger boot trigger as SYSTEM (Administrator)
import "github.com/ArmornetIO/ota/service"

spec := service.Spec{
	Name:        "myapp",
	LabelPrefix: "com.example.", // launchd label becomes com.example.myapp
	BinaryPath:  "/opt/myapp/bin/myapp",
	Args:        []string{"serve", "-config", "/etc/myapp.yaml"},
}
if err := service.Install(spec); errors.Is(err, service.ErrUnsupported) {
	// No service manager here: fall back to running detached.
}
st, _ := service.Query(spec) // Installed, Running, Detail (the manager's own words)
_ = service.Restart(spec)    // runs whatever file is at BinaryPath now
  • User scope is the default. Registering needs no privileges unless you ask for ScopeSystem.
  • It shells out to the platform's own tool. A failure can be reproduced by running the same command by hand, and no library ships for code that only runs on one OS. launchctl and schtasks are invoked by absolute path.
  • Spec is validated before anything reaches exec or a generated file. Names are limited to a conservative alphabet, paths must be absolute, and line breaks are rejected anywhere. systemd %, $ and \ expansion is escaped, and cmd.exe metacharacters are refused when a log path is set.
  • Windows uses Task Scheduler, not the Service Control Manager. An SCM service must answer the SCM's control messages, and an ordinary console program registered with sc fails to start.

Zero dependencies, Go 1.27+. Part of ota.

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

View Source
var ErrNotInstalled = errors.New("service: service is not installed")

ErrNotInstalled reports that no service by this name is registered.

View Source
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

func Install(s Spec) error

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

func Restart(s Spec) error

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.

func Start

func Start(s Spec) error

Start starts an installed unit.

func Stop

func Stop(s Spec) error

Stop stops a unit without disabling it, so it still comes back at boot.

func Uninstall

func Uninstall(s Spec) error

Uninstall stops, disables and removes the unit. A service that is already absent is not an error — uninstall is something a caller should be able to run twice.

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.

func OS

func OS() Backend

OS returns the Backend for this machine's 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

func (s Spec) Validate() error

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.

func Query

func Query(s Spec) (Status, error)

Query reports whether the unit is registered and up.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL