美文网首页
Windows上使用WinSW让程序以服务的方式运行

Windows上使用WinSW让程序以服务的方式运行

作者: 超人不会Fly | 来源:发表于2021-10-11 10:54 被阅读0次

    WinSW介绍

    WinSW在是github上的一个开源软件,可以让你的程序在windows上以服务的方式运行,并开机自启动。

    使用WinSW

    1.下载软件.exe与配置文件.xml

    image

    2.配置xml文件

    首先需要配置一下需要被安装的程序。

    <!-- ID of the service. It should be unique accross the Windows system-->
    
      <id>frpc</id>
    
      <!-- Display name of the service -->
    
      <name>frpc(powered by WinSW)</name>
    
      <!-- Service description -->
    
      <description>frp_0.37.1_windows_amd64</description>
    
      <!-- Path to the executable, which should be started -->
    
      <executable>%BASE%\frp_0.37.1_windows_amd64\frpc.exe</executable>
    

    配置运行程序时需要带入的参数。

    <!--
    
        OPTION: arguments
    
        Arguments, which should be passed to the executable
    
      -->
    
      <arguments>-c %BASE%\frp_0.37.1_windows_amd64\frpc.ini</arguments>
    

    配置日志输出的路径。

    <!--
    
        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>
    

    配置日志保存策略, 下面总共有5种模式,我这边使用了reset,每次启动时清除日志。

    <!--
    
        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 - Roll logs based on size
    
          * roll-by-time - Roll 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>
    

    3.将程序添加到服务中
    编写好配置文件之后,记得把配置文件和可执行文件放在一起,并把xml文件名改成与软件一样的名字,如WinSw.xml。这样WinSw才能正确识别配置文件。然后我们打开一个cmd窗口或者PowerShell进入WinSw目录。如果在window10上,则可以按住shift键,鼠标右击当前文件夹空白位置,选择“在此处打开Powershell窗口”即可。接入输入.\inSw.exe install 命令就可以把程序添加到服务中了。

    image

    然后打开Windows的服务,我们可以看到这下已经出现了注册的服务,我们可以像一般服务那样开启、关闭它。

    image

    如果不再需要这个服务,使用.\WinSw.exe install命令即可卸载服务。当然还有诸如start、stop这样的命令启动和关闭服务。可以通过.\WinSw.exe --help命令来查看。

    image

    相关文章

      网友评论

          本文标题:Windows上使用WinSW让程序以服务的方式运行

          本文链接:https://www.haomeiwen.com/subject/hsdloltx.html