service icon indicating copy to clipboard operation
service copied to clipboard

1053 error on starting the service on Windows 10

Open Kaitou786 opened this issue 4 years ago • 3 comments

Hi, I have been trying to have the server running in a windows 10 as a service but getting 1053 error. From reading the comments and doc I can't seem to figure out what could be wrong, The program.Start() command is running program.Run() as a go routine and exiting early, the program.Stop() should be stopping the server, If I run the svc.Run() (using the canary-checker service run) command directly from the Command Prompt in windows it works so I don't think there is any runtime error. Any insight on what might be going wrong would be really helpful, thanks! the code:

package cmd

import (
	"fmt"
	"log"
	nethttp "net/http"
	"os"
	"path/filepath"

	"github.com/kardianos/service"
	"github.com/spf13/cobra"
)

var svc service.Service
var prg = &program{
	server: &nethttp.Server{},
}
var err error

var Service = &cobra.Command{
	Use: "service",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		configFile = "canary-checker.yaml"
		path, err := os.Executable()
		if err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}
		configFile = filepath.Join(filepath.Dir(path), configFile)
		ServiceConfig.Arguments = getArguments()
		// service.
		svc, err = service.New(prg, ServiceConfig)
		if err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}
		serviceLogger, err = svc.Logger(nil)
		if err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}

	},
}

var InstallService = &cobra.Command{
	Use:   "install",
	Short: "Install Canary Checker as a Service",
	Run: func(cmd *cobra.Command, args []string) {
		if err := svc.Install(); err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}
		serviceLogger.Info("Service Installed Successfully.") // nolint: errcheck
	},
}

var UninstallService = &cobra.Command{
	Use:   "uninstall",
	Short: "Uninstall Canary Checker as a Service",
	Run: func(cmd *cobra.Command, args []string) {
		err := svc.Uninstall()
		if err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}
		serviceLogger.Info("Service Uninstalled Successfully.") // nolint: errcheck
	},
}

var RunService = &cobra.Command{
	Use: "run",
	Run: func(cmd *cobra.Command, args []string) {
		err = svc.Run()
		if err != nil {
			fmt.Println(err)
			log.Print(err)
			serviceLogger.Error(err)
			return
		}
	},
}

var StartService = &cobra.Command{
	Use: "start",
	Run: func(cmd *cobra.Command, args []string) {
		err := svc.Start()
		if err != nil {
			serviceLogger.Error(err)
			return
		}
	},
}

var StopService = &cobra.Command{
	Use: "stop",
	Run: func(cmd *cobra.Command, args []string) {
		err := svc.Stop()
		if err != nil {
			serviceLogger.Error(err) // nolint: errcheck
			return
		}
	},
}

var RestartService = &cobra.Command{
	Use: "restart",
	Run: func(cmd *cobra.Command, args []string) {
		svc.Restart()
	},
}

var serviceLogger service.Logger

type program struct {
	server *nethttp.Server
}

var ServiceConfig = &service.Config{
	Name:        "canary-checker",
	DisplayName: "Canary Checker Server",
	Description: "Starts the canary checker server",
}

func (p *program) Start(s service.Service) error {
	go p.run()
	return nil
}

func (p *program) run() {
	serviceLogger.Info("starting server")
	p.server = serverRun()
	p.server.ListenAndServe()
}

func (p *program) Stop(s service.Service) error {
	err := p.server.Close()
	if err != nil {
		serviceLogger.Error(err)
	}
	return nil
}

func getArguments() []string {
	arguments := []string{"service", "run"}
	return arguments
}

func init() {
	Service.AddCommand(InstallService, RunService, UninstallService, StartService, StopService, RestartService)
	RunService.Flags().StringVarP(&configFile, "configfile", "c", "canary-checker.yaml", "Path to the config file")
	InstallService.Flags().StringVarP(&configFile, "configfile", "c", "canary-checker.yaml", "Path to the config file")
	CommonFlags(InstallService.Flags())
	CommonFlags(RunService.Flags())
}

Kaitou786 avatar Oct 06 '21 18:10 Kaitou786

@kardianos any input would be really helpful. Thanks

Kaitou786 avatar Oct 14 '21 07:10 Kaitou786

Is this issue any progress?

H-hao avatar Aug 18 '22 03:08 H-hao

I try with rsrc github.com/akavel/rsrc , I can start or stop windows service. I wan't to get program log ,but I can't see terminal . How will I do?

nihility23 avatar Mar 09 '23 03:03 nihility23