{"record":{"id":"68f641aca8db653f","repo":"hashicorp/nomad","slug":"unable-to-stop-existing-service-w","errorCode":null,"errorMessage":"unable to stop existing service - %w","messagePattern":"unable to stop existing service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_install.go","lineNumber":158,"sourceCode":"\t\tnmdSvc, err := m.GetService(winsvc.WINDOWS_SERVICE_NAME)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not get existing service to stop - %w\", err)\n\t\t}\n\n\t\t// If the service is running and the reinstall\n\t\t// option was not set, return an error\n\t\trunning, err := nmdSvc.IsRunning()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to determine service state - %w\", err)\n\t\t}\n\t\tif running && !opts.reinstall {\n\t\t\treturn fmt.Errorf(\"service is already running. Please run the\\ncommand again with -reinstall to stop the service and install.\")\n\t\t}\n\n\t\tif running {\n\t\t\tc.Ui.Output(\"  Stopping existing nomad service\")\n\t\t\tif err := nmdSvc.Stop(); err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to stop existing service - %w\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Install the nomad binary into the system\n\tif err = c.binaryInstall(opts); err != nil {\n\t\treturn fmt.Errorf(\"binary install failed - %w\", err)\n\t}\n\n\tc.Ui.Output(fmt.Sprintf(\"  Nomad binary installed to: %s\", opts.binaryPath))\n\n\t// Create a configuration directory and add\n\t// a basic configuration file if no configuration\n\t// currently exists\n\tif err = c.configInstall(opts); err != nil {\n\t\treturn fmt.Errorf(\"configuration install failed - %w\", err)\n\t}\n","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_install.go#L140-L176","documentation":"Wraps the failure of stopping an already-running Nomad Windows service during `windows install`. The command detected a running service and called Windows service manager Stop; the underlying SCM error is preserved via %w. Install aborts so the stale binary/service state is not half-replaced.","triggerScenarios":"Running `nomad windows install` while the Nomad service is running and the golang.org/x/sys windows svc manager Stop call fails (access denied, service marked for deletion, SCM timeout).","commonSituations":"Installing over a service started under a different elevation; prior uninstall left the service in marked-for-deletion state; service hung and did not respond to the stop control within the timeout.","solutions":["Re-run the command with -reinstall so the flow stops the service itself before installing","Open an elevated (Administrator) PowerShell/cmd and retry the install","Run `sc query nomad` / `sc.exe stop nomad` manually, wait for STOPPED state, then install again","Reboot to clear a service stuck in marked-for-deletion or stop-pending state"],"exampleFix":"// before\nnomad windows install\n// after\nnomad windows install -reinstall","handlingStrategy":"validation","validationCode":"sc := exec.Command(\"sc.exe\", \"query\", \"nomad\"); out, err := sc.Output()\nif err == nil && strings.Contains(string(out), \"RUNNING\") {\n\tfmt.Println(\"Service running — use -reinstall or stop it first:\",\n\t\texec.Command(\"sc.exe\", \"stop\", \"nomad\").Run())\n}","typeGuard":null,"tryCatchPattern":"if err := nmdSvc.Stop(); err != nil {\n\tvar exitErr *exec.ExitError\n\tif errors.As(err, &exitErr) && isAccessDenied(exitErr) {\n\t\t// prompt for elevation and retry\n\t}\n\treturn fmt.Errorf(\"unable to stop existing service - %w\", err)\n}","preventionTips":["Always run `nomad windows install` from an elevated shell","Check `sc query nomad` state before installing","Use -reinstall when a service is already present","Reboot to clear marked-for-deletion services"],"tags":["windows","service-control-manager","installation"],"backgroundTag":"windows-service-stop-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}