# winsw
# Installation
# Installation steps
In order to setup WinSW, you commonly need to perform the following steps:
- Take winsw.exe from the distribution, and rename it to your taste (
such as myapp.exe) - Write myapp.xml (see XML Config File specification for more details)
- Place those two files side by side, because that's how WinSW discovers its configuration.
- Run myapp.exe install
<OPTIONS>in order to install the service wrapper. - Optional - Perform additional configuration in the Windows Service Manager.
- Optional - Perform extra configurations if required (guidelines are available below).
- Declare that the executable is compatible with
.NET 4or above (for WinSW 1.x only) - Enable the WinSW offline mode
- Declare that the executable is compatible with
- Run the service from the Windows Service Manager.
# Service registration
You can then install the service like:
myapp.exe install <OPTIONS>
... and you can use the exit code from these processes to determine whether the operation was successful. Possible return error codes are described here. Beyond these error codes, all the non-zero exit code should be assumed as a failure.
The Installer can be also started with the /p option. In such case it will prompt for an account name and password, which should be used as a service account.
# xml文件
示例配置文件
<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>
<logmode>rotate</logmode>
</service>
2
3
4
5
6
7
8
9
# id
Specifies the ID that Windows uses internally to identify the service. This has to be unique among all the services installed in a system, and it should consist entirely out of alpha-numeric characters.
# name
Short display name of the service, which can contain spaces and other characters. This shouldn't be too long, like <id>, and this also needs to be unique among all the services in a given system.
# executable
This element specifies the executable to be launched. It can be either absolute path, or you can just specify the executable name and let it be searched from PATH (although note that the services often run in a different user account and therefore it might have different PATH than your shell does.)
# startmode
This element specifies the start mode of the Windows service. It can be one of the following values: Boot, System, Automatic, or Manual. See MSDN for details. The default value is Automatic.
# delayedAutoStart
This Boolean option enables the delayed start mode if the Automatic start mode is defined. More information about this mode is provided here.
Please note that this startup mode will not take affect on old Windows versions older than Windows 7 and Windows Server 2008. Windows service installation may fail in such case.
<delayedAutoStart/>
# depend
Specify IDs of other services that this service depends on. When service X depends on service Y, X can only run if Y is running.
Multiple elements can be used to specify multiple dependencies.
<depend>Eventlog</depend>
<depend>W32Time</depend>
2
# logging
Optionally set a different logging directory with <logpath> and startup <logmode>: reset (clear log), roll (move to *.old) or append (default).
See the Logging and Error reporting page for more info.
# argument
This element specifies the arguments to be passed to the executable. Winsw will quote each argument if necessary, so do not put quotes in <argument> to avoid double quotation.
<argument>arg1</argument>
<argument>arg2</argument>
<argument>arg3</argument>
2
3
For backward compatibility, <arguments> element can be used instead to specify the whole command line in a single element.
# stopargument/stopexecutable
When the service is requested to stop, winsw simply calls TerminateProcess function API to kill the service instantly. However, if <stopargument> elements are present, winsw will instead launch another process of <executable> (or <stopexecutable> if that's specified) with the <stopargument> arguments, and expects that to initiate the graceful shutdown of the service process.
Winsw will then wait for the two processes to exit on its own, before reporting back to Windows that the service has terminated.
When you use the <stopargument>, you must use <startargument> instead of <argument>. See the complete example below:
<executable>catalina.sh</executable>
<startargument>jpda</startargument>
<startargument>run</startargument>
<stopexecutable>catalina.sh</stopexecutable>
<stopargument>stop</stopargument>
2
3
4
5
6
Note that the name of the element is startargument and not startarguments. As such, to specify multiple arguments, you'll specify multiple elements.
# stoptimeout
When the service is requested to stop, winsw first attempts to GenerateConsoleCtrlEvent function (similar to Ctrl+C), then wait for up to 15 seconds for the process to exit by itself gracefully. A process failing to do that (or if the process does not have a console), then winsw resorts to calling TerminateProcess function API to kill the service instantly.
This optional element allows you to change this "15 seconds" value, so that you can control how long winsw gives the service to shut itself down. See <onfailure> below for how to specify time duration:
<stoptimeout>10sec</stoptimeout>
# env
This optional element can be specified multiple times if necessary to specify environment variables to be set for the child process. The syntax is:
<env name="HOME" value="c:\abc" />
# interactive
If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes. If your program requires GUI, set this like the following:
<interactive />
Note that since the introduction UAC (Windows Vista and onward), services are no longer really allowed to interact with the desktop. In those OSes, all that this does is to allow the user to switch to a separate window station to interact with the service.
# beeponshutdown
This optional element is to emit simple tone when the service shuts down. This feature should be used only for debugging, as some operating systems and hardware do not support this functionality.
# download
This optional element can be specified multiple times to have the service wrapper retrieve resources from URL and place it locally as a file. This operation runs when the service is started, before the application specified by <executable> is launched.
For servers requiring authentication some parameters must be specified depending on the type of authentication. Only the basic authentication requires additional sub-parameters. Supported authentication types are:
- none: default, must not be specified
- sspi: Microsoft authentication including Kerberos, NTLM etc.
- basic: Basic authentication, sub-parameters:
- user="UserName"
- password="Passw0rd"
- unsecureAuth="true": default="false"
The parameter “unsecureAuth” is only effective when the transfer protocol is HTTP - unencrypted data transfer. This is a security vulnerability because the credentials are send in clear text! For a SSPI authentication this is not relevant because the authentication tokens are encrypted.
For target servers using the HTTPS transfer protocol it is necessary, that the CA which issued the server certificate is trusted by the client. This is normally the situation when the server ist located in the Internet. When an organisation is using a self issued CA for the intranet this probably is not the case. In this case it is necessary to import the CA to the Certificate MMC of the Windows client. Have a look to the instructions on this site. The self issued CA must be imported to the Trusted Root Certification Authorities for the computer.
By default, the download command does not fail the service startup if the operation fails (e.g. from is not available). In order to force the download failure in such case, it is possible to specify the failOnError boolean attribute.
Examples:
<download from="http://example.com/some.dat" to="%BASE%\some.dat" />
<download from="http://example.com/some.dat" to="%BASE%\some.dat" failOnError="true"/>
<download from="https://example.com/some.dat" to="%BASE%\some.dat" auth="sspi" />
<download from="https://example.com/some.dat" to="%BASE%\some.dat" failOnError="true"
auth="basic" user="aUser" password="aPassw0rd" />
<download from="http://example.com/some.dat" to="%BASE%\some.dat"
auth="basic" unsecureAuth="true"
user="aUser" password="aPassw0rd" />
2
3
4
5
6
7
8
9
10
11
12
This is another useful building block for developing a self-updating service.
# log
See the "Logging" section above for more details.
# onfailure
This optional repeatable element controls the behaviour when the process launched by winsw fails (i.e., exits with non-zero exit code).
<onfailure action="restart" delay="10 sec"/>
<onfailure action="restart" delay="20 sec"/>
<onfailure action="reboot" />
2
3
For example, the above configuration causes the service to restart in 10 seconds after the first failure, restart in 20 seconds after the second failure, then Windows will reboot if the service fails one more time.
Each element contains a mandatory action attribute, which controls what Windows SCM will do, and optional delay attribute, which controls the delay until the action is taken. The legal values for action are:
- restart: restart the service
- reboot: reboot Windows
- none: do nothing and leave the service stopped
The possible suffix for the delay attribute is sec/secs/min/mins/hour/hours/day/days. If missing, the delay attribute defaults to 0.
If the service keeps failing and it goes beyond the number of <onfailure> configured, the last action will be repeated. Therefore, if you just want to always restart the service automatically, simply specify one <onfailure> element like this:
<onfailure action="restart" />
# resetfailure
This optional element controls the timing in which Windows SCM resets the failure count. For example, if you specify <resetfailure>1 hour</resetfailure> and your service continues to run longer than one hour, then the failure count is reset to zero. This affects the behaviour of the failure actions (see <onfailure> above).
In other words, this is the duration in which you consider the service has been running successfully. Defaults to 1 day.
# Service account
It is possible to specify the useraccount (and password) that the service will run as. To do this, specify a <serviceaccount> element like this:
<serviceaccount>
<domain>YOURDOMAIN</domain>
<user>useraccount</user>
<password>Pa55w0rd</password>
<allowservicelogon>true</allowservicelogon>
</serviceaccount>
2
3
4
5
6
The <allowservicelogon> is optional. If set to true, will automatically set the "Allow Log On As A Service" right to the listed account.
To use (Group) Managed Service Accounts append $ to the account name and remove <password> element:
<serviceaccount>
<domain>YOURDOMAIN</domain>
<user>gmsa_account$</user>
<allowservicelogon>true</allowservicelogon>
</serviceaccount>
2
3
4
5
# Working directory
Some services need to run with a working directory specified. To do this, specify a <workingdirectory> element like this:
<workingdirectory>C:\application</workingdirectory>
# priority
Optionally specify the scheduling priority of the service process (equivalent of Unix nice) Possible values are idle, belownormal, normal, abovenormal, high, realtime (case insensitive.)
<priority>idle</priority>
Specifying a priority higher than normal has unintended consequences. See the MSDN article ProcessPriorityClass Enumeration for details. This feature is intended primarily to launch a process in a lower priority so as not to interfere with the computer's interactive usage.
# stopparentprocessfirst
Optionally specify the order of service shutdown. If true, the parent process is shutdown first. This is useful when the main process is a console, which can respond to Ctrl+C command and will gracefully shutdown child processes.
<stopparentprocessfirst>true</stopparentprocessfirst>
# 可执行jar文件
在Linux设备上后台启动SpringBoot项目只需要一条命令:nohup sh run.sh &,run.sh脚本包含了启动SpringBoot项目的命令。而windows上则比较尴尬,终端会开着!
<service>
<id>sentinelDashboard</id>
<name>sentinelDashboard</name>
<description>This is sentinelDashboard service.</description>
<!-- java环境变量 -->
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<arguments>-jar "D:\springCloud\sentinel\sentinelDashboard.jar"</arguments>
<!-- 当前同一文件夹,可以直接如下: -->
<!-- <arguments>-jar sentinelDashboard.jar</arguments> -->
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>%BASE%\log</logpath>
<logmode>rotate</logmode>
</service>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 可执行cmd文件
<service>
<id>nacos</id>
<name>nacos</name>
<description>This is nacos 1.1.0 service.</description>
<executable>startup.cmd</executable>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>%BASE%\log</logpath>
<logmode>rotate</logmode>
</service>
2
3
4
5
6
7
8
9
10
11
# 安装服务
nacos install
# 启动服务
可以直接进入windows的服务管理窗口进行服务的启动、停止、运行模式
也可以通过命令行启动、停止
net start nacos
net stop nacos
2
# 删除服务
sc delete ServiceName
# 查找服务的PID
sc queryex serviceName