如题

今天在删除 Windows 操作系统下的 nginx 服务时,遇到一个问题,就是删除之后,在 windows service 控制面板中一直能看到 nginx 服务。

1
2
3
4
c:\nginx-1.12.0>winsw uninstall
2017-05-08 11:38:55,635 INFO - Starting ServiceWrapper in the CLI mode
2017-05-08 11:38:55,958 INFO - Uninstalling the service with id 'nginx'
2017-05-08 11:38:55,968 INFO - Completed. Exit code is 0

运行上述命令之后,服务里还是有 nginx 只不过是 Disabled 状态。
再次运行下面的命令,提示 The specified service has been marked for deletion,汗:

1
2
3
4
c:\nginx-1.12.0>sc delete nginx
[SC] DeleteService FAILED 1072:

The specified service has been marked for deletion.

这时,如果再次安装 nginx 服务,会失败:

1
2
3
4
5
6
7
8
9
10
11
C:\nginx-1.12.0>winsw install
2017-05-08 11:40:35,487 INFO - Starting ServiceWrapper in the CLI mode
Service with id 'nginx' already exists
To install the service, delete the existing one or change service Id in the configuration file
2017-05-08 11:40:35,769 FATAL - Unhandled exception
System.Exception: Installation failure: Service with id 'nginx' already exists
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)
System.Exception: Installation failure: Service with id 'nginx' already exists
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)

问题原因及解决办法

  1. 导致 windows service 不能部署,也不能被删除,使用 sc delete serviceName 命令也不奏效。是因为 windows service database 缓存的原因,简单粗暴的办法就是重启操作系统。
  2. 我遇到这个问题的原因是我的 windows service 控制面板没有完全关闭,之前打开的 nginx 属性窗口还开着,又点开了另外一个服务窗口,导致 nginx 服务信息一下存在于 windows 缓存当中,关闭 windows service 控制面板,问题解决。
  3. 通常,可以尝试下面的办法解决这种问题:

    1. 查找 windows service 的 PID:运行 queryex service_name,例如:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      C:\nginx-1.12.0>sc queryex nginx

      SERVICE_NAME: nginx
      TYPE : 10 WIN32_OWN_PROCESS
      STATE : 1 STOPPED
      WIN32_EXIT_CODE : 0 (0x0)
      SERVICE_EXIT_CODE : 0 (0x0)
      CHECKPOINT : 0x0
      WAIT_HINT : 0x0
      PID : 0
      FLAGS :
    2. 如果 PID 项不为0,则杀掉进程:taskkill /PID service_pid /f

    3. 如果 PID 为0,则很有可能遇到了跟我一样的问题,需要关闭所有 windows service 控制面板。

如何在 windows 下安装和删除 nginx 服务,请看我的另外一篇文章:把 Nginx 安装为 Windows 的一个服务

没有打开 windows service 控制面板的情况下,删除服务的提示应该是 DeleteService SUCCESS

1
2
C:\nginx-1.12.0>sc delete nginx
[SC] DeleteService SUCCESS