|
| 1 | +package processes |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type Process struct { |
| 12 | + Cmd *exec.Cmd |
| 13 | +} |
| 14 | + |
| 15 | +func Command(exe string, args ...string) (*Process, error) { |
| 16 | + command := exec.Command(exe, args...) |
| 17 | + errChan := make(chan error) |
| 18 | + go func() { |
| 19 | + if err := command.Start(); err != nil { |
| 20 | + errChan <- err |
| 21 | + } |
| 22 | + }() |
| 23 | + |
| 24 | + for { |
| 25 | + select { |
| 26 | + case <-time.After(time.Millisecond * 50): |
| 27 | + return &Process{Cmd: command}, nil |
| 28 | + |
| 29 | + case err := <-errChan: |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func ProcessSnapshot() (map[string]string, error) { |
| 36 | + if runtime.GOOS == "windows" { |
| 37 | + return processSnapshotWindows() |
| 38 | + } |
| 39 | + |
| 40 | + return processSnapshotLinux() |
| 41 | +} |
| 42 | + |
| 43 | +func GetPid(serverName string) (string, error) { |
| 44 | + snapshot, err := ProcessSnapshot() |
| 45 | + if err != nil { |
| 46 | + return "", err |
| 47 | + } |
| 48 | + for k, v := range snapshot { |
| 49 | + if strings.Index(v, serverName) != -1 { |
| 50 | + return k, nil |
| 51 | + } |
| 52 | + } |
| 53 | + return "", fmt.Errorf("NOT FUND") |
| 54 | +} |
| 55 | + |
| 56 | +func KillByPid(pid string) error { |
| 57 | + if runtime.GOOS == "windows" { |
| 58 | + return killPidWindows(pid) |
| 59 | + } |
| 60 | + return killPidLinux(pid) |
| 61 | +} |
| 62 | + |
| 63 | +func killPidLinux(pid string) error { |
| 64 | + _, err := RunCommand(fmt.Sprintf("kill -9 %s", pid)) |
| 65 | + return err |
| 66 | +} |
| 67 | + |
| 68 | +func killPidWindows(pid string) error { |
| 69 | + c := fmt.Sprintf(`taskkill /F /pid %s`, pid) |
| 70 | + _, err := RunCommand(c) |
| 71 | + return err |
| 72 | +} |
| 73 | + |
| 74 | +func processSnapshotLinux() (map[string]string, error) { |
| 75 | + rp := map[string]string{} |
| 76 | + a := `ps -ef |awk '{print $2,$8}'` |
| 77 | + command, err := RunCommand(a) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + split := strings.Split(command, "\n") |
| 82 | + for _, v := range split { |
| 83 | + sp2 := strings.Split(v, " ") |
| 84 | + if len(sp2) >= 2 { |
| 85 | + rp[sp2[0]] = sp2[1] |
| 86 | + } |
| 87 | + } |
| 88 | + return rp, nil |
| 89 | +} |
| 90 | + |
| 91 | +func processSnapshotWindows() (map[string]string, error) { |
| 92 | + rp := map[string]string{} |
| 93 | + a := "tasklist" |
| 94 | + command, err := RunCommand(a) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + split := strings.Split(command, "\n") |
| 99 | + for _, v := range split { |
| 100 | + sp2 := strings.Split(v, " ") |
| 101 | + sp2 = clearNone(sp2) |
| 102 | + if len(sp2) >= 2 { |
| 103 | + rp[sp2[1]] = sp2[0] |
| 104 | + } |
| 105 | + } |
| 106 | + return rp, nil |
| 107 | +} |
| 108 | + |
| 109 | +func runInWindows(cmd string) (string, error) { |
| 110 | + result, err := exec.Command("cmd", "/c", cmd).Output() |
| 111 | + if err != nil { |
| 112 | + return "", err |
| 113 | + } |
| 114 | + return strings.TrimSpace(string(result)), err |
| 115 | +} |
| 116 | + |
| 117 | +func RunCommand(cmd string) (string, error) { |
| 118 | + if runtime.GOOS == "windows" { |
| 119 | + return runInWindows(cmd) |
| 120 | + } else { |
| 121 | + return runInLinux(cmd) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func runInLinux(cmd string) (string, error) { |
| 126 | + //fmt.Println("Running Linux cmd:" + cmd) |
| 127 | + result, err := exec.Command("/bin/sh", "-c", cmd).Output() |
| 128 | + if err != nil { |
| 129 | + return "", err |
| 130 | + } |
| 131 | + return strings.TrimSpace(string(result)), err |
| 132 | +} |
| 133 | + |
| 134 | +func clearNone(sp []string) []string { |
| 135 | + var result []string |
| 136 | + for _, v := range sp { |
| 137 | + if v != "" { |
| 138 | + result = append(result, v) |
| 139 | + } |
| 140 | + } |
| 141 | + return result |
| 142 | +} |
0 commit comments