kardianos / service

Run go programs as a service on major platforms.
zlib License
4.43k stars 677 forks source link

Http Server as windows service #108

Open charliemaiors opened 6 years ago

charliemaiors commented 6 years ago

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 commented 6 years ago

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

charliemaiors commented 6 years ago

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

mahaben commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

bbalet commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Check your system firewall.

On Mon, May 14, 2018, 19:16 Nuri notifications@github.com wrote:

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?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kardianos/service/issues/108#issuecomment-389021003, or mute the thread https://github.com/notifications/unsubscribe-auth/AAuFsZ8cjD-KJ7l5Z9tZIat9unW41j-3ks5tyjqJgaJpZM4QadAk .

nkev commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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 commented 5 years ago

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

Sozialarchiv commented 5 years ago

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 commented 5 years ago

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

Sozialarchiv commented 5 years ago

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 commented 2 years ago

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 commented 2 years ago

how you solved it? i have the same issue

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