在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.1nginx-1.21.1> dir
目录: C:nginx-1.21.1nginx-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.1nginx-1.21.1logs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>C:nginx-1.21.1nginx-1.21.1nginx.exe</executable>
<startarguments>-p C:nginx-1.21.1nginx-1.21.1</startarguments>
<stopexecutable>C:nginx-1.21.1nginx-1.21.1nginx.exe</stopexecutable>
<stoparguments>-p C:nginx-1.21.1nginx-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.1nginx-1.21.1> dir
目录: C:nginx-1.21.1nginx-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.1nginx-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.
启动服务即可
二进制程序
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=%sn", r.URL.Path,src)
})
http.ListenAndServe(":81", nil)
}
PS C:UserssuperDesktopmybookwindows-autostarttest> 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:testlogs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>C:testweb-flag.exe</executable>
<startarguments>-src "/data/wwwroot"</startarguments>
<stopexecutable>C:testweb-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.
启动即可
参考
generatePublisherEvidencewinsw