从可执行文件创建 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:33:56  来源:igfitidea点击:

Create Windows service from executable

windowswindows-services

提问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 exepath, and a space after the binPath=.

实际exe路径周围必须有引号,binPath=.后有一个空格。

More information on the sccommand 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 文件作为服务运行。

http://nssm.cc/

http://nssm.cc/

  • 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

enter image description here

在此处输入图片说明

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. stdoutand stderrare 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".

如果您的服务名称有任何空格,请用“引号”括起来。

回答by A. Masson

I've tested a good product for that: AlwaysUp. Not free but they have a 30 days trial period so you can give it a try...

我为此测试了一个很好的产品:AlwaysUp。不是免费的,但他们有 30 天的试用期,所以你可以试一试......