把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

浙公網安備 33010602011771號