1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Example service program that beeps.
9 // The program demonstrates how to create Windows service and
10 // install / remove it on a computer. It also shows how to
11 // stop / start / pause / continue any service, and how to
12 // write to event log. It also shows how to use debug
13 // facilities available in debug package.
23 "golang.org/x/sys/windows/svc"
26 func usage(errmsg string) {
27 fmt.Fprintf(os.Stderr,
29 "usage: %s <command>\n"+
30 " where <command> is one of\n"+
31 " install, remove, debug, start, stop, pause or continue.\n",
37 const svcName = "myservice"
39 isIntSess, err := svc.IsAnInteractiveSession()
41 log.Fatalf("failed to determine if we are running in an interactive session: %v", err)
44 runService(svcName, false)
49 usage("no command specified")
52 cmd := strings.ToLower(os.Args[1])
55 runService(svcName, true)
58 err = installService(svcName, "my service")
60 err = removeService(svcName)
62 err = startService(svcName)
64 err = controlService(svcName, svc.Stop, svc.Stopped)
66 err = controlService(svcName, svc.Pause, svc.Paused)
68 err = controlService(svcName, svc.Continue, svc.Running)
70 usage(fmt.Sprintf("invalid command %s", cmd))
73 log.Fatalf("failed to %s %s: %v", cmd, svcName, err)