service

package module
v0.0.0-...-5028bef Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: Zlib Imports: 10 Imported by: 1

README

service (BETA)

service will install / un-install, start / stop, and run a program as a service (daemon). Currently supports Windows XP+, Linux/(systemd | Upstart | SysV), and OSX/Launchd.

Windows controls services by setting up callbacks that is non-trivial. This is very different then other systems. This package provides the same API despite the substantial differences. It also can be used to detect how a program is called, from an interactive terminal or from a service manager.

TODO

Need to test the Interactive test for the following platforms:

  • SysV
  • systemd system and user service
  • Launchd system and user service

The following items need to be done:

  • Determine if systemd has a launchd equivalent user service.
  • Fix Interactive test for user services.
  • Document that windows doesn't have a user service.
  • Document that upstart's user service definition changes over versions. Do not support.
  • For Linux platforms and Launchd add:
    • UserName
    • Arguments
    • WorkingDirectory
    • ChRoot
  • Determine the best way to document the Config.Option values per platform.
    • Should some of the current parameters like "ChRoot" and "WorkingDirectory" move to "Option" map?

Documentation

Overview

Package service provides a simple way to create a system service. Currently supports Windows, Linux/(systemd | Upstart | SysV), and OSX/Launchd.

Index

Constants

This section is empty.

Variables

View Source
var ConsoleLogger = consoleLogger{}

ConsoleLogger logs to the std err.

Functions

func Platform

func Platform() string

Platform returns a description of the OS and service platform.

Types

type Config

type Config struct {
	Name             string       // Required name of the service. No spaces suggested.
	Privileged       bool         // If true, service will run as root/Administrator/etc
	Program          string       // The name of the program, defaults to the current program
	Arguments        []string     // Run with arguments.
	WorkingDirectory string       // Optional, service working directory
	Start            func() error // Required, function that starts the service (must not block)
	Stop             func() error // Optional, function that gets called when the service is stopping
}

Config provides the setup for a Service. The Name field is required.

type Service

type Service interface {
	// Start signals to the OS service manager the given service should start.
	Start() error

	// Stop signals to the OS service manager the given service should stop.
	Stop() error

	// Restart signals to the OS service manager the given service should stop
	// then start.
	Restart() error

	// InstalLOrUpdateRequired checks whether the service needs to be installed
	// or udpated.
	InstallOrUpdateRequired() (bool, error)

	// InstallOrUpdate installs or updates the given service to the OS service manager. If
	// the service doesn't yet exist, it is created. If it already exists, the
	// existing service is updated. If additional privileges are needed, the
	// user is prompted with an escalation dialog.
	//
	// Returns true if the service was installed or updated, false if it was
	// left alone.
	InstallOrUpdate() (bool, error)

	// Uninstall uninstalls the given service from the OS service manager. This may require
	// greater rights. Will return an error if the service is not present.
	Uninstall() error

	// Run runs the service
	Run() error
}

Service represents a service that can be run or controlled.

func New

func New(c Config) (Service, error)

New creates a new service based on a service interface and configuration.

Directories

Path Synopsis
example
logging
Simple service that only works by printing a log message every few seconds.
Simple service that only works by printing a log message every few seconds.
simple
simple does nothing except block while running the service.
simple does nothing except block while running the service.

Jump to

Keyboard shortcuts

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