Search code examples
windowsservicewindows-servicesconsulnssm

Consul Windows Service no longer starts with error "Incorrect function"


So at one point I had consul running on Windows 7. This was several months ago. Recently I started looking back at consul, so I installed it using the Chocolatey package found here on the Chocolatey website.

First off, the package always throws an error when it tries to start the service. The error:

c o n s u l :   U n e x p e c t e d   s t a t u s   S E R V I C E _ S T O P P E D   i n   r e s p o n s e   t o   S T A R T   c o n t r o l .

That doesn't make Chocolatey think that the package failed to install though. Afterwards, I try dumping out the service using nssm and this is what appears:

C:\ProgramData\chocolatey PS>nssm dump consul
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe install consul C:\ProgramData\chocolatey\lib\consul\tools\consul.exe
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppParameters "agent -ui -config-dir=C:\ProgramData\consul\config -data-dir=C:\ProgramData\consul\data -bind '127.0.0.1' -bootstrap -server"
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppDirectory C:\ProgramData\chocolatey\lib\consul\tools
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppExit Default Exit
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppEnvironmentExtra :GOMAXPROCS=2
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppStdout C:\ProgramData\consul\logs\consul-output.log
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppStderr C:\ProgramData\consul\logs\consul-error.log
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppRotateFiles 1
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppRotateOnline 1
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul AppRotateBytes 10485760
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul DisplayName consul
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul ObjectName "NT Authority\NetworkService"
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul Start SERVICE_AUTO_START
C:\ProgramData\chocolatey\lib\NSSM\tools\nssm.exe set consul Type SERVICE_WIN32_OWN_PROCESS

If I take the above AppParameters and paste them after consul, it works perfectly. So, I'm at a lost to why I cannot start the service.

C:\ProgramData\chocolatey PS>start-service consul
start-service : Service 'consul (consul)' cannot be started due to the following error: Cannot start service consul on computer '.'.
At line:1 char:1
+ start-service consul
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

When I look at the Event Viewer it tells me this error message:

The consul service terminated with the following service-specific error: 
Incorrect function.

Has anyone got the consul Chocolatey package to work recently on Windows? Is there a chance that this is a Windows bug, or a NSSM bug, or even a consul bug? I tried different versions of NSSM and different versions of consul with the same result. Any help I can get to get consul running on Windows would be appreciated.


Solution

  • Just in case someone else runs across this issue, I had a couple of problems that I needed to address to get the service to startup.

    First off, I had multiple private IP addresses and when I installed the package using Chocolatey, I needed to specify the IP address using either the --params option with the choco install command or by specifying it through a consul configuration file. I did this above, but I wasted a lot of time at first due to that. Also, when specifying the IP address to bind to, you need to also indicate that you need consul to run in server mode. So just downloading the package and trying to run consul won't work in some cases. Also, the = sign is pretty important when specifying parameters with consul. Sometimes you can get away with just adding a space and using no quotes, but I would recommend that you always specify the parameters using =. The final command I used to get it running on my local Windows 7 machine was this:

    cinst consul --yes --force --params='"-server -bootstrap -bind=""127.0.0.1"" "'

    There were several things I missed here during troubleshooting.

    1. If you already have consul installed, make sure the service is stopped and ensure there's no consul process running before trying to reinstall consul using Chocolatey. To make sure, run this in PowerShell:

    Stop-Service consul; Get-Process consul -ErrorAction SilentlyIgnore | Stop-Process -Force

    1. If the service won't start look at where the Chocolatey package places the log files. For me it was under C:\ProgramData\consul\logs\consul-error.log.

    2. I didn't learn about NSSM first. It's really an awesome tool that allows you to easily debug why the service won't start. If you want to tweak the start parameters for consul just run this in your console:

    nssm edit consul