Code Monkey home page Code Monkey logo

go-svc's Introduction

go-svc

Go Reference MIT License Go Report Card Build Status

Go Windows Service wrapper that plays nice with Linux. Windows tests here.

Project Status

  • Used in Production.
  • Maintained. Issues and Pull Requests will be responded to.

Go Modules

  • Please note the import path and go.mod change from github.com/judwhite/go-svc/svc to github.com/judwhite/go-svc for v1.2+
  • v1.1.3 and earlier can be imported using the previous import path
  • v1.2+ code is backwards compatible with previous versions
module awesomeProject

go 1.15

require github.com/judwhite/go-svc v1.2.0
import "github.com/judwhite/go-svc"

Example

package main

import (
	"log"
	"sync"

	"github.com/judwhite/go-svc"
)

// program implements svc.Service
type program struct {
	wg   sync.WaitGroup
	quit chan struct{}
}

func main() {
	prg := &program{}

	// Call svc.Run to start your program/service.
	if err := svc.Run(prg); err != nil {
		log.Fatal(err)
	}
}

func (p *program) Init(env svc.Environment) error {
	log.Printf("is win service? %v\n", env.IsWindowsService())
	return nil
}

func (p *program) Start() error {
	// The Start method must not block, or Windows may assume your service failed
	// to start. Launch a Goroutine here to do something interesting/blocking.

	p.quit = make(chan struct{})

	p.wg.Add(1)
	go func() {
		log.Println("Starting...")
		<-p.quit
		log.Println("Quit signal received...")
		p.wg.Done()
	}()

	return nil
}

func (p *program) Stop() error {
	// The Stop method is invoked by stopping the Windows service, or by pressing Ctrl+C on the console.
	// This method may block, but it's a good idea to finish quickly or your process may be killed by
	// Windows during a shutdown/reboot. As a general rule you shouldn't rely on graceful shutdown.

	log.Println("Stopping...")
	close(p.quit)
	p.wg.Wait()
	log.Println("Stopped.")
	return nil
}

More Examples

See the example directory for more examples, including installing and uninstalling binaries built in Go as Windows services.

Similar Projects

License

go-svc is under the MIT license. See the LICENSE file for details.

go-svc's People

Contributors

jehiah avatar judwhite avatar letsfire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-svc's Issues

get name of the service

Can I somehow get the name of the service my progam run under?
for example in Init()?
env var?

incompatible for go module

it is a nice package ! it would be better if it is available as go module。
why not add a go.mod under its root directory.

HTTP and Windows

If I add an HTTP-Server with a go routine I always get a 1053 timeout if I start the service on Windows.

package main

import (
	"log"
	"ims/webserver"
	"sync"

	"github.com/judwhite/go-svc/svc"
)

// program implements svc.Service
type program struct {
	wg   sync.WaitGroup
	quit chan struct{}
}

func main() {
	prg := &program{}

	// Call svc.Run to start your program/service.
	if err := svc.Run(prg); err != nil {
		log.Fatal(err)
	}
}

func (p *program) Init(env svc.Environment) error {
	log.Printf("is win service? %v\n", env.IsWindowsService())
	return nil
}

func (p *program) Start() error {
	// The Start method must not block, or Windows may assume your service failed
	// to start. Launch a Goroutine here to do something interesting/blocking.

	go webserver.StartWebserver()

	p.quit = make(chan struct{})

	p.wg.Add(1)
	go func() {
		log.Println("Starting...")
		<-p.quit
		log.Println("Quit signal received...")
		p.wg.Done()
	}()

	return nil
}

func (p *program) Stop() error {
	// The Stop method is invoked by stopping the Windows service, or by pressing Ctrl+C on the console.
	// This method may block, but it's a good idea to finish quickly or your process may be killed by
	// Windows during a shutdown/reboot. As a general rule you shouldn't rely on graceful shutdown.

	log.Println("Stopping...")
	close(p.quit)
	p.wg.Wait()
	log.Println("Stopped.")
	return nil
}

Stopping the service when the screen is locked

OS windows 10 corp

Hi. When the screen is locked (hotkey win + L), the service stops, when logging in, the service starts again. If I understood the code correctly, then the information about the status change comes from the library golang.org/x/sys/windows/svc , is it possible to do something about it, or does it not depend on the implementation of the current library, so it works golang.org/x/sys/windows/svc ? And in general, how normal is this behavior for services?

Using gMSA (group managed service account)

When I'm using version 1.2.1 of the library, the service fails to start when using gMSA.
It works perfectly with local system accounts and local users.
I downgraded the module to version 1.0.0, and now it works as expected (at least the service starts).

Support running under NSSM

If the binary isn't running under Service Control Manager (SCM), then StartServiceCtrlDispatcher() will fail with ERROR_FAILED_SERVICE_CONTROLLER_CONNECT. This is a more reliable way of detecting if a service is running under SCM than checking svc.IsAnInteractiveSession. Checking the IsAnInteractiveSession value first can be an optimization to avoid skipping the StartServiceCtrlDispatcher error check.

In Go, svc.Run should fail here calling windows.StartServiceCtrlDispatcher.

Troubleshooting tips, if needed: http://stackoverflow.com/a/30719534 (from 2015-05-26)

CTRL_CLOSE_EVENT

Here's how to catch CTRL_CLOSE_EVENT in Windows (end task or close console window). There was some effort to translate this to SIGTERM but requires blocking which isn't compatible with the signal.Notify model.

The right way to stop a service would be sc stop or net stop and I believe these stop signals are sent during reboot/shutdown, I'll have to double check. If they are this is probably just "interesting" and adding this would help in the case of someone killing the process instead of stopping the service gracefully.

Does not work with Go version 1.15

go-svc has been working great for me. However, after upgrading from Go 1.14.1 to 1.15.0 my Windows 10 service no longer works! When I attempt to start the service, Windows gives this error:

Error 1067: The process terminated unexpectedly

The Go 1.15 release notes talk about significant changes to the Linker, perhaps that's the problem? The issue is easy to reproduce using the Example program from the go-svc readme. Windows 10 Professional, build 1903.

Programatic graceful stop?

I've written a client for win/lin/mac that can run interactively, or as a service, or as a scheduled task.
Whilst using go-svc handles a graceful stop when a SIGINT or windows service stop is received, I want to also be able to call prg.Stop() to stop programatically eg after a set number of interactions/time etc.

For some calling prg.Stop() (as per your example/main.go) just hangs.
I've also tried sending a SIGINT to self: This works on mac/lin causing the Stop() func to run, but on windows it exits immediately without calling Stop()

How do you stop go-svc programatically (at the moment I'm just doing os.exit() but that seems crude).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.