仓库地址: https://github.com/kohsuke/winsw
下载地址: https://github.com/kohsuke/wi...
安装说明
https://github.com/kohsuke/wi...
- 下载
winsw
程序,选择对应版本。(WinSW.NET2.exe 或 WinSW.NET4.exe) - 安装
.NET Framework
。 - 将
WinSW.exe
复制到自定义的目录,并重命名为test.exe
- 同目录下创建
test.xml
。特别注意,xml和exe必须同名 - 使用
test.exe install
安装服务。 - 使用
test.exe uninstall
卸载服务。
示例,配置springboot的服务
<configuration> <id>lance</id> <name>lance(winsw)</name> <description>java</description> <executable>java</executable> <arguments>-server -Xms400m -Xmx400m -Xmn100m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xverify:none -XX:+DisableExplicitGC -Djava.awt.headless=true -jar "lance-1.2.0.jar"</arguments> <log mode="reset"/></configuration>
命令说明
E:\service>test.exe /?A wrapper binary that can be used to host executables as Windows servicesUsage: winsw [/redirect file] <command> [<args>] Missing arguments trigger the service modeAvailable commands:- 'install' - install the service to Windows Service Controller- 'uninstall' - uninstall the service- 'start' - start the service (must be installed before)- 'stop' - stop the service- 'restart' - restart the service- 'restart!' - self-restart (can be called from child processes)- 'status' - check the current status of the service- 'test' - check if the service can be started and then stopped- 'testwait' - starts the service and waits until a key is pressed then stops the service- 'version' - print the version info- 'help' - print the help info (aliases: -h,--help,-?,/?)Extra options:- '/redirect' - redirect the wrapper's STDOUT and STDERR to the specified fileWinSW 2.2.0.0More info: https://github.com/kohsuke/winswBug tracker: https://github.com/kohsuke/winsw/issues
配置说明
https://github.com/kohsuke/wi...
<!-- 这是Windows服务包装器的一个配置示例。 此配置文件应放在WinSW.exe同目录,名称应相同。 例如,对于myapp.exe,配置文件名应为myapp.xml。 更多信息:https://github.com/kohsuke/winsw/blob/master/doc/xmlconfigfile.md--><configuration> <!-- SECTION: 必填项--> <!-- 服务ID,在windows系统中必须唯一 --> <id>myapp</id> <!-- 服务显示名称,只能英文和数字 --> <name>MyApp Service (powered by WinSW)</name> <!-- 服务描述,只能英文和数字 --> <description>This service is a service cratead from a sample configuration</description> <!-- 启动可执行文件的路径 --> <executable>%BASE%\myExecutable.exe</executable><!--SECTION: 安装这些选项仅在安装时有效。修改配置后,必须重新安装服务才能生效。--> <!-- OPTION: serviceaccount 定义运行服务的账号。 --> <!-- <serviceaccount> <domain>YOURDOMAIN</domain> <user>useraccount</user> <password>Pa55w0rd</password> <allowservicelogon>true</allowservicelogon> </serviceaccount> --> <!-- OPTION: onfailure 定义一系列操作,如果托管的可执行文件失败,则顺序执行这些操作。 支持的操作:restart,reboot,none --> <!-- <onfailure action="restart" delay="10 sec"/> <onfailure action="restart" delay="20 sec"/> <onfailure action="reboot" /> --> <!-- OPTION: resetfailure windows服务重置故障状态的时间。 默认值 1 day --> <!-- <resetfailure>1 hour</resetfailure> --><!--SECTION: 执行管理配置--> <!-- OPTION: arguments 传递给可执行文件的参数。 --> <!-- <arguments>-classpath c:\cygwin\home\kohsuke\ws\hello-world\out\production\hello-world test.Main</arguments> --> <!-- OPTION: startarguments 可执行文件启动参数。 如果配置,则覆盖"arguments" --> <!-- <startarguments></startarguments> --> <!-- OPTION: workingdirectory 如果指定,则设置可执行文件的默认工作目录。 默认值:服务包装的可执行文件的目录 --> <!-- <workingdirectory>C:\myApp\work</workingdirectory>--> <!-- OPTION: priority 所需的进程优先级。 可设置的值:Normal, Idle, High, RealTime, BelowNormal, AboveNormal 默认值:Normal --> <priority>Normal</priority> <!-- OPTION: stoptimeout 强制终止可执行文件之前,尝试正常关闭的等待时间。 默认值:15 seconds --> <stoptimeout>15 sec</stoptimeout> <!-- OPTION: stopparentprocessfirst 如果设置为true,在停止子进程之前终止父进程。 默认值:false --> <stopparentprocessfirst>false</stopparentprocessfirst> <!-- OPTION: stopexecutable 执行关闭服务的可执行文件路径。 只有配置了"stoparguments"时,才使用此配置。如果没有则用"executable"关闭服务 --> <!-- <stopexecutable>%BASE%\stop.exe</stopexecutable> --> <!-- OPTION: stoparguments 终止服务时,传递给"stopexecutable"的附加参数 此选项还允许通过停止可执行文件终止可执行文件 --> <!-- <stoparguments>-stop true</stoparguments> --><!-- SECTION: 服务管理--> <!-- OPTION: startmode 定义服务的启动模式 支持的模式:Automatic, Manual, Boot, System (latter ones are supported for driver services only) 默认模式:Automatic --> <startmode>Automatic</startmode> <!-- OPTION: delayedAutoStart 如果在"startmode"配置了"Automatic",则启用延迟自动启动。 请参阅Winsw文档以获取有关支持的平台版本和限制的信息。 --> <!-- <delayedAutoStart/> --> <!-- OPTION: depend 在本服务启动之前启动的服务。 --> <!-- <depend>Eventlog</depend> <depend>W32Time</depend> --> <!-- OPTION: waithint 挂起停止操作所需的时间 在指定的时间段过去之前,服务应该下次调用setServiceStatus函数。 否则服务将被标记为无响应 默认值:15 seconds --> <waithint>15 sec</waithint> <!-- OPTION: sleeptime 服务下次调用setServiceStatus函数之前的时间。 不要超过"waithint"。建议间隔是"waithint"的十分之一,但不小于1秒,也不大于10秒。 默认值:1 second --> <sleeptime>1 sec</sleeptime> <!-- OPTION: interactive 指示服务可以与桌面交互。 --> <!-- <interactive/> --> <!-- SECTION: 日志记录 --> <!-- OPTION: logpath 为服务包装程序生成的所有日志设置自定义日志目录。 默认值:包含执行器的目录 --> <!-- <logpath>%BASE%\logs</logpath> --> <!-- OPTION: log 为可执行文件生成的日志定义日志记录模式。 支持的模式: * append - 追加到现有日志 * none - 不保存日志 * reset - 启动时擦除日志 * roll - 根据大小自动循环分割日志 * roll-by-time - 根据日期循环分割日志 默认模式: append 每种模式都有不同的设置。https://github.com/kohsuke/winsw/blob/master/doc/loggingAndErrorReporting.md --> <log mode="append"> <!-- <setting1/> <setting2/> --> </log> <!--SECTION: 环境设置--> <!-- OPTION: env 设置或重写环境变量。 顶层可能配置了多个条目。 --> <!-- <env name="MY_TOOL_HOME" value="C:\etc\tools\myTool" /> <env name="LM_LICENSE_FILE" value="host1;host2" /> --> <!-- OPTION: download 启动前由包装器执行的下载列表。 --> <!-- <download from="http://www.google.com/" to="%BASE%\index.html" /> 下载并在出现错误时使服务启动失败: <download from="http://www.nosuchhostexists.com/" to="%BASE%\dummy.html" failOnError="true"/> 由于连接未加密而导致基本身份验证不安全的示例: <download from="http://example.com/some.dat" to="%BASE%\some.dat" auth="basic" unsecureAuth=“true” username="aUser" password=“aPassw0rd" /> 通过HTTPS安全基本身份验证 <download from="https://example.com/some.dat" to="%BASE%\some.dat" auth="basic" username="aUser" password="aPassw0rd" /> 当目标服务器和客户端是同一域的成员,并且客户端域属于具有信任的域时,安全身份验证: <download from="https://example.com/some.dat" to="%BASE%\some.dat" auth="sspi" /> --><!-- SECTION: 其他选项--> <!-- OPTION: beeponshutdown 指示服务在关闭时(如果操作系统支持)应发出嘟嘟声。 --> <!-- <beeponshutdown/> --> <!--SECTION: 扩展此配置部分允许指定自定义扩展。更多信息请访问:https://github.com/kohsuke/winsw/blob/master/doc/extensions/extensions.md--><!--<extensions> Extension 1: id values must be unique <extension enabled="true" id="extension1" className="winsw.Plugins.SharedDirectoryMapper.SharedDirectoryMapper"> <mapping> <map enabled="false" label="N:" uncpath="\\UNC"/> <map enabled="false" label="M:" uncpath="\\UNC2"/> </mapping> </extension> ...</extensions>--></configuration>