blob: 9bf9bbdc512fc098426b5ee41cce1f1b6202c5d7 [file] [log] [blame]
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
// Example service program that beeps.
// The program demonstrates how to create Windows service and
// install / remove it on a computer. It also shows how to
// stop / start / pause / continue any service, and how to
// write to event log. It also shows how to use debug
// facilities available in debug package.
package main
import (
func usage(errmsg string) {
"usage: %s <command>\n"+
" where <command> is one of\n"+
" install, remove, debug, start, stop, pause or continue.\n",
errmsg, os.Args[0])
func main() {
const svcName = "myservice"
inService, err := svc.IsWindowsService()
if err != nil {
log.Fatalf("failed to determine if we are running in service: %v", err)
if inService {
runService(svcName, false)
if len(os.Args) < 2 {
usage("no command specified")
cmd := strings.ToLower(os.Args[1])
switch cmd {
case "debug":
runService(svcName, true)
case "install":
err = installService(svcName, "my service")
case "remove":
err = removeService(svcName)
case "start":
err = startService(svcName)
case "stop":
err = controlService(svcName, svc.Stop, svc.Stopped)
case "pause":
err = controlService(svcName, svc.Pause, svc.Paused)
case "continue":
err = controlService(svcName, svc.Continue, svc.Running)
usage(fmt.Sprintf("invalid command %s", cmd))
if err != nil {
log.Fatalf("failed to %s %s: %v", cmd, svcName, err)