linuxea:winsw安装windows程序开机启动服务

marksugar
2022-04-19 / 0 评论 / 1,811 阅读 / 正在检测是否收录...

在windows下也有需要开机启动的需求,而一些程序被打包后是没有做开机启动服务的。但是可以放在开机启动中或者计划任务中被不断的监制。这样的话就需要手动编写脚本完成。

而更有友好的方式是将应用程序作为windown服务进行包装和管理,而在github上winsw项目致力于解决这个问题。

要使用WinSw,至少需要三个文件

  • WinSW.exe可执行程序
  • NAME-service.xml 配置文件注明停止或者启动的参数和路径
  • NAME-service.exe.config配置文件主要用做禁用对应用程序的 CAS 发布者策略的检查

示例如下:

nginx

下载后,将WinSW-x64.exe放置当前目录,修改为nginx-service.exe

PS C:\nginx-1.21.1\nginx-1.21.1> dir


    目录: C:\nginx-1.21.1\nginx-1.21.1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2021/8/31     16:04                conf
d-----        2021/8/31     16:04                contrib
d-----        2021/8/31     16:04                docs
d-----        2021/8/31     16:04                html
d-----        2021/8/31     16:05                logs
d-----        2021/8/31     16:05                temp
-a----        2021/8/31     16:26       17462251 nginx-service.exe
------         2021/7/6     17:42        3752448 nginx.exe

创建nginx-service.xml,内容如下

<service>   
    <id>nginx</id>   
    <name>Nginx Service</name>   
    <description>High Performance Nginx Service</description>   
    <logpath>C:\nginx-1.21.1\nginx-1.21.1\logs</logpath>   
    <log mode="roll-by-size">     
        <sizeThreshold>10240</sizeThreshold>     
        <keepFiles>8</keepFiles>   
    </log>   
    <executable>C:\nginx-1.21.1\nginx-1.21.1\nginx.exe</executable>   
    <startarguments>-p C:\nginx-1.21.1\nginx-1.21.1</startarguments>   
    <stopexecutable>C:\nginx-1.21.1\nginx-1.21.1\nginx.exe</stopexecutable>   
    <stoparguments>-p C:\nginx-1.21.1\nginx-1.21.1 -s stop</stoparguments> 
</service>

创建nginx-service.exe.config文件,内容如下

<configuration>  
    <startup>    
        <supportedRuntime version="v2.0.50727" />    
        <supportedRuntime version="v4.0" />  
    </startup>  
    <runtime>    
        <generatePublisherEvidence enabled="false"/>   
    </runtime>
</configuration>

目录如下

PS C:\nginx-1.21.1\nginx-1.21.1> dir


    目录: C:\nginx-1.21.1\nginx-1.21.1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2021/8/31     16:04                conf
d-----        2021/8/31     16:04                contrib
d-----        2021/8/31     16:04                docs
d-----        2021/8/31     16:04                html
d-----        2021/8/31     16:44                logs
d-----        2021/8/31     16:05                temp
-a----        2021/8/31     16:26       17462251 nginx-service.exe
-a----        2021/8/31     16:37            266 nginx-service.exe.config
-a----        2021/8/31     16:36            650 nginx-service.xml
------         2021/7/6     17:42        3752448 nginx.exe

安装服务

PS C:\nginx-1.21.1\nginx-1.21.1> .\nginx-service.exe install
2021-08-31 16:37:39,775 INFO  - Installing service 'Nginx Service (nginx)'...
2021-08-31 16:37:39,811 INFO  - Service 'Nginx Service (nginx)' was installed successfully.

启动服务即可

image-20210831164455538.png

二进制程序

go二进制传参数

package main

import (
    "fmt"
    "net/http"
    "flag"
)

func main() {
    var src string
    flag.StringVar(&src, "src", "", "source file")    
    flag.Parse()
    flag.Usage()
    
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, you've requested: %s,src=%s\n", r.URL.Path,src)
    })

    http.ListenAndServe(":81", nil)
}
PS C:\Users\super\Desktop\mybook\windows-autostart\test> go build

而后开始制作

WinSW-x64.exe放置当前目录,修改为web-flag-service.exe

  • web-flag-server.exe.config
<configuration>
    <startup>
        <supportedRuntime version="v2.0.50727" />
        <supportedRuntime version="v4.0" />
    </startup>
    <runtime>
        <generatePublisherEvidence enabled="false"/>
    </runtime>
</configuration>
  • web-flag-service.xml

<startarguments>-src "/data/wwwroot"</startarguments>-src "/data/wwwroot"为参数

<service>
    <id>web-flag</id>
    <name>web-flag</name>
    <description>This go test , name: web-flag</description>
    <logpath>C:\test\logs</logpath>
     <log mode="roll-by-size">
        <sizeThreshold>10240</sizeThreshold>
        <keepFiles>8</keepFiles>
    </log>
    <executable>C:\test\web-flag.exe</executable>
    <startarguments>-src "/data/wwwroot"</startarguments>
    <stopexecutable>C:\test\web-flag.exe</stopexecutable>
    <stoparguments>-p C:\test</stoparguments>
</service>
  • 最终的准备文件如下
PS C:\test> dir


    目录: C:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2021/8/31     18:06                logs
-a----        2021/8/31     16:37            266 web-flag-server.exe.config
-a----        2021/8/31     16:26       17462251 web-flag-service.exe
-a----        2021/8/31     18:13            550 web-flag-service.xml
-a----        2021/8/31     18:10        6003712 web-flag.exe

安装

PS C:\test> .\web-flag-service.exe install
2021-08-31 18:03:23,199 INFO  - Installing service 'web-flag (web-flag)'...
2021-08-31 18:03:23,286 INFO  - Service 'web-flag (web-flag)' was installed successfully.

启动即可

image-20210831180603981.png

参考

generatePublisherEvidence
winsw

0

评论

博主关闭了所有页面的评论