Windows Sever 通过winSW注册服务后,服务被系统强制中断的服务属性设置问题

Posted 幽反丶叛冥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows Sever 通过winSW注册服务后,服务被系统强制中断的服务属性设置问题相关的知识,希望对你有一定的参考价值。

摘要:在一个实际项目中,最近突然后台应用一直被强行关闭,导致不能正常的访问。应用通过winSW将java编写的后台jar(spring框架)程序注册为一个服务。后续临时的解决办法为将此服务设置为在服务中断后,自行重启,使其能接着使用

上面的问题通过windows的事件日志,查找到对应错误信息。同时也是通过直接在对应服务上,点击鼠标右键属性设置。服务采用的winSW进行注册服务,这里也对winSW的使用做一个简单的记录(注:采用 winSW 注册为服务后,可以不用弹出Java运行的黑框体,大大讲减少了被 “不小心” 关闭的问题)。


一、问题现状


将 .jar应用程序注册为服务后,当服务挂掉,也意味着 .jar后台应用程序挂掉,在不会自行重启。

二、解决办法

2.1 设置服务中断恢复


01、恢复属性设置

设置服务在中断后,自行尝试重启动。


02、设置后的效果

通过以上设置后,在服务终端后,系统会自行去尝试重启此服务。


三、winSW使用及配置说明

前面提到是通过winSW进行将应用注册为服务,在winSW的配置文件分为两种,一种是最小配置;另一种是全配置文件。最小配置文件仅将应用程序注册问服务。全配置中则可以配置上方服务器终端后的恢复模式,优先级等等。对于这些参数个人并未一一尝试,这里仅为对 winSW作者给到的说明进行机翻,以便自己掌握。

winSW链接地址:https://github.com/winsw/winsw/releases

3.1 winSW使用

winSW配置文件内容

<configuration>
  
    <!--安装成Windows服务后的服务名-->
    <id>SjjdServiceID</id>
    <!--显示的服务名称-->
    <name>SjjdService</name>
    <!--对服务的描述-->
    <description>思杰软件</description>
    <!--这里写java的路径,如何配置了环境变量直接写"java"就行-->
    <executable>java</executable>
    <!--Xmx256m 代表堆内存最大值为256MB -jar后面的是项目名-->
    <arguments>-Xmx1024m -jar SjjdService.jar</arguments>
    <!--日志模式-->
    <logmode>rotate</logmode>

</configuration>

SjjdServiceStart.bat 文件内容

SjjdService.exe install
net start SjjdService

SjjdServiceStop.bat 文件内容

net stop SjjdService
SjjdService.exe uninstall

通过以上截图,winSW 的应用主要用以上几步:

  1. winSW.exe名称需要需要和配置文件一致
  2. winSW.exe、winSW配置文件、应用程序三者在同一目录
  3. 通过命令完成服务的开启或停止

主要命令有一下几种,通过此链接:https://github.com/winsw/winsw/blob/v3/docs/cli-commands.md#install-command 可以查看官网给到说明。

  • winsw install 安装服务
  • winsw uninstall 卸载服务
  • winsw start 开启服务
  • winsw stop 停止服务
  • winsw restart 重新启动服务
  • winsw status 检查服务的当前状态

3.2 配置说明

这里根据配置文件节点进行一段一段的分拆翻译,以下为全配置版本配置文件


01、必须配置项
原文:

  <!-- ID of the service. It should be unique accross the Windows system-->
  <id>myapp</id>
  <!-- Display name of the service -->
  <name>MyApp Service (powered by WinSW)</name>
  <!-- Service description -->
  <description>This service is a service cratead from a sample configuration</description>
  
  <!-- Path to the executable, which should be started -->
  <executable>%BASE%\\myExecutable.exe</executable>

翻译:

