Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The service did not respond to the start or control request in a timely fashion. #166

Open
sritejab opened this issue Mar 29, 2019 · 5 comments

Comments

@sritejab
Copy link

sritejab commented Mar 29, 2019

Hi, while trying to write a basic program using kardianos/service, I am facing the following error. I am able to install the service but when trying to start the service i get "The service did not respond to the start or control request in a timely fashion.". Here is my code

package main

import (
"fmt"
"os"
"github.com/kardianos/service"
"time"
)

type program struct{}

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

func (p *program) run() {
fmt.Println("In program: Run")
for {
time.Sleep(3 * time.Second)
fmt.Println("Still running")
}
}

func (p *program) Stop(s service.Service) error {
return nil
}

func main() {

var sc = &service.Config{
	Name:        "Go Service Example - 2",
	DisplayName: "GoServiceExample2",
	Description: "Example of a go service",
}

prg := &program{}
s, err := service.New(prg, sc)
if err != nil {

}

strCmdArg := os.Args[1]

switch strCmdArg {

case "install":
	err = s.Install()
	if err != nil {
		fmt.Println(err)
	}

	err = s.Start()
	if err != nil {
		fmt.Println(err)
	}
	os.Exit(0)

case "start":
	err = s.Start()
	if err != nil {
		fmt.Println(err)
	}
	os.Exit(0)

case "stop":
	err = s.Stop()
	if err != nil {
		fmt.Println(err)
	}

	os.Exit(0)

case "uninstall":
	err = s.Stop()
	if err != nil {
		fmt.Println(err)
	}

	err = s.Uninstall()
	if err != nil {
		fmt.Println(err)
	}
	os.Exit(0)
case "service":
	err = s.Run()
	if err != nil {
		fmt.Println(err)
	}

default:
	os.Exit(0)
}

}

Please help

@kardianos
Copy link
Owner

When run as a service, s.Run will never be called.
Put s.Run in the default case for the service to work.

@sritejab
Copy link
Author

sritejab commented Apr 2, 2019

I have done as mentioned above and it did not work. However, when I sent a string as arguments like
ServiceArg := make([]string, 1)
ServiceArg[0] = "service"

svcConfig := &service.Config{
	Name:        "GoServiceExampleSimple",
	DisplayName: "Go Service Example",
	Description: "This is an example Go service.",
	Arguments:   ServiceArg,
}

It did work.
Trying to figure out what might be the reason.
Thanks

@shenlanzifa
Copy link

the reason is because the code "strCmdArg := os.Args[1]"。when you do not sent a arguments, the process will panic。

@projectweekend
Copy link

For what it's worth, I was dealing with a similar issue here. My executable ran just fine on its own, but as a service, it would not start. I ended up copying the full path to the executable from the Service properties and attempting to run that in PowerShell....and...BAM! There was the error I needed to see. I was using viper to load configuration from a JSON file, but I forgot to put the file in my user directory where the Service could find it.

@shaunenslin
Copy link

shaunenslin commented Oct 1, 2019

Hi Anyone
In my example, I send in some flags when starting the service and it seems to set up the windows service correctly.
If I copy the fill path from service manager, then as attached, it runs fine.
However, when starting the service I also get above error. I have put in some logging in the main program, but that does not seem to be called at all for service=run

My main program calls ControlService with the -run flag, so as shown I have setup the service to call the program with the -service=run flag.

Any idea's?

Screenshot 2019-10-01 at 18 24 43

Here is my service go file:


func (p *program) run() {
	logger.Info("Running service, getting properties")
	// initialize properties
	prp, err := properties.New(p.organization, p.orgpassword)
	if err != nil {
		logger.Error(err)
		return
	}
	p.infoLog, p.errLog = file.InitLogs(false, prp.LogFolder, "rapiddump")

	p.extract = prp.GetExtract(p.extractName)
	logger.Info("Running for extract:" + p.extract.Name)

	p.infoLog.Println("Checking tables for " + p.extract.Name)
	//loop until service told to stop
	for {
		select {
		default:
			err := p.checkTables()
			if err != nil {
				p.errLog.Print(err)
				return
			}
			time.Sleep(time.Minute)
		case <-p.exit:
			logger.Info("Service stopped")
			p.infoLog.Println("Service Stopped!")
			return
		}
	}
}

// ControlService when either running as a service, or controlling the service with run|stop|install etc.
func ControlService(cmd string, organization string, orgpassword string, extractName string, verbose bool) {
	if extractName == "" {
		log.Fatal("You need an -extract flag when using the -service flag")
	}
	// Setup our config
	svcConfig := &service.Config{
		Arguments:   []string{"-service=run", "-extract=" + extractName},
		Name:        "rapiddump" + extractName,
		DisplayName: "Rapiddump extract service for " + extractName,
		Description: "This service will wake up every 10 minutes",
	}
	// setup the service program
	prg := &program{
		organization: organization,
		orgpassword:  orgpassword,
		extractName:  extractName,
		verbose:      verbose,
	}
	s, err := service.New(prg, svcConfig)
	if err != nil {
		log.Fatal(err)
	}
	//logger to log in system event logs
	errs := make(chan error, 5)
	logger, err = s.Logger(errs)
	if err != nil {
		log.Fatal(err)
	}

	// either run the service, or stop/start/install/uninstall via service.Control()
	switch cmd {
	case "run":
		s.Run()
	default:
		err := service.Control(s, cmd)
		if err != nil {
			//logger.Printf("%s not valid, Valid actions: %q\n%v", cmd, service.ControlAction, err)
			log.Fatal(err)
		}
		return
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants