winsvc

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 4 Imported by: 0

README

Windows service

GoDoc

Install

go get github.com/czyt/winsvc

Example

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"github.com/czyt/winsvc"
)

var (
	appPath string

	flagServiceName = flag.String("service-name", "hello-winsvc", "Set service name")
	flagServiceDesc = flag.String("service-desc", "hello windows service", "Set service description")

	flagServiceInstall   = flag.Bool("service-install", false, "Install service")
	flagServiceUninstall = flag.Bool("service-remove", false, "Remove service")
	flagServiceStart     = flag.Bool("service-start", false, "Start service")
	flagServiceStop      = flag.Bool("service-stop", false, "Stop service")

	flagHelp = flag.Bool("help", false, "Show usage and exit.")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `Usage:
  hello [options]...

Options:
`)
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "%s\n", `
Example:
  # run hello server
  $ go build -o hello.exe hello.go
  $ hello.exe

  # install hello as windows service
  $ hello.exe -service-install

  # start/stop hello service
  $ hello.exe -service-start
  $ hello.exe -service-stop

  # remove hello service
  $ hello.exe -service-remove

  # help
  $ hello.exe -h

Report bugs to <chaishushan{AT}gmail.com>.`)
	}

	// change to current dir
	var err error
	if appPath, err = winsvc.GetAppPath(); err != nil {
		log.Fatal(err)
	}
	if err := os.Chdir(filepath.Dir(appPath)); err != nil {
		log.Fatal(err)
	}
}

func main() {
	flag.Parse()

	// install service
	if *flagServiceInstall {
		if err := winsvc.InstallService(appPath, *flagServiceName,"", *flagServiceDesc); err != nil {
			log.Fatalf("installService(%s, %s): %v\n", *flagServiceName, *flagServiceDesc, err)
		}
		fmt.Printf("Done\n")
		return
	}

	// remove service
	if *flagServiceUninstall {
		if err := winsvc.RemoveService(*flagServiceName); err != nil {
			log.Fatalln("removeService:", err)
		}
		fmt.Printf("Done\n")
		return
	}

	// start service
	if *flagServiceStart {
		if err := winsvc.StartService(*flagServiceName); err != nil {
			log.Fatalln("startService:", err)
		}
		fmt.Printf("Done\n")
		return
	}

	// stop service
	if *flagServiceStop {
		if err := winsvc.StopService(*flagServiceName); err != nil {
			log.Fatalln("stopService:", err)
		}
		fmt.Printf("Done\n")
		return
	}

	// run as service
	if winsvc.InServiceMode() {
		log.Println("main:", "runService")
		if err := winsvc.RunAsService(*flagServiceName, StartServer, StopServer, false); err != nil {
			log.Fatalf("svc.Run: %v\n", err)
		}
		return
	}

	// run as normal
	StartServer()
}

func StartServer() {
	log.Println("StartServer, port = 8080")
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "winsrv server", time.Now())
	})
	http.ListenAndServe(":8080", nil)
}

func StopServer() {
	log.Println("StopServer")
}

BUGS

Note

the repo was forked from chai2010

Report bugs to czytcn@gmail.com.

Thanks!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAppPath

func GetAppPath() (string, error)

func InServiceMode

func InServiceMode() bool

func InstallService

func InstallService(appPath, name, desc string, params ...string) error

func InstallServiceWithOption

func InstallServiceWithOption(appPath, name string, params ServiceParamsOption, option ...ServiceOption) error

func IsAnInteractiveSession

func IsAnInteractiveSession() bool

func QueryService

func QueryService(name string) (status string, err error)

func RemoveService

func RemoveService(name string) error

func RunAsService

func RunAsService(name string, start, stop func(), isDebug bool) (err error)

func StartService

func StartService(name string) error

func StopService

func StopService(name string) error

Types

type ServiceOption

type ServiceOption func(*mgr.Config)

func AutoDelayStart

func AutoDelayStart() ServiceOption

func AutoStart

func AutoStart() ServiceOption

func Dependencies

func Dependencies(serviceName ...string) ServiceOption

func Description

func Description(description string) ServiceOption

func DisabledStart

func DisabledStart() ServiceOption

func DisplayName

func DisplayName(displayName string) ServiceOption

func OnBootStart

func OnBootStart() ServiceOption

func OnDemandStart

func OnDemandStart() ServiceOption

func OnSystemStart

func OnSystemStart() ServiceOption

type ServiceParamsOption

type ServiceParamsOption func(param ...string) []string

func ServiceInstallParams

func ServiceInstallParams(param ...string) ServiceParamsOption

Jump to

Keyboard shortcuts

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