配置必填项
标签含义
<id>服务ID,在windows系统中,必须唯一
<name>服务显示名称,只能是英文或者数字
<description>对服务的描述,可以用于记录此服务有什么用
<executable>启动可执行文件的路径,实际环境中,如果配置了Java环境变量,可以直接使用Java替代


02、安装相关
此相关配置进行修改后,需要重启服务才能生效

原文

<!--
SECTION: Installation
These options are being used during the installation only.
Their modification will not take affect without the service re-installation.
-->

  <!--
    OPTION: serviceaccount
    Defines account, under which the service should run.
  -->
  <!--
  <serviceaccount>
    <domain>YOURDOMAIN</domain>
    <user>useraccount</user>
    <password>Pa55w0rd</password>
    <allowservicelogon>true</allowservicelogon>
  </serviceaccount>
  -->
  
  <!--
    OPTION: onfailure
    Defines a sequence of actions, which should be performed if the managed executable fails.
    Supported actions: restart, reboot, none
  -->
  <!--
  <onfailure action="restart" delay="10 sec"/>
  <onfailure action="restart" delay="20 sec"/>
  <onfailure action="reboot" />
  -->
  
  <!--
    OPTION: resetfailure
    Time, after which the Windows service resets the failure status.
    Default value: 1 day
  -->
  <!--
  <resetfailure>1 hour</resetfailure>
  -->

翻译:

定义运行服务账户
标签含义
<domain>windows的域,在windows sever系统上,这是很大一个功能块
<user>用户名,可在控制面板 —> 用户管理里面查看
<password>用户密码
<allowservicelogon>允许以服务形式登录
服务启动失败或中断情况下的恢复机制
标签含义
< onfailure action="restart" delay="10 sec"/>第一失败后,执行什么操作。action 共计有3中模式
  • restart:重新启动服务
  • reboot:重新启动计算机
  • none:无操作
<onfailure action="restart" delay="20 sec"/>第二失败后,执行什么操作。action 共计有3中模式
  • restart:重新启动服务
  • reboot:重新启动计算机
  • none:无操作
<onfailure action="reboot" />以上两次都失败后,执行什么操作。action 共计有3中模式
  • restart:重新启动服务
  • reboot:重新启动计算机
  • none:无操作
<resetfailure>windows系统会有一个日志记录,并且计数。间隔多久后重置状态


03、执行管理配置

原文

<!--
SECTION: Executable management
-->

  <!-- 
    OPTION: arguments
    Arguments, which should be passed to the executable
  -->
  <!--
  <arguments>-classpath c:\\cygwin\\home\\kohsuke\\ws\\hello-world\\out\\production\\hello-world test.Main</arguments>
  -->

  <!-- 
    OPTION: startarguments
    Arguments, which should be passed to the executable when it starts
    If specified, overrides 'arguments'.
  -->
  <!--
  <startarguments></startarguments>
  -->
  
  <!--
    OPTION: workingdirectory
    If specified, sets the default working directory of the executable
    Default value: Directory of the service wrapper executable.
  -->
  <!--
  <workingdirectory>C:\\myApp\\work</workingdirectory>
-->
  
  <!--
    OPTION: priority
    Desired process priority.
    Possible values: Normal, Idle, High, RealTime, BelowNormal, AboveNormal
    Default value: Normal
  -->
  <priority>Normal</priority>
  
  <!-- 
    OPTION: stoptimeout
    Time to wait for the service to gracefully shutdown the executable before we forcibly kill it
    Default value: 15 seconds
  -->
  <stoptimeout>15 sec</stoptimeout>
    
  <!--
    OPTION: stopparentprocessfirst
    If set, WinSW will terminate the parent process before stopping the children.
    Default value: false
  -->
  <stopparentprocessfirst>false</stopparentprocessfirst>
  
  
  <!-- 
    OPTION: stopexecutable
    Path to an optional executable, which performs shutdown of the service.
    This executable will be used if and only if 'stoparguments' are specified.
    If 'stoparguments' are defined without this option, 'executable' will be used as a stop executable
  -->
  <!--
  <stopexecutable>%BASE%\\stop.exe</stopexecutable>
  -->

  <!-- 
    OPTION: stoparguments
    Additional arguments, which should be passed to the stop executable during termination.
    This OPTION also enables termination of the executable via stop executable
  -->
  <!--
  <stoparguments>-stop true</stoparguments>-->

