Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

77 righe
1.7 KiB

  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.
  4. // +build windows
  5. // Example service program that beeps.
  6. //
  7. // The program demonstrates how to create Windows service and
  8. // install / remove it on a computer. It also shows how to
  9. // stop / start / pause / continue any service, and how to
  10. // write to event log. It also shows how to use debug
  11. // facilities available in debug package.
  12. //
  13. package main
  14. import (
  15. "fmt"
  16. "log"
  17. "os"
  18. "strings"
  19. "golang.org/x/sys/windows/svc"
  20. )
  21. func usage(errmsg string) {
  22. fmt.Fprintf(os.Stderr,
  23. "%s\n\n"+
  24. "usage: %s <command>\n"+
  25. " where <command> is one of\n"+
  26. " install, remove, debug, start, stop, pause or continue.\n",
  27. errmsg, os.Args[0])
  28. os.Exit(2)
  29. }
  30. func main() {
  31. const svcName = "myservice"
  32. isIntSess, err := svc.IsAnInteractiveSession()
  33. if err != nil {
  34. log.Fatalf("failed to determine if we are running in an interactive session: %v", err)
  35. }
  36. if !isIntSess {
  37. runService(svcName, false)
  38. return
  39. }
  40. if len(os.Args) < 2 {
  41. usage("no command specified")
  42. }
  43. cmd := strings.ToLower(os.Args[1])
  44. switch cmd {
  45. case "debug":
  46. runService(svcName, true)
  47. return
  48. case "install":
  49. err = installService(svcName, "my service")
  50. case "remove":
  51. err = removeService(svcName)
  52. case "start":
  53. err = startService(svcName)
  54. case "stop":
  55. err = controlService(svcName, svc.Stop, svc.Stopped)
  56. case "pause":
  57. err = controlService(svcName, svc.Pause, svc.Paused)
  58. case "continue":
  59. err = controlService(svcName, svc.Continue, svc.Running)
  60. default:
  61. usage(fmt.Sprintf("invalid command %s", cmd))
  62. }
  63. if err != nil {
  64. log.Fatalf("failed to %s %s: %v", cmd, svcName, err)
  65. }
  66. return
  67. }