在windows下通过winsw将程序注册为服务实现开机自启

前言

  1. 查看电脑 net framework 版本
    在命令行工具中运行:dir /b %windir%\Microsoft.NET\Framework\v* 可查看列表,最新的即为你电脑安装的版本。
    示例:
    在这里插入图片描述
    可见我的是 4.0。
    如果想要查看更细节的版本,操作过程如下:
    在这里插入图片描述
  2. 电脑联网并能够无障碍访问 GitHub并下载相关文件。

下载 winsw

WinSW下载地址:https://github.com/winsw/winsw/releases
根据我电脑 .NET 版本下载相关文件:

  • WinSW.NET4.exe
  • sample-allOptions.xml(或者 sample-minimal.xml)

编写配置文件

我们需要编写一个和程序(WinSW.NET4.exe)同名的XML文件(WinSW.NET4.xml)作为winsw的配置文件。
下面简要的设置一个可以满足日常需求的带配置文件的程序的配置。
如:

  • 程序位于:D:\program\my.exe
  • 可执行程序对应的配置文件为:D:\program\my.conf
    原始方法我在命令行工具中执行程序的方法为:D:\program\my.exe -c D:\program\my.conf
    接下来就是程序示例:
<service>
  
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>myServiceId</id>
  <!-- Display name of the service -->
  <name>My Service (powered by WinSW)</name>
  <!-- Service description -->
  <description>This service is a service created from a minimal configuration, for  remote control. </description>
  
  <!-- Path to the executable, which should be started -->
  <executable>D:\program\my.exe</executable>
  <arguments>-c D:\program\my.conf</arguments>
  <!--
    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="60 sec"/>
  <onfailure action="restart" delay="120 sec"/>


  <!--
    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="reset">
    <!--
    <setting1/>
    <setting2/>
    -->
  </log>
    <!--
      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>

</service>

注册服务

编写好配置文件之后,记得把配置文件和可执行文件放在一起,这样winsw才能正确识别配置文件。然后我们打开一个管理员权限的命令提示符或Powershell窗口,然后输入WinSW.NET4.exe install
在这里插入图片描述
对应的服务为:
在这里插入图片描述

卸载服务

同理,WinSW.NET4.exe uninstall
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或许,这就是梦想吧!

如果对你有用,欢迎打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值