把Windows应用程序应用程序exe做成windows服务

将Windows应用程序应用程序exe做成windows服务

问题:

sc命令注册exe为服务时显示失败,提示代码1053,如何解决?

使用SC命令将EXE文件注册为Windows服务时,如果遇到错误代码1053(“服务没有及时响应启动或控制请求"),通常是因为目标程序未按Windows服务的标准实现。

具体原因可能包括:

程序未包含服务入口点(ServiceMain函数)、

未正确处理服务控制管理器的通信,或者启动超时。

解决方法如下:

1.确保EXE是专门设计为Windows服务的程序。

2.如果是普通应用程序,可借助工具如

'nssm'(Non-Sucking Service Manager) 将其包装为服务。

3.检查服务配置参数是否正确,特别是启动路径和依赖项。

4. 增加服务启动超时时间:通过注册表键

'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control下的‘ServicesPipeTimeout 值进行调整。

cmd执行如下命令:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control" /v ServicesPipeTimeout /t REG_DWORD /d 60000 /f

5. 以管理员权限运行

CMD井重新執行SC命。

6、 如果用SC命令不能删除服务,可以从注册表里删除。

reg delete HKLM/SYSTEM/CurrentControlSet/Services/ServiceName 

示例命:

sc create MyService binPath= "C\path\tolyour.exe" start= auto

1、使用windows自带的命令sc

首先我们要打开cmd,下面的命令在cmd中运行,最好使用管理员运行cmd

1、注册服务:

sc create demo服务 binpath= D:\demo服务\demo服务.exe type= own start= auto displayname= demo服务

介绍参数

binpath:你的应用程序所在的路径。

displayname:服务显示的名称

sc config 服务名 start= demand //手动

sc condig 服务名 start= auto //自动

sc config 服务名 start= disabled //禁用

当你使用sc create命令创建一个服务时,如果没有指定type参数,它通常会默认为Own Process类型。这是最安全的配置,因为它隔离了服务,减少了潜在的风险,如果一个服务崩溃,不会影响到其他服务

 

2、启动服务

net start demo服务1

3、停止服务

net stop demo服务1

4、删除服务

sc delete "demo服务"

2、InstallUtil 操作 Windows Service

要安装windows service 首先要找到 InstallUtil.exe,InstallUtil.exe位置在 C:\Windows\Microsoft.NET\Framework\v4.0.30319,用什么版本写的服务,找到对应版本,各个版本不同详见(C:\Windows\Microsoft.NET\Framework\版本)

然后使用命令

安装服务

InstallUtil.exe 服务路径/windowsServiceName.exe

快捷安装:直接找到对应的 installutil.exe,拖cmd中,再拖你服务的位置路径下的服务名称.exe,(win10 以上系统支持)

启动该服务

net start windowsServiceName

停止服务

net stop windowsServiceName

卸载服务

InstallUtil.exe /u 服务路径/windowsServiceName.exe

3、NSSM install Windows Service

nssm install MyService "C:\path\to\your.exe"

NSSM和WinSW把exe注册成服务,

sc可以操作服务 sc query xx服务名字

NSSM下载地址:http://www.nssm.cc/ http://www.nssm.cc/release/nssm-2.24.zip

WinSW下载地址:https://github.com/winsw/winsw/releases

4、WinSW把exe注册成服务

NSSM和WinSW把exe注册成服务,sc可以操作服务 sc query xx服务名字

NSSM下载地址:http://www.nssm.cc/http://www.nssm.cc/release/nssm-2.24.zip

WinSW下载地址:https://github.com/winsw/winsw/releases

1、根据需要注册的系统,64系统使用WinSW-x64.exe或者32系统使用WinSW-x86.exe

2、复制WinSW-x64.exe或者WinSW-x86.exe到需要注册的目录,把名称修改为需要注册的名字(名字随意)

3、例如:nginx.exe 需要注册到服务中(64系统使用WinSW-x64.exe)

1、修改WinSW-x64.exe为nginxService.exe

2、修改sample-minimal.xml为nginxService.xml(nginxService.exe与nginxService.xml必须同一目录)

3、nginxService.xml中内容如下

4、在nginxService.exe当前目录中进入cmd ,执行nginxService.exe install,服务中就有了

nginxService.xml中内容如下

<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>nginxService123</id>
<!-- Display name of the service -->
<name>nginxService</name>
<!-- Service description -->
<description>nginx服务的描述</description>
<executable>nginx.exe</executable>
</service>

 

java注册666.jar注册到服务,有参数

例如:jar需要注册到服务中(64系统使用WinSW-x64.exe)

1、修改WinSW-x64.exe为javaService.exe

2、修改sample-minimal.xml为javaService.xml(java.exe与javaService.xml必须同一目录)

3、javaService.xml中内容如下

4、在javaService.exe当前目录中进入cmd ,执行javaService.exe install,服务中就有了

javaService.xml中内容如下

<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>javaService123</id>
<!-- Display name of the service -->
<name>javaService</name>
<!-- Service description -->
<description>jar服务的描述</description>
<executable>java</executable>
<arguments> -jar 666.jar</arguments>
</service>

 

 参考链接:https://blog.csdn.net/qq_74271133/article/details/139483670

posted @ 2025-06-13 15:20  龙骑科技  阅读(266)  评论(0)    收藏  举报