-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.go
More file actions
138 lines (127 loc) · 2.84 KB
/
install.go
File metadata and controls
138 lines (127 loc) · 2.84 KB
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
/**
* Created by coffee on 15/11/23.
*/
package utils
import (
"html/template"
"os"
"path"
"fmt"
"flag"
)
func InstallService(serviceName string,args string){
if !flag.Parsed(){
flag.Parse()
}
_args:=flag.Args()
needInstall := false
for _,arg:=range _args{
if arg == "install"{
needInstall = true
break
}
}
if needInstall {
service := struct {
ServiceName string
ServicePath string
ServiceAegs string
WorkDir string
}{serviceName,GetAppPath(),args,GetAppDir()}
t:= template.Must(template.New("").Parse(sysvScript))
file, err := os.OpenFile(path.Join("/etc/init.d", serviceName), os.O_CREATE | os.O_RDWR, 0766)
if err != nil {
fmt.Printf("打开文件[%s]失败:%s", path.Join("/etc/init.d", serviceName), err)
os.Exit(-1)
}
t.Execute(file, service)
os.Exit(0)
}
}
var sysvScript = `#!/bin/sh
# For RedHat and cousins:
# chkconfig: - 99 01
# description: {{.ServiceName}}
# processname: {{.ServicePath}}
### BEGIN INIT INFO
# Provides: {{.ServicePath}}
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
cmd="{{.ServicePath}} {{.ServiceAegs}}"
name=$(basename $0)
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd '{{.WorkDir}}'
$cmd >> "$stdout_log" 2>> "$stderr_log" &
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill $(get_pid)
for i in {1..10}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
`