-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_func.go
64 lines (59 loc) · 1.36 KB
/
main_func.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
)
// const RUN_LOCK_FILE = "run.lock"
// 停止程序运行
func stopApp(port int) {
req, _ := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:%d/api/local/stop", port), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(b))
// var err error
// // pid, err := os.ReadFile(RUN_LOCK_FILE)
// pid := miniutils.GetPidByPort(sconf.Port)
// if pid > -1 {
// err = miniutils.KillPid(fmt.Sprintf("%d", pid))
// if err != nil {
// return fmt.Errorf("kill app fail(%v)", err)
// }
// return os.Remove(RUN_LOCK_FILE)
// }
// return fmt.Errorf("app is not running")
}
// 以守护进程的方式后台运行
func startDaemon() error {
args := os.Args
var newArgs []string
for i, arg := range args {
if arg == "-d" || arg == "--d" || i == 0 {
continue
}
newArgs = append(newArgs, arg)
}
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
winArgs := append([]string{"/c", args[0]}, newArgs...)
cmd = exec.Command("cmd", winArgs...)
} else {
cmd = exec.Command(args[0], newArgs...)
}
cmd.Env = os.Environ()
err := cmd.Start()
if err != nil {
return err
}
return nil
// return os.WriteFile(RUN_LOCK_FILE, []byte(fmt.Sprintf("%d", cmd.Process.Pid)), 0755)
}