-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (98 loc) · 1.99 KB
/
Copy pathmain.go
File metadata and controls
109 lines (98 loc) · 1.99 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
package main
import (
"github.com/robfig/cron/v3"
"strings"
"time"
)
func main() {
c := newWithSeconds()
c.AddFunc(cronStr, func() {
checkPid()
})
c.Start()
select {}
}
// 返回一个支持至 秒 级别的 cron
func newWithSeconds() *cron.Cron {
secondParser := cron.NewParser(cron.Second | cron.Minute |
cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor)
return cron.New(cron.WithParser(secondParser), cron.WithChain())
}
func checkPid() {
clock, min, sec := time.Now().Clock()
Info.Printf("%d:%d:%d 开始执行\n", clock, min, sec)
if !isTime() {
return
}
process := findAllPid()
canDels := make([]string, 0)
for _, v := range process {
for _, v2 := range blackList {
if v == v2 {
canDels = append(canDels, v)
}
}
}
Info.Println("需要关闭的程序有:")
for _, r := range canDels {
Info.Println(r, ", ")
}
for _, del := range canDels {
b := killProcess(del)
if b {
Info.Println(del, "关闭失败")
} else {
Error.Println(del, "关闭失败")
}
}
}
func isTime() bool {
clock, min, _ := time.Now().Clock()
mtime := NewMytimeByInt(clock, min)
for _, v := range times {
start := NewMytime(v[0])
end := NewMytime(v[1])
if mtime.inner(*start, *end) {
return true
}
}
return false
}
type mytime struct {
hour int
min int
}
func NewMytime(str string) *mytime {
split := strings.Split(str, ":")
m := new(mytime)
if len(split) < 2 {
return m
}
m.hour = str2Int(split[0])
m.min = str2Int(split[1])
return m
}
func NewMytimeByInt(hour int, min int) *mytime {
m := new(mytime)
m.hour = hour
m.min = min
return m
}
func (this *mytime) inner(start mytime, end mytime) bool {
return this.bigger(start) && end.bigger(*this)
}
func (this *mytime) bigger(target mytime) bool {
if this.hour > target.hour {
return true
} else if this.hour == target.hour {
return this.min >= target.min
}
return false
}
func str2Int(str string) int {
sum := 0
for _, v := range str {
sum = sum*10 + int(v-'0')
}
return sum
}