-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (137 loc) · 2.84 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
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
package main
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/robfig/cron/v3"
)
var restartInProgress = false
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("---------------------")
fmt.Println("FXServer-Restarter")
fmt.Println("© Leon Kappes")
fmt.Println("---------------------")
restartProcess(true)
times := make([]int, 0)
f, err := os.Open("restart.csv")
if err != nil {
log.Fatal(err)
}
r := csv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
for value := range record {
i, err := strconv.Atoi(record[value])
if err != nil {
log.Fatal(err)
}
times = append(times, i)
}
}
c := cron.New()
c.AddFunc("0 * * * *", func() {
t := time.Now()
h := t.Hour()
if contains(times, h) {
time.Sleep(10 * time.Second)
restartProcess(false)
}
})
ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <- ticker.C:
if runs, err := isProcRunning("FXServer.exe"); runs == false && err == nil && restartInProgress == false {
restartProcess(false)
}
case <- quit:
ticker.Stop()
return
}
}
}()
c.Start()
fmt.Println("Press r to restart the server")
for {
fmt.Print("-> ")
scanner.Scan()
var text = scanner.Text()
if text == "r" {
restartProcess(false)
}
}
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func restartProcess(firstrun bool) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if runs, err := isProcRunning("FXServer.exe"); runs == true && err == nil && firstrun == false {
fmt.Println("Stopping FXServer")
restartInProgress = true
cmd := exec.Command("taskkill.exe", "/F", "/IM", "FXServer.exe")
err = cmd.Start()
if err != nil {
fmt.Println("Server was not running?!?")
}
err = os.RemoveAll(dir + "\\cache")
if err != nil {
log.Fatal(err)
}
time.Sleep(15 * time.Second)
cmd = exec.Command("taskkill.exe", "/F", "/IM", "cmd.exe")
err = cmd.Start()
if err != nil {
fmt.Println("Server was not running?!?")
}
}
fmt.Println("Starting FXServer")
cmd := exec.Command("cmd", "/C", "start", "cmd.exe", "@cmd", "/k", dir+"\\run.cmd")
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
restartInProgress = false
if !firstrun {
fmt.Print("-> ")
}
}
func isProcRunning(names ...string) (bool, error) {
if len(names) == 0 {
return false, nil
}
cmd := exec.Command("tasklist.exe", "/fo", "csv", "/nh")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.Output()
if err != nil {
return false, err
}
for _, name := range names {
if bytes.Contains(out, []byte(name)) {
return true, nil
}
}
return false, nil
}