This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
/
kprocess.go
137 lines (124 loc) · 3.18 KB
/
kprocess.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
package kutil
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type UtilError string
func (err UtilError) Error() string { return "Utils Error: " + string(err) }
func ProcessName() string {
file := os.Args[0]
i := strings.LastIndex(file, "\\")
j := strings.LastIndex(file, "/")
if j < i {
file = file[i+1:]
} else if j > i {
file = file[j+1:]
}
i = strings.LastIndex(file, ".")
if i > 0 {
file = file[0:i]
}
return file
}
func DefaultPidFileName() string {
file := ProcessName()
pidFile := "/var/run/"
if runtime.GOOS == "windows" {
pidFile = "c:\\" + file + ".pid"
} else {
pidFile += file + ".pid"
}
return pidFile
}
func ProcessFile() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
return path
}
func ProcessPath() string {
path := filepath.Dir(ProcessFile())
return path
}
func WritePidFile(myFile string, pid int) (err error) {
return ioutil.WriteFile(myFile, []byte(fmt.Sprintf("%d", pid)), 0644)
}
func CheckWritePidPermission(pidFile string) error {
if len(pidFile) <= 0 {
pidFile = DefaultPidFileName()
}
file := pidFile + ".tmp"
if err := ioutil.WriteFile(file, []byte(fmt.Sprintf("%d", 0)), 0644); err != nil {
return UtilError("had no permission to write pid file")
}
os.Remove(file)
return nil
}
func ExecProcess(background bool, file string, args ...string) (int, error) {
fmt.Println("args:", args, "-Len:", len(args))
filePath, _ := filepath.Abs(file)
cmd := exec.Command(filePath, args...)
if background {
cmd.Stdin = nil //给新进程设置文件描述符,可以重定向到文件中
cmd.Stdout = nil
cmd.Stderr = nil
} else {
cmd.Stdin = os.Stdin //给新进程设置文件描述符,可以重定向到文件中
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err := cmd.Start()
if err == nil {
return cmd.Process.Pid, nil
}
return -1, err
}
func TryToRunAsDaemon(key, pidFile string) int {
key = strings.ToLower(key)
var argsNew []string
var bFind bool = false
for _, arg := range os.Args {
arg = strings.ToLower(arg)
if arg == key {
bFind = true
} else {
argsNew = append(argsNew, arg)
}
}
if bFind {
if err := CheckWritePidPermission(pidFile); err != nil {
return -1
}
if pid, err := ExecProcess(true, ProcessFile(), argsNew...); err == nil {
return pid
}
return -2
}
return 0
}
func StartProcess(background bool, file string, args []string) (*os.Process, error) {
filePath, _ := filepath.Abs(file)
if background {
return os.StartProcess(filePath, args, &os.ProcAttr{Files: []*os.File{nil, nil, nil}})
}
return os.StartProcess(filePath, args, &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}})
}
func WaitProcess(background bool, file string, args []string) ([]byte, error) {
filePath, _ := filepath.Abs(file)
cmd := exec.Command(filePath, args...)
if background {
cmd.Stdin = nil //给新进程设置文件描述符,可以重定向到文件中
cmd.Stdout = nil
cmd.Stderr = nil
} else {
cmd.Stdin = os.Stdin //给新进程设置文件描述符,可以重定向到文件中
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
//cmd.Run()不能返回内容,而Output可以返回内容。
return cmd.Output()
}