1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/snowlyg/learns/advance/windows"
)
func run() {
// 每隔 5 秒打印当前时间
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
// for 循环阻塞程序主进程
// ticker.C 通道每隔五秒会发送一个值
for {
<-ticker.C
// 格式化时间
now := time.Now().Format(time.RFC3339)
fmt.Printf("当前时间:%s \n", now)
}
}
func (p *program) Start() error {
go run()
return nil
}
func (p *program) Stop() error {
//stop
return nil
}
type program struct{}
// usage 获取终端输入参数
func usage(errmsg string) {
fmt.Fprintf(os.Stderr,
"%s\n\n"+
"usage: %s <command> <servicename>\n"+
" where <command> is one of\n"+
" install, remove, start, stop, status .\n"+
" and use install : .\n"+
" install <service name> <exec path> <display name> <system name> <password> \n",
errmsg, os.Args[0])
os.Exit(2)
}
func main() {
// new 一个服务
s, err := windows.NewWindowsService(&program{}, "myservice")
if err != nil {
fmt.Printf("new service get error %v \n", err)
}
if len(os.Args) >= 2 {
srvName := strings.ToLower(os.Args[2])
cmd := strings.ToLower(os.Args[1])
switch cmd {
case "start": // 启动
err := windows.ServiceStart(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("start success")
return
case "install": //安装
if len(os.Args) == 7 {
err := windows.ServiceInstall(srvName, os.Args[3], os.Args[4], os.Args[5], os.Args[6])
if err != nil {
fmt.Printf("%v \n", err)
}
} else if len(os.Args) == 5 {
err := windows.ServiceInstall(srvName, os.Args[3], os.Args[4])
if err != nil {
fmt.Printf("%v \n", err)
}
} else {
usage("error command specified")
}
println("install success")
return
case "stop": // 停止
err := windows.ServiceStop(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("stop success")
return
case "remove": // 卸载
err := windows.ServiceUninstall(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("remove success")
return
case "status": // 查询服务状态
status, _ := windows.ServiceStatus(srvName)
if status == windows.StatusRunning {
println("运行中")
} else if status == windows.StatusStopped {
println("已停止")
} else if status == windows.StatusUninstall {
println("未安装")
} else if status == windows.StatusUninstall {
println("未安装")
}
return
default:
println("invaild command")
}
switch cmd {
case "start": // 启动
err := windows.ServiceStart(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("start success")
return
case "install": //安装
if len(os.Args) == 7 {
err := windows.ServiceInstall(srvName, os.Args[3], os.Args[4], os.Args[5], os.Args[6])
if err != nil {
fmt.Printf("%v \n", err)
}
} else if len(os.Args) == 5 {
err := windows.ServiceInstall(srvName, os.Args[3], os.Args[4])
if err != nil {
fmt.Printf("%v \n", err)
}
} else {
usage("error command specified")
}
println("install success")
return
case "stop": // 停止
err := windows.ServiceStop(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("stop success")
return
case "remove": // 卸载
err := windows.ServiceUninstall(srvName)
if err != nil {
fmt.Printf("%v \n", err)
}
println("remove success")
return
case "status": // 查询服务状态
status, _ := windows.ServiceStatus(srvName)
if status == windows.StatusRunning {
println("运行中")
} else if status == windows.StatusStopped {
println("已停止")
} else if status == windows.StatusUninstall {
println("未安装")
} else if status == windows.StatusUnknown {
println("未知状态")
}
return
default:
println("invaild command")
}
}
s.Run(false)
}
|