service

package module
v0.0.0-...-f527391 Latest Latest
Warning

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

Go to latest
Published: May 8, 2013 License: PostgreSQL Imports: 5 Imported by: 2

README

Create system services in Go.

golang service package will install / un-install and run a program as a service (daemon). Currently supports Windows XP+ and Linux/Upstart.

To start out follow "example/main.go".

package main

import (
	"github.com/axgle/service"
	"fmt"
	"os"
)

func main() {
	var displayName = "Go Service Test"
	var desc = "This is a test Go service.  It is designed to run well."
	var ws, err = service.NewService("GoServiceTest2", displayName, desc)

	if(err != nil) {
		fmt.Printf("%s unable to start: %s", displayName, err)
		return
	}

	if len(os.Args) > 1 {
		var err error
		verb := os.Args[1]
		switch verb {
		case "install":
			err = ws.Install()
			if err != nil {
				fmt.Printf("Failed to install: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" installed.\n", displayName)
		case "remove":
			err = ws.Remove()
			if err != nil {
				fmt.Printf("Failed to remove: %s\n", err)
				return
			}
			fmt.Printf("Service \"%s\" removed.\n", displayName)
		}
		return
	}
	err = ws.Run(func() error {
		// start
		go doWork()
		ws.LogInfo("I'm Running!")
		return nil
	}, func() error {
		// stop
		stopWork()
		ws.LogInfo("I'm Stopping!")
		return nil
	})
	if err != nil {
		ws.LogError(err.Error())
	}
}

func doWork() {

}
func stopWork() {

}

From https://bitbucket.org/kardianos/service

Documentation

Overview

Package service provides a simple way to create a system service. Currently supports Windows and Linux/Upstart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetExePath

func GetExePath() (exePath string, err error)

Returns the full path of the running executable as reported by the system. Includes the executable image name.

Types

type Service

type Service interface {
	// Installs this service on the system.  May return an
	// error if this service is already installed.
	Install() error

	// Removes this service from the system.  May return an
	// error if this service is not already installed.
	Remove() error

	// Call quickly after initial entry point.  Does not return until
	// service is ready to stop.  onStart is called when the service is
	// starting, returning an error will fail to start the service.
	// If an error is returned from onStop, the service will still stop.
	// An error passed from onStart or onStop will be returned as
	// an error from Run.
	// Both callbacks should return quickly and not block.
	Run(onStart, onStop func() error) error

	// Basic log functions in the context of the service.
	LogError(format string, a ...interface{}) error
	LogWarning(format string, a ...interface{}) error
	LogInfo(format string, a ...interface{}) error
}

Represents a generic way to interact with the system's service.

func NewService

func NewService(name, displayName, description string) (Service, error)

Creates a new service. name is the internal name and should not contain spaces. Display name is the pretty print name. The description is an arbitrary string used to describe the service.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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