下载WinSW
项目地址:
GitHub - winsw/winsw: A wrapper executable that can run any executable as a Windows service, in a permissive license.
下载地址:
https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-x64.exe
参考配置模板
<service>
<id>jenkins</id>
<name>Jenkins</name>
<description>This service runs Jenkins continuous integration system.</description>
<env name="JENKINS_HOME" value="%BASE%" />
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>
<log mode="roll" />
<onfailure action="restart" />
</service>
创建服务目录
结构如下:
PS C:\ConsoleApp1Service> ls
目录: C:\ConsoleApp1Service
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/10/12 15:52 4608 ConsoleApp1.exe
-a---- 2024/10/14 10:12 18243033 ConsoleApp1Service.exe
-a---- 2024/10/14 10:19 312 ConsoleApp1Service.xml
-a---- 2024/10/14 10:25 46 CreateConsoleApp1Service.ps1
ConsoleApp1.exe
控制台程序
ConsoleApp1Service.xml
<service>
<id>ConsoleApp1Service</id>
<name>ConsoleApp1Service</name>
<description>This service runs ConsoleApp1Service continuous integration system.</description>
<executable>C:\ConsoleApp1Service\ConsoleApp1.exe</executable>
<log mode="roll" />
<onfailure action="restart" />
</service>
CreateConsoleApp1Service.ps1
.\ConsoleApp1Service.exe install ConsoleApp1Service.xml
Start-Service ConsoleApp1Service
ConsoleApp1Service.exe
下载的WinSW-x64.exe修改文件名为ConsoleApp1Service.exe,保持文件名与.xml配置文件对应!
创建并启动ConsoleApp1Service服务
管理员启动powershell,执行CreateConsoleApp1Service.ps1
服务日志
WinSW日志:ConsoleApp1Service.wrapper.log
2024-10-14 10:40:11,915 DEBUG - Starting WinSW in console mode
2024-10-14 10:40:12,004 INFO - Installing service 'ConsoleApp1Service (ConsoleApp1Service)'...
2024-10-14 10:40:12,060 INFO - Service 'ConsoleApp1Service (ConsoleApp1Service)' was installed successfully.
2024-10-14 10:40:13,335 DEBUG - Starting WinSW in service mode
2024-10-14 10:40:13,720 INFO - Starting C:\ConsoleApp1Service\ConsoleApp1.exe
2024-10-14 10:40:13,787 INFO - Started process 4072
2024-10-14 10:40:13,822 DEBUG - Forwarding logs of the process System.Diagnostics.Process (ConsoleApp1) to WinSW.RollingLogAppender
原程序标准输出日志:ConsoleApp1Service.out.log
该程序每秒输出一次时间到终端标准输出
2024-10-14 10:40:13
2024-10-14 10:40:14
2024-10-14 10:40:15
2024-10-14 10:40:16
2024-10-14 10:40:17
2024-10-14 10:40:18
2024-10-14 10:40:19
2024-10-14 10:40:20
2024-10-14 10:40:21
原程序错误输出日志:ConsoleApp1Service.err.log
由于没有运行错误,程序也没有向标准错误输出,因此是空文件。
PS C:\ConsoleApp1Service> cat .\ConsoleApp1Service.err.log
PS C:\ConsoleApp1Service>
服务状态
可以看到服务程序是SYSTEM用户启动,工作在会话ID=0中