翻译:

标签含义
<arguments> 传递给可执行文件的参数。
<startarguments>参数在可执行文件启动时传递给可执行文件,如果此处指定,则重写上方<arguments>参数。
<workingdirectory> 如果指定,则设置可执行文件的默认工作目录。默认值:存放winSW文件的目录
<priority>所需的进程优先级。值:Normal, Idle, High, RealTime, BelowNormal, AboveNormal 默认值:Normal,具体可查看表格
<stoptimeout>当服务被要求停止时,winsw首先尝试调用GenerateConsoleCtrlEvent 方法(类似于Ctrl+C),然后等待长达15秒的时间,让进程自行退出。如果这样做了,进程关闭还是失败了(或者如果进程没有控制台),然后winsw会调用终止进程的API函数来立即终止服务。这个可选元素允许您改变这个“15秒”的值,这样您就可以控制winsw等待服务进程自行关闭的时间。如何指定时间期限,可参考下面的“onfailure”元素的设置
<stopparentprocessfirst>可选地指定服务关闭的顺序。如果“true”,父进程首先关闭。当主进程是一个控制台时,这是很有用的,它可以响应Ctrl+C命令,并优雅地关闭子进程。
<stopexecutable>请求停止服务时,winsw通过调用终止进程的API函数来立即终结服务。然而,如果存在“stopargument”元素,winsw将通过使用”stopargument“作为参数,来启动“executable“元素(或者是”stopexecutable“元素)中配置的进程,来代替调用终止进程的API函数。期望通过这种方式来优雅的关闭服务进程。然后,Winsw将等待两个进程自行退出,然后向Windows报告该服务已经终止。
<stoparguments><stopexecutable>附加参数

进程(线程)的优先级越高,那么就可以分占越多的CPU时间片,每个进程都有相应的优先级,优先级决定它何时运行和占用CPU的时间。最终的优先级共分 32 级,是从 0 到 31 的数值,称为基本优先级别。

进程优先级
优先级标志优先级值
idle (低) IDLE_PRIORITY_CLASS4
Below (低于标准) BELOW_NORMAL_PRIORITY_CLASS
normal (标准) NORMAL_PRIORITY_CLASS7或9
Above (高于标准) ABOVE_NORMAL_PRIORITY_CLASS
high (高) HIGH_PRIORITY_CLASS13
realtime (实时) REALTIME_PRIORITY_CLASS24


04、服务管理

原文

<!-- 
SECTION: Service management 
-->
    <!--
      OPTION: startmode
      Defines start mode of the service.
      Supported modes: Automatic, Manual, Boot, System (latter ones are supported for driver services only)
      Default mode: Automatic
    -->
    <startmode>Automatic</startmode>
    
    <!--
      OPTION: delayedAutoStart
      Enables the Delayed Automatic Start if 'Automatic' is specified in the 'startmode' field.
      See the WinSW documentation to get info about supported platform versions and limitations.
    -->
    <!--<delayedAutoStart/>-->
    
    <!-- 
      OPTION: depend
      Optionally specifies services that must start before this service starts.
    -->
    <!--
    <depend>Eventlog</depend>
    <depend>W32Time</depend>
    -->
    
    <!--
      OPTION: waithint
      The estimated time required for a pending stop operation.
      Before the specified amount of time has elapsed, the service should make its next call to the SetServiceStatus function.
      Otherwise the service will be marked as non-responding
      Default value: 15 seconds
    -->
    <waithint>15 sec</waithint>
    
    <!--
      OPTION: sleeptime
      The time before the service should make its next call to the SetServiceStatus function.
      Do not wait longer than the wait hint. A good interval is one-tenth of the wait hint but not less than 1 second and not more than 10 seconds.
      Default value: 1 second
    -->
    <sleeptime>1 sec</sleeptime>
    
    <!--
      OPTION: interactive
      Indicates the service can interactwith the desktop.
    -->
    <!--
    <interactive/>
    -->

