github.com/zhongdalu/gf@v1.0.0/third/golang.org/x/sys/windows/svc/example/main.go (about)

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