• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H15-Aug-2021-

example/H15-Aug-2021-

LICENSEH A D15-Aug-20211.1 KiB

README.mdH A D15-Aug-20212.8 KiB

go.modH A D15-Aug-2021104

go.sumH A D15-Aug-2021207

svc.goH A D15-Aug-20212.3 KiB

svc_common.goH A D15-Aug-20211.2 KiB

svc_common_test.goH A D15-Aug-20211.7 KiB

svc_test.goH A D15-Aug-2021599

svc_windows.goH A D15-Aug-20213.7 KiB

svc_windows_test.goH A D15-Aug-202111.1 KiB

README.md

1# go-svc
2
3[![Go Reference](https://pkg.go.dev/badge/github.com/judwhite/go-svc.svg)](https://pkg.go.dev/github.com/judwhite/go-svc)
4[![MIT License](https://img.shields.io/badge/license-MIT-007d9c)](https://github.com/judwhite/go-svc/blob/main/LICENSE)
5[![Go Report Card](https://goreportcard.com/badge/github.com/judwhite/go-svc)](https://goreportcard.com/report/github.com/judwhite/go-svc)
6[![Build Status](https://github.com/judwhite/go-svc/workflows/tests/badge.svg)](https://github.com/judwhite/go-svc/actions?query=workflow%3Atests)
7
8Go Windows Service wrapper that plays nice with Linux. Windows tests [here](https://github.com/judwhite/go-svc/blob/main/svc_windows_test.go).
9
10## Project Status
11
12- Used in Production.
13- Maintained. Issues and Pull Requests will be responded to.
14
15## Go Modules
16
17* 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+`
18* `v1.1.3` and earlier can be imported using the previous import path
19* `v1.2+` code is backwards compatible with previous versions
20
21```nginx no-this-isnt-nginx-but-the-syntax-highlighting-works
22module awesomeProject
23
24go 1.15
25
26require github.com/judwhite/go-svc v1.2.0
27```
28
29```go
30import "github.com/judwhite/go-svc"
31```
32
33## Example
34
35```go
36package main
37
38import (
39	"log"
40	"sync"
41
42	"github.com/judwhite/go-svc"
43)
44
45// program implements svc.Service
46type program struct {
47	wg   sync.WaitGroup
48	quit chan struct{}
49}
50
51func main() {
52	prg := &program{}
53
54	// Call svc.Run to start your program/service.
55	if err := svc.Run(prg); err != nil {
56		log.Fatal(err)
57	}
58}
59
60func (p *program) Init(env svc.Environment) error {
61	log.Printf("is win service? %v\n", env.IsWindowsService())
62	return nil
63}
64
65func (p *program) Start() error {
66	// The Start method must not block, or Windows may assume your service failed
67	// to start. Launch a Goroutine here to do something interesting/blocking.
68
69	p.quit = make(chan struct{})
70
71	p.wg.Add(1)
72	go func() {
73		log.Println("Starting...")
74		<-p.quit
75		log.Println("Quit signal received...")
76		p.wg.Done()
77	}()
78
79	return nil
80}
81
82func (p *program) Stop() error {
83	// The Stop method is invoked by stopping the Windows service, or by pressing Ctrl+C on the console.
84	// This method may block, but it's a good idea to finish quickly or your process may be killed by
85	// Windows during a shutdown/reboot. As a general rule you shouldn't rely on graceful shutdown.
86
87	log.Println("Stopping...")
88	close(p.quit)
89	p.wg.Wait()
90	log.Println("Stopped.")
91	return nil
92}
93```
94
95## More Examples
96
97See the [example](https://github.com/judwhite/go-svc/tree/main/example) directory for more examples, including installing and uninstalling binaries built in Go as Windows services.
98
99## Similar Projects
100
101- https://github.com/kardianos/service
102
103## License
104
105`go-svc` is under the MIT license. See the [LICENSE](https://github.com/judwhite/go-svc/blob/main/LICENSE) file for details.
106