从可执行文件创建 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3582108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Windows service from executable
提问by German Latorre
Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?
给定一个可执行文件,有没有什么快速的方法可以创建一个在启动时启动它的 Windows 服务?
回答by Sergii Pozharov
To create a Windows Service from an executable, you can use sc.exe
:
要从可执行文件创建 Windows 服务,您可以使用sc.exe
:
sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"
You must have quotation marks around the actual exe
path, and a space after the binPath=
.
实际exe
路径周围必须有引号,binPath=
.后有一个空格。
More information on the sc
command can be found in Microsoft KB251192.
sc
可以在 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:
请注意,它不适用于任何可执行文件:可执行文件必须是 Windows 服务(即实现 ServiceMain)。将非服务可执行文件注册为服务时,尝试启动服务时会出现以下错误:
Error 1053: The service did not respond to the start or control request in a timely fashion.
错误 1053:服务没有及时响应启动或控制请求。
There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.
有一些工具可以从任意的非服务可执行文件创建 Windows 服务,请参阅其他答案以获取此类工具的示例。
回答by Kevin Tong
Use NSSM( the non-Sucking Service Manager) to run a .BAT or any .EXE file as a service.
使用 NSSM(非 Sucking Service Manager)将 .BAT 或任何 .EXE 文件作为服务运行。
- Step 1: Download NSSM
- Step 2: Install your sevice with
nssm.exe install [serviceName]
- Step 3: This will open a GUI which you will use to locate your executable
- 第 1 步:下载 NSSM
- 第 2 步:安装您的服务
nssm.exe install [serviceName]
- 第 3 步:这将打开一个 GUI,您将使用它来定位您的可执行文件
回答by Kevin Tong
Extending (Kevin Tong) answer.
延伸 (Kevin Tong) 的回答。
Step 1:Download & Unzip nssm-2.24.zip
第 1 步:下载并解压nssm-2.24.zip
Step 2:From command line type:
第 2 步:从命令行输入:
C:\> nssm.exe install [servicename]
C:\> nssm.exe install [servicename]
it will open GUI as below (the example is UT2003 server), then simply browse it to: yourapplication.exe
它将打开如下图形用户界面(示例为 UT2003 服务器),然后只需浏览到:yourapplication.exe
More information on: https://nssm.cc/usage
更多信息:https: //nssm.cc/usage
回答by KFL
Many existing answers include human intervention at install time. This can be an error-prone process. If you have many executables wanted to be installed as services, the last thing you want to do is to do them manually at install time.
许多现有的答案包括安装时的人工干预。这可能是一个容易出错的过程。如果您想将许多可执行文件安装为服务,那么您最不想做的就是在安装时手动执行它们。
Towards the above described scenario, I created serman, a command line tool to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run
针对上述场景,我创建了serman,这是一个命令行工具,用于将可执行文件安装为服务。您需要编写的所有内容(并且只编写一次)是一个简单的服务配置文件以及您的可执行文件。跑
serman install <path_to_config_file>
will install the service. stdout
and stderr
are all logged. For more info, take a look at the project website.
将安装服务。stdout
并且stderr
都被记录了。有关更多信息,请查看项目网站。
A working configuration file is very simple, as demonstrated below. But it also has many useful features such as <env>
and <persistent_env>
below.
工作配置文件非常简单,如下所示。但它也有许多有用的功能,例如<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>
回答by PodTech.io
these extras prove useful.. need to be executed as an administrator
这些附加功能证明很有用.. 需要以管理员身份执行
sc create <service_name> binpath=<binary_path>
sc stop <service_name>
sc queryex <service_name>
sc delete <service_name>
If your service name has any spaces, enclose in "quotes".
如果您的服务名称有任何空格,请用“引号”括起来。