-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
86 lines (76 loc) · 1.86 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
package main
import (
"flag"
"fmt"
"github.com/TomatoMr/visor/config"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
)
var wg sync.WaitGroup
func main() {
var configPath string
var start bool
var stop bool
var daemon bool
var restart bool
flag.StringVar(&configPath, "config", "./config/config.yaml", "assign your config file: -config=your_config_file_path.")
flag.BoolVar(&start, "start", false, "up your app, just like this: -start or -start=true|false.")
flag.BoolVar(&stop, "stop", false, "down your app, just like this: -stop or -stop=true|false.")
flag.BoolVar(&restart, "restart", false, "restart your app, just like this: -restart or -restart=true|false.")
flag.BoolVar(&daemon, "d", false, "daemon, just like this: -start -d or -d=true|false.")
flag.Parse()
if err := config.InitConfig(configPath); err != nil {
fmt.Print(err)
os.Exit(-1)
}
if start {
if daemon {
cmd := exec.Command(os.Args[0], "-start", "-config="+configPath)
cmd.Start()
os.Exit(0)
}
wg.Add(1)
fmt.Println("start.")
Start()
wg.Wait()
}
if stop {
Stop()
}
if restart {
Restart()
}
//处理信号
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
select {
case <-sigs:
return
}
}
func Start() {
defer wg.Done()
ioutil.WriteFile(config.GetConfig().Pid, []byte(fmt.Sprintf("%d", os.Getpid())), 0666) //记录pid
go visorCpu()
go visorMem()
}
func Stop() {
pid, _ := ioutil.ReadFile(config.GetConfig().Pid)
cmd := exec.Command("kill", "-9", string(pid))
cmd.Start()
fmt.Println("kill ", string(pid))
os.Remove(config.GetConfig().Pid) //清除pid
os.Exit(0)
}
func Restart() {
fmt.Println("restarting...")
pid, _ := ioutil.ReadFile(config.GetConfig().Pid)
stop := exec.Command("kill", "-9", string(pid))
stop.Start()
start := exec.Command(os.Args[0], "-start", "-d")
start.Start()
}