翻译:

服务管理
标签含义
<startmode>该元素指定Windows服务的启动模式。它可以是下列值之一:开机、系统、自动或手动。有关详细信息,请参阅MSDN【https://msdn.microsoft.com/en-us/library/aa384896%28v=vs.85%29.aspx】。默认值是“Automatic”。
<delayedAutoStart>这个布尔选项允许在定义“自动”启动模式时延时启动。关于延时启动模式,可参阅【https://blogs.technet.microsoft.com/askperf/2008/02/02/ws2008-startup-processes-and-delayed-automatic-start】。 请注意,延时启动模式在早于Windows 7和Windows Server 2008的操作系统中可能失效。在这种情况下,Windows服务安装可能会失败。
<depend>指定该服务所依赖的其他服务的id。当服务“X”依赖于服务“Y”时,“X”只能在“Y”运行后运行。可以使用多个元素来指定多个依赖项。
<waithint>
<sleeptime>多少秒后调用SetServiceStatus函数的时间,建议值 1 ~10 秒
<interactive>如果指定了这个可选的元素,那么该服务将被允许与桌面交互,比如显示一个新的窗口和对话框。如果你的程序需要GUI,那么设置如下:请注意,自从引入UAC(Windows Vista及之后的版本)以来,服务不再被允许与桌面交互。在这些操作系统中,所有这一切都是为了允许用户切换到一个单独的窗口来与服务交互。


05、服务进程的日志以及错误信息

原文

<!-- 
SECTION:Logging 
-->

  <!--
    OPTION: logpath
    Sets a custom logging directory for all logs being produced by the service wrapper
    Default value: Directory, which contains the executor
  -->
  <!--
    <logpath>%BASE%\\logs</logpath>
  -->
  
  <!--
    OPTION: log
    Defines logging mode for logs produced by the executable.
    Supported modes:
      * append - Rust update the existing log
      * none - Do not save executable logs to the disk
      * reset - Wipe the log files on startup
      * roll - Rotate logs based on size
      * roll-by-time - Rotate logs based on time
      * rotate - Rotate logs based on size, (8 logs, 10MB each). This mode is deprecated, use "roll"
    Default mode: append
    
    Each mode has different settings. 
    See https://github.com/kohsuke/winsw/blob/master/doc/loggingAndErrorReporting.md for more details
  -->
  <log mode="append">
    <!--
    <setting1/>
    <setting2/>
  -->
  </log>

翻译:

服务进程的日志以及错误信息
标签含义
<logpath>生成的所有日志设置自定义日志目录,默认为winSW目录
<log>应用程序日志模式,共有一下几种,具体详情可以查看【https://github.com/winsw/winsw/blob/master/doc/loggingAndErrorReporting.md】
  • append:追加-更新现有日志
  • none:不将日志保存到磁盘
  • reset:重置-启动时擦除日志文件
  • roll:滚动-根据日志的大小
  • roll-by-time:滚动-根据日子时间
  • rotate:滚动-根据日志大小(8个日志,每个10MB)。此模式已被弃用,请使用“滚动”。默认模式:追加


06、环境设置

原文

