[Java] 將 java 程式轉為 Windows 系統服務 (Windows Service Wrapper)
作者 GitHub
https://github.com/kohsuke/winsw
下載 winsw (Windows Service Wrapper)
http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/
有分為 .NET2 及 .NET4 執行環境的版本
EX: 以 2.2.0 版本為例
winsw-2.2.0-bin.exe (.NET2)
假設要轉為服務的檔案為 test.jar
執行的環境已經安裝好 Java Runtime,且以設定好 Path 環境變數
下載下來的 winsw 可以變更為自己習慣的檔名 如 test.exe
對應配置檔檔名應設定為 test.xml
配置檔內容
<service>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>test</id>
<!-- Display name of the service -->
<name>test</name>
<!-- Service description -->
<description>This runs Spring Boot as a Service.</description>
<!-- Path to the executable, which should be started -->
<executable>java</executable>
<arguments>-Xmx256m -jar "test.jar"</arguments>
<!-- log mode -->
<logmode>rotate</logmode>
</service>
*.war 也可以
<service>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>test</id>
<!-- Display name of the service -->
<name>test</name>
<!-- Service description -->
<description>This runs Spring Boot as a Service.</description>
<!-- Path to the executable, which should be started -->
<executable>java</executable>
<arguments>--Xrs -Xmx256m -jar "test.war" -httpPort=8080</arguments>
<!-- log mode -->
<logmode>rotate</logmode>
</service>
詳細配置檔配置可參照
WinSW XML Configuration File
https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
接下來可以以系統管理員身分執行 command window
註冊服務:
test install
2019-08-16 11:23:57,842 INFO - Installing the service with id 'test'
啟動服務:
test start
2019-08-16 11:53:55,747 INFO - Starting the service with id 'test'
停止服務:
test stop
2019-08-16 11:54:12,234 INFO - Stopping the service with id 'test'
卸載服務:
test uninstall
2019-08-16 13:14:04,268 INFO - Uninstalling the service with id 'test'
若無使用系統管理員權限 執行 command
則會發生 AccessDenied
2019-08-16 13:14:04,268 INFO - Stopping the service with id ''test''
2019-08-16 13:14:04,268 FATAL - WMI Operation failure: AccessDenied
WMI.WmiException: AccessDenied
在 WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
在 WMI.WmiRoot.InstanceHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
在 winsw.WrapperService.Main(String[] args)
WMI.WmiException: AccessDenied
在 WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
在 WMI.WmiRoot.InstanceHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
在 winsw.WrapperService.Main(String[] args)
Ref:
https://www.baeldung.com/spring-boot-app-as-a-service
http://edn.embarcadero.com/article/32068
https://www.cnblogs.com/gexiaoshan/p/10109882.html
https://www.lovesofttech.com/windows/winInstallNginxAsService/
沒有留言 :
張貼留言