Skip to content

Commit 0f416f7

Browse files
committed
🎉 Init Project
1 parent 0efaa66 commit 0f416f7

File tree

6 files changed

+239
-0
lines changed

6 files changed

+239
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
.idea
17+
a.exe

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/Pow-Duck/processes
2+
3+
go 1.14

processes.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

processes_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package processes
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"testing"
9+
)
10+
11+
func TestBaseCommand(t *testing.T) {
12+
command := exec.Command("a.exe")
13+
command.Stdout = os.Stdout
14+
command.Stderr = os.Stderr
15+
16+
err := command.Start()
17+
if err != nil {
18+
log.Fatalln(err)
19+
}
20+
}
21+
22+
func TestQuery(t *testing.T) {
23+
command, err := RunCommand(`C:\Users\Github/AppData/Local/chia-blockchain/app-1.1.6/resources/app.asar.unpacked/daemon/chia.exe version`)
24+
if err != nil {
25+
log.Fatalln(err)
26+
}
27+
28+
fmt.Println(command)
29+
}

query.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package processes
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"os/exec"
7+
"time"
8+
)
9+
10+
type ProcessQuery struct {
11+
stdout *bytes.Buffer
12+
stderr *bytes.Buffer
13+
}
14+
15+
func Query(exe string, args ...string) (*ProcessQuery, error) {
16+
process := ProcessQuery{
17+
stdout: bytes.NewBuffer([]byte{}),
18+
stderr: bytes.NewBuffer([]byte{}),
19+
}
20+
21+
command := exec.Command(exe, args...)
22+
23+
process.stdout = bytes.NewBuffer([]byte{})
24+
stdoutWrite := bufio.NewWriter(process.stdout)
25+
26+
process.stderr = bytes.NewBuffer([]byte{})
27+
stderrWrite := bufio.NewWriter(process.stderr)
28+
29+
command.Stdout = stdoutWrite
30+
command.Stderr = stderrWrite
31+
32+
errChan := make(chan error)
33+
go func() {
34+
if err := command.Start(); err != nil {
35+
errChan <- err
36+
}
37+
}()
38+
39+
for {
40+
select {
41+
case <-time.After(time.Millisecond * 50):
42+
return &process, nil
43+
44+
case err := <-errChan:
45+
return nil, err
46+
}
47+
}
48+
}

test/a.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
i := 0
10+
for {
11+
i += 1
12+
time.Sleep(time.Second)
13+
fmt.Println("hello world: ", i)
14+
}
15+
}

0 commit comments

Comments
 (0)