-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (44 loc) · 990 Bytes
/
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
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"time"
)
var keepCMD, logPATH string
func term() string {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("pidof %v", keepCMD))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Printf("[ LOG ] EXECUCAO PARADA EM: %v - [ ERRO ] %v \n", time.Now().Format("Mon Jan _2 15:04:05 2006"), err)
}
return out.String()
}
func keepalive() {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%v >> %v", keepCMD, logPATH))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Printf("[ LOG ] PROBLEMAS AO REINICAR EM: %v - [ ERRO ] %v \n", time.Now().Format("Mon Jan _2 15:04:05 2006"), err)
}
}
func main() {
arg := os.Args[1:]
if len(arg) < 2 {
log.Fatal(`Utilização: ./go-keepalive <COMMAND_NAME> <PATH_TO_LOG_FILE>`)
}
keepCMD = arg[0]
logPATH = arg[1]
for {
time.Sleep(time.Millisecond * 500)
pid := term()
if pid == "" {
keepalive()
}
}
}