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.
Kev*_*ong 221
使用NSSM(非Sucking Service Manager)将.BAT或任何.EXE文件作为服务运行.
nssm.exe install [serviceName]小智 90
延伸(Kevin Tong)回答.
第1步:下载并解压缩nssm-2.24.zip
第2步:从命令行类型:
C:\> nssm.exe install [servicename]
它将打开如下GUI(示例是UT2003服务器),然后只需将其浏览到:yourapplication.exe

有关以下内容的更多信息:https://nssm.cc/usage
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 的可执行文件。
这些额外的东西证明是有用的..需要以管理员身份执行
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)
如果您的服务名称包含任何空格,请用"引号"括起来.
一些最热门的答案都指向 NSSM。该项目自 2017 年以来就没有更新过。
类似功能的更现代替代方案是https://github.com/winsw/winsw/ - 包含多个示例和良好的文档。
为了方便使用,这帮助我比 NSSM 更快地设置它:如果您将winsw可执行文件命名为myapp.exe并创建 XML 配置文件myapp.xml,您只需运行myapp.exe install即可myapp.exe start,瞧,服务正在运行。
| 归档时间: |
|
| 查看次数: |
619333 次 |
| 最近记录: |