Create Windows service from executable

Ger*_*rre 320 windows windows-services

Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?

Ser*_*rov 428

To create a Windows Service from an executable, you can use sc.exe:

sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"
Run Code Online (Sandbox Code Playgroud)

You must have quotation marks around the actual exe path, and a space after the binPath=.

More information on the sc command can be found in Microsoft KB251192.

Note that it will not work for just any executable: the executable must be a Windows Service (i.e. implement ServiceMain). When registering a non-service executable as a service, you'll get the following error upon trying to start the service:

Error 1053: The service did not respond to the start or control request in a timely fashion.

There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.

  • 您(几乎可以肯定)必须以管理员身份运行命令提示符才能使此命令生效 (7认同)
  • 该路径还需要是完全限定的路径 - 我无法通过使用相对路径来启动我的服务。 (3认同)
  • `binpath =`之后的空格以及必须用双引号括起可执行路径是完全错误的,至少对于Windows 10.当且仅当路径包含像空格这样的特殊字符时才需要引用.另外,套管(lowe/upper/mixed-case)在任何地方都无关紧要,在变量名中也是如此,`displayname ="my service"`是另一个在创建服务时传递命令行的好东西,作为第一行查看(`name`)在`services.msc`. (3认同)
  • 我在 Windows 7 上需要`binPath=` 之后的空格,但在 Windows 10 上不需要 (3认同)

Kev*_*ong 221

使用NSSM(非Sucking Service Manager)将.BAT或任何.EXE文件作为服务运行.

http://nssm.cc/

  • 第1步:下载NSSM
  • 第2步:安装你的服务nssm.exe install [serviceName]
  • 第3步:这将打开一个GUI,您将使用它来查找可执行文件

  • 这绝对是伟大的,我希望我能接受这个答案,而不是第一个,:-( (9认同)
  • 最好的服务经理.我甚至设法让PlexWatch使用NSSM作为服务安装. (6认同)
  • 这套是永远的服务吗?每次windows启动服务都会启动吗?还有我怎么能在没有用户交互的情况下做到这一点?某种脚本或代码? (2认同)
  • 我可以在服务器上将 Dropbox 作为服务运行。绝对不吸的工具! (2认同)

小智 90

延伸(Kevin Tong)回答.

第1步:下载并解压缩nssm-2.24.zip

第2步:从命令行类型:

C:\> nssm.exe install [servicename]

它将打开如下GUI(示例是UT2003服务器),然后只需将其浏览到:yourapplication.exe

在此输入图像描述

有关以下内容的更多信息:https://nssm.cc/usage

  • 正确的语法`nssm.exe install [serviceName]`.此解决方案有效但如果您有GUI应用程序,它将无法在Win Serever2003上运行.如果您以后想要删除它,请使用`nssm.exe remove [youservicename]` (4认同)
  • 当我尝试使用 nssm 时,我的 Windows 窗体正在运行,但未显示窗体...为什么? (2认同)

KFL*_*KFL 15

许多现有答案包括安装时的人为干预.这可能是一个容易出错的过程.如果您有许多可执行文件想要作为服务安装,那么您要做的最后一件事是在安装时手动执行它们.

针对上述场景,我创建了serman,一个命令行工具,用于将可执行文件安装为服务.您需要编写的所有内容(只编写一次)是一个简单的服务配置文件以及您的可执行文件.跑

serman install <path_to_config_file>
Run Code Online (Sandbox Code Playgroud)

将安装该服务.stdout并且stderr全部记录.有关详细信息,请查看项目网站.

工作配置文件非常简单,如下所示.但它也有许多有用的功能,如下面<env><persistent_env>下面.

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable to the system.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>
Run Code Online (Sandbox Code Playgroud)


sta*_*tor 11

与Sergii Pozharov 的答案相同,但使用 PowerShell cmdlet:

New-Service -Name "MyService" -BinaryPathName "C:\Path\to\myservice.exe"
Run Code Online (Sandbox Code Playgroud)

请参阅New-Service参考资料 以获取更多定制信息。

这仅适用于已经实现Windows 服务 API 的可执行文件。


Pod*_*.io 7

这些额外的东西证明是有用的..需要以管理员身份执行

sc install <service_name> binpath=<binary_path>
sc stop    <service_name>
sc queryex <service_name>
sc delete  <service_name>
Run Code Online (Sandbox Code Playgroud)

如果您的服务名称包含任何空格,请用"引号"括起来.


use*_*062 6

一些最热门的答案都指向 NSSM。该项目自 2017 年以来就没有更新过。

类似功能的更现代替代方案是https://github.com/winsw/winsw/ - 包含多个示例和良好的文档。

为了方便使用,这帮助我比 NSSM 更快地设置它:如果您将winsw可执行文件命名为myapp.exe并创建 XML 配置文件myapp.xml,您只需运行myapp.exe install即可myapp.exe start,瞧,服务正在运行。