<!--
SECTION: Environment setup
-->
  <!--
    OPTION: env
    Sets or overrides environment variables.
    There may be multiple entries configured on the top level.
  -->
  <!--
  <env name="MY_TOOL_HOME" value="C:\\etc\\tools\\myTool" />
  <env name="LM_LICENSE_FILE" value="host1;host2" />
  -->


  <!--
    OPTION: download
    List of downloads to be performed by the wrapper before starting
  -->
  <!--
  <download from="http://www.google.com/" to="%BASE%\\index.html" />
  
  Download and fail the service startup on Error:
  <download from="http://www.nosuchhostexists.com/" to="%BASE%\\dummy.html" failOnError="true"/>

  An example for unsecure Basic authentication because the connection is not encrypted:
  <download from="http://example.com/some.dat" to="%BASE%\\some.dat"
            auth="basic" unsecureAuth=“true”
            username="aUser" password=“aPassw0rd" />

  Secure Basic authentication via HTTPS:
  <download from="https://example.com/some.dat" to="%BASE%\\some.dat"
            auth="basic" username="aUser" password="aPassw0rd" />

  Secure authentication when the target server and the client are members of the same domain or 
  the server domain and the client domain belong to the same forest with a trust:
  <download from="https://example.com/some.dat" to="%BASE%\\some.dat" auth="sspi" />
  -->

翻译:

标签含义
<env>可以多次指定的可选元素,为子进程设置环境变量。
<download>可以多次指定,让服务包装器从URL检索资源,并将其作为文件放置在本地。这个操作是在服务启动时运行的,在“executable”指定的应用程序启动之前。对于需要身份验证的服务器,必须根据认证的类型指定一些参数。只有基本身份验证需要额外的子参数。支持的身份验证类型是:
  • none:默认类型,不能指定
  • sspi:微软认证,包括Kerberos、NTLM等。
  • basic:基本身份认证,子参数包括:1)user=“UserName”,2)password=“Password”,3)unsecureAuth=“true”:default=“false”
  • 当传输协议是未加密的HTTP数据传输时,参数“unsecureAuth”才生效。这是一个安全漏洞,因为凭证是用明文发送的!对于SSPI认证,这是不相关的,因为身份验证令牌是加密的。

    对于使用HTTPS传输协议的目标服务器这是必须的,因为服务器发出的CA证书是客户端信任的。这通常是服务器在Internet上的情况。当一个组织在内网中使用自发布的CA时,情况可能不是这样。在这种情况下,有必要将CA导入到Windows客户端的证书MMC。可以看一下微软的说明(https://technet.microsoft.com/en-us/librar/cc754841.aspx)。自发布CA必须被导入到计算机的受信任的根证书颁发机构。

    默认情况下,如果操作失败(例如,无效的下载地址),“download”命令也不会在服务启动时失败。为了在这种情况下强制下载失败,可以指定“failOnError”布尔属性。


07、其他选项

原文

<!-- 
SECTION: Other options 
-->
  
  <!--
    OPTION: beeponshutdown
    Indicates the service should beep when finished on shutdown (if it's supported by OS).
  -->
  <!--
  <beeponshutdown/> 
  -->
  
<!--
SECTION: Extensions
This configuration section allows specifying custom extensions.
More info is available here: 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>
-->

翻译:

标签含义
<beeponshutdown>指示关闭时服务应发出蜂鸣音(如果操作系统支持)。
<extensions>自定义扩展,具体详情可以查看【https://github.com/kohsuke/winsw/blob/master/doc/extensions/extensions.md】

四、参考链接

winSW链接地址:https://github.com/winsw/winsw/releases
winsw说明
windows 域,windows domain
如何使用其他帐户将Apache ActiveMQ Artemis作为Windows服务运行
设置进程和线程的优先级

以上是关于Windows Sever 通过winSW注册服务后,服务被系统强制中断的服务属性设置问题的主要内容,如果未能解决你的问题,请参考以下文章

WinSW(Windows service wrapper)开机自启动nginx配置

使用WinSW把nginx做成windows服务

nginx+winsw windows服务

远程连接阿里云服务器Windows sever 2008和Windows sever 2012

把Jar包加入windows系统服务

windows sever 2008怎么配置服务器