• Winsw将jar包部署为windows服务


    下载winsw

    https://github.com/winsw/winsw/releases

    下载winsw官网上的xml文件和.exe文件

    创建文件编辑xml

    创建一个文件夹,将所需要转换的jar包,和这两个文件放在一起,并且将.exe的文件名修改和.xml文件一样

     xml的可选项

    <!--
     这是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:cygwinhomekohsukewshello-worldoutproductionhello-world test.Main</arguments>
      -->
     
      <!-- 
        OPTION: startarguments
        可执行文件启动参数。
        如果配置,则覆盖"arguments"
      -->
      <!--
      <startarguments></startarguments>
      -->
      
      <!--
        OPTION: workingdirectory
        如果指定,则设置可执行文件的默认工作目录。
        默认值:服务包装的可执行文件的目录
      -->
      <!--
      <workingdirectory>C:myAppwork</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	oolsmyTool" />
      <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>

    示例xml

    <service>
    <id>test</id>
    <name>test</name>
    <description>test</description>
    <executable>E:jdkinjava.exe</executable>
    <arguments>-jar testjar.jar</arguments>
    <startmode>Automatic</startmode>
    <logpath>%BASE%/log</logpath>
    <logmode>rotate</logmode>
    </service>

    运行exe

    删除服务

    以管理员命令运行

    sc  delete  test
  • 相关阅读:
    数据类型
    32个关键字
    标识符
    Xcode常用快捷键
    Linux中级之keepalived配置
    linux中级之keepalived概念
    Linux中级之lvs三个模式的图像补充(nat,dr,tun)
    linux中级之防火墙的数据传输过程
    Linux中级之netfilter/iptables应用及补充
    linux中级之lvs配置(命令)
  • 原文地址:https://www.cnblogs.com/cyh1282656849/p/14594138.html
Copyright © 2020-2023  润新知