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

Http Server as windows service #108

Open
charliemaiors opened this issue Nov 11, 2017 · 18 comments
Open

Http Server as windows service #108

charliemaiors opened this issue Nov 11, 2017 · 18 comments

Comments

@charliemaiors
Copy link

Hi, I've defined simple service which stars an HTTP server I've defined the service installation here but when I try to start service from PowerShell or simple services.msc it will return the error 1053: the service did not respond to the start or control request in a timely fashion.

@mahaben
Copy link

mahaben commented Jan 26, 2018

@charliemaiors I'm facing the same issue. Did you find some workaround? Thanks

@charliemaiors
Copy link
Author

Yes I'ive created a Powershell script which install nssm from chocolatey and define the service in nssm.

@mahaben
Copy link

mahaben commented Jan 26, 2018

I was using nssm previously and I don't want to use it anymore. I'll try to find out a solution. Thank you anyway.

@bbalet
Copy link

bbalet commented Jan 27, 2018

For Windows, the main function must be the fastest possible so as to return very quickly a kind of ack to the OS. So you shouldn't do some many operations such as initialising things but be as brief as possible. Loading configuration, initialising stuffs and doing the job should be delegated to another function that is the service. See an example here (in French, but the sample code is commented on English): http://decouvric.cluster013.ovh.net/golang/thirdparty/divers/creer-un-service-golang-avec-kardianos.html

@mahaben
Copy link

mahaben commented Jan 29, 2018

Thanks. But I can't understand when the function s.Run is getting called?

@bbalet
Copy link

bbalet commented Jan 29, 2018

The part of code where s.Run is used is the service itself. As you can see, s.Run is called very quickly (nothing big is initialised before the call and no parameter is given to a Windows service). This is the pattern you must follow if you want to code a Windows service.

@nkev
Copy link

nkev commented May 15, 2018

My HTTP is unreachable when running under Windows service, even via localhost. Works fine if not running under Windows service. My s.Run is fast and and there is no error that I can see in the Windows Event logs. Has anybody encountered this?

@kardianos
Copy link
Owner

kardianos commented May 15, 2018 via email

@nkev
Copy link

nkev commented May 15, 2018

Yes, I looked at that and couldn't see anything wrong. If the firewall was the issue, the service would be blocked when running stand-alone (not as Windows service), but it works fine that way.

@bbalet
Copy link

bbalet commented May 15, 2018

Windows Firewall can block the network port used by your service, even when accessed from localhost to localhost. Try to disable it, if it is the root cause, allow the port.
An antivirus could be the root cause too.

@bbalet
Copy link

bbalet commented May 15, 2018

When your program is executed as a stand alone process, it doesn't use the same account than when it runs as a service (so the level of privileges and the context differ).

@PauloSalum
Copy link

I was using nssm previously and I don't want to use it anymore. I'll try to find out a solution. Thank you anyway.

@mahaben did you find a solution for this? I'm having the same issue.

@kardianos
Copy link
Owner

Your start function must return in milliseconds.
If in doubt post code.

@Sozialarchiv
Copy link

Hi I have the same problem. If there is nothing to do everything work. As soon as I add an http server I get the 1053 error:

package main

import (
	"fmt"
	"github.com/kardianos/service"
	"log"
	"ims/webserver"
	"time"
)

var logger service.Logger

type program struct{}

func (p *program) Start(s service.Service) error {
	// Start should not block. Do the actual work async.
	go p.run()
	return nil
}
func (p *program) run() {
	fmt.Println(time.Now().Format("2006-01-02 03:04:05 PM"), "Service started")
	webserver.StartWebserver()
}
func (p *program) Stop(s service.Service) error {
	// Stop should not block. Return with a few seconds.
	return nil
}

func main() {
	svcConfig := &service.Config{
		Name:        "ims",
		DisplayName: "IMS",
		Description: "IMS",
	}

	prg := &program{}
	s, err := service.New(prg, svcConfig)
	if err != nil {
		log.Fatal(err)
	}
	logger, err = s.Logger(nil)
	if err != nil {
		log.Fatal(err)
	}
	err = s.Run()
	if err != nil {
		logger.Error(err)
	}
}

Thank you for your help.

@kardianos
Copy link
Owner

Maybe "webserver.StartWebserver()" is panicing? Does it work if you comment that out?

@Sozialarchiv
Copy link

Sozialarchiv commented Feb 7, 2019

Maybe "webserver.StartWebserver()" is panicing? Does it work if you comment that out?

You are right. It was a panic, that was just occuring if it was running as service. I solved the problem and now everthing works great.

Thank you for your fast help and your great package!

@NadavTalIsr
Copy link

Maybe "webserver.StartWebserver()" is panicing? Does it work if you comment that out?

You are right. It was a panic, that was just occuring if it was running as service. I solved the problem and now everthing works great.

Thank you for your fast help and your great package!

how you solved it? i have the same issue

@Sozialarchiv
Copy link

how you solved it? i have the same issue

I am sorry I could not find any more code for this.

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

8 participants