systemservice

package module
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2020 License: MIT Imports: 12 Imported by: 0

README

systemservice

GoDoc MIT License Go Report Card Test Status code size

THIS IS A WORK IN PROGRESS AND IS NOT CURRENLTY USABLE

A cross-platform system service manager written in Go

Operating system support:

  • Windows (via sc.exe)
  • Mac (via launchctl)
  • Linux: (via systemd)

Install

go get -u github.com/danawoodman/systemservice

Usage

Please see the examples folder for examples.

First, setup a new service:

// Create a command to run.
cmd := systemservice.ServiceCommand{
  Label: "some-unique-id",
  Program: "echo",
  Args: []string{"Hello", "World", "!"},
}

// Create the service
serv := systemservice.New(cmd)

Now you can manage your service as needed:

serv.Install(start bool) error
serv.Start() error
serv.Restart() error
serv.Stop() error
serv.Uninstall() error
serv.Status() (systemservice.ServiceStatus, error)
serv.Running() bool

These commands are the same no matter the operating system target.

Platform Notes
Mac OSX (aka Darwin)

Replace <LABEL> and <NAME> with the values you setup in your Command.

  • If running as a root user:
    • Service plist is located at /Library/LaunchDaemons/<LABEL>.plist
    • Stdout logs are sent to /Library/Logs/<NAME>/<NAME>.stdout.log
    • Stderr logs are send to /Library/Logs/<NAME>/<NAME>.stderr.log
  • If running as a non-root user:
    • Service plist is located at ~/Library/LaunchAgents/<LABEL>.plist
    • Stdout logs are sent to ~/Library/Logs/<NAME>/<NAME>.stdout.log
    • Stderr logs are send to ~/Library/Logs/<NAME>/<NAME>.stderr.log
Linux (Systemd)
  • View logs with journalctl -u <LABEL>

Similar project

License

MIT. See license.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(customLogger Logger)

SetLogger allows the consumer of this package (that's you!) configure your own customer logger. As long as it implements the "Logger" interface

Types

type Logger

type Logger interface {
	Log(v ...interface{})
	Logf(format string, v ...interface{})
}

Logger implements a basic, overridable logging interface

type ServiceCommand

type ServiceCommand struct {
	// The human-friendly name of your service. Note: best to not include
	// spaces in the name.
	Name string

	// The label to use to identify the service. This must be unique
	// and should not include spaces.
	Label string

	// The name of the program to run
	Program string

	// The arguments to pass to the command. Optional.
	Args []string

	// The description of your service. Optional.
	Description string

	// The URL to your service documentation. Optional.
	Documentation string

	// Whether or not to turn on debug behavior
	Debug bool
}

ServiceCommand represents the command the system service should run

func (*ServiceCommand) String

func (c *ServiceCommand) String() string

type ServiceDoesNotExistError

type ServiceDoesNotExistError struct {
	// contains filtered or unexported fields
}

ServiceDoesNotExistError is an error return if a given service does not exist on the system. This is usually returned if the user attempts to manage a service not yet configured on the system.

func (*ServiceDoesNotExistError) Error

func (e *ServiceDoesNotExistError) Error() string

Error implements the errors.Error interface

type ServiceStatus

type ServiceStatus struct {
	Running bool
	PID     int
}

ServiceStatus is a generic representation of the service running on the system

type SystemService

type SystemService struct {
	Command ServiceCommand
}

SystemService represents a generic system service configuration

func New

New creates a new system service manager instance.

func (*SystemService) Exists

func (s *SystemService) Exists() bool

Exists returns whether or not the unit file eixts

func (*SystemService) Install

func (s *SystemService) Install(start bool) error

Install the system service. If start is passed, also starts the service.

func (*SystemService) Restart

func (s *SystemService) Restart() error

Restart attempts to stop the service if running then starts it again

func (*SystemService) Run

func (s *SystemService) Run() error

Run is a no-op on Linux based systems

func (*SystemService) Running

func (s *SystemService) Running() (bool, error)

Running indicates if the service is active and running

func (*SystemService) Start

func (s *SystemService) Start() error

Start the system service if it is installed

func (*SystemService) Status

func (s *SystemService) Status() (status *ServiceStatus, err error)

Status returns whether or not the system service is running

func (*SystemService) Stop

func (s *SystemService) Stop() error

Stop stops the system service by unloading the unit file

func (*SystemService) Uninstall

func (s *SystemService) Uninstall() error

Uninstall the system service by first stopping it then removing the unit file.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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