forked from jimmy201602/webconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (105 loc) · 1.91 KB
/
main.go
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
package main
import (
"webconsole/utils"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"time"
"webconsole/website"
)
const (
START string = "start"
STOP string = "stop"
STATUS string = "status"
)
type Apibox struct {
PID int
}
func (a *Apibox) GetPID() (*Apibox, error) {
b, err := ioutil.ReadFile(utils.PidPath)
if nil != err {
return nil, err
}
b = bytes.TrimSpace(b)
pid, err := utils.StringUtils(string(b)).Int()
if nil != err {
return nil, err
}
a.PID = pid
return a, nil
}
func (a *Apibox) Start() error {
website.Run()
return nil
}
func (a *Apibox) Stop() error {
time.Sleep(time.Duration(1 * time.Second))
if utils.Exists(utils.PidPath) {
_, err := a.GetPID()
if nil != err {
return err
}
p, err := os.FindProcess(a.PID)
if nil != err {
return err
}
err = p.Kill()
if nil != err {
return err
}
} else {
return fmt.Errorf("Unable to read the PID file.")
}
return nil
}
func (a *Apibox) Status() (bool, error) {
if utils.Exists(utils.PidPath) {
_, err := a.GetPID()
if nil != err {
return false, err
}
if err := syscall.Kill(a.PID, 0); nil != err {
return false, nil
} else {
return true, nil
}
} else {
return false, fmt.Errorf("Unable to read the PID file.")
}
}
func main() {
flag.Parse()
var cmd string = flag.Arg(0)
cmd = strings.ToLower(cmd)
switch strings.TrimSpace(cmd) {
case START:
a := &Apibox{}
err := a.Start()
if nil != err {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}
case STOP:
a := &Apibox{}
err := a.Stop()
if nil != err {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}
case STATUS:
a := &Apibox{}
t, err := a.Status()
if nil != err {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}
if !t {
fmt.Fprintf(os.Stdout, "Stop.\n")
} else {
fmt.Fprintf(os.Stdout, "Running...\n")
}
default:
fmt.Fprintf(os.Stderr, "Usage: %s command <start|stop|status>\n", os.Args[0])
}
}