Repository navigation
10000
More file actions
Expand file tree
/
Copy pathgproc.go
More file actions
161 lines (143 loc) · 3.77 KB
/
Copy pathgproc.go
File metadata and controls
161 lines (143 loc) · 3.77 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gproc implements management and communication for processes.
package gproc
import (
"bytes"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/util/gconv"
"io"
"os"
"runtime"
"strings"
"time"
)
const (
gPROC_ENV_KEY_PPID_KEY = "GPROC_PPID"
gPROC_TEMP_DIR_ENV_KEY = "GPROC_TEMP_DIR"
)
var (
// 进程开始执行时间
processStartTime = time.Now()
)
// 获取当前进程ID
func Pid() int {
return os.Getpid()
}
// 获取父进程ID(gproc父进程,如果当前进程本身就是父进程,那么返回自身的pid,不存在时则使用系统父进程)
func PPid() int {
if !IsChild() {
return Pid()
}
// gPROC_ENV_KEY_PPID_KEY为gproc包自定义的父进程
ppidValue := os.Getenv(gPROC_ENV_KEY_PPID_KEY)
if ppidValue != "" && ppidValue != "0" {
return gconv.Int(ppidValue)
}
return PPidOS()
}
// 获取父进程ID(系统父进程)
func PPidOS() int {
return os.Getppid()
}
// 判断当前进程是否为gproc创建的子进程
func IsChild() bool {
ppidValue := os.Getenv(gPROC_ENV_KEY_PPID_KEY)
return ppidValue != "" && ppidValue != "0"
}
// 设置gproc父进程ID,当ppid为0时表示该进程为gproc主进程,否则为gproc子进程
func SetPPid(ppid int) error {
if ppid > 0 {
return os.Setenv(gPROC_ENV_KEY_PPID_KEY, gconv.String(ppid))
} else {
return os.Unsetenv(gPROC_ENV_KEY_PPID_KEY)
}
}
// 进程开始执行时间
func StartTime() time.Time {
return processStartTime
}
// 进程已经运行的时间(毫秒)
func Uptime() int {
return int(time.Now().UnixNano()/1e6 - processStartTime.UnixNano()/1e6)
}
// 阻塞执行shell指令,并给定输入输出对象
func Shell(cmd string, out io.Writer, in io.Reader) error {
p := NewProcess(getShell(), []string{getShellOption(), cmd})
p.Stdin = in
p.Stdout = out
return p.Run()
}
// 阻塞执行shell指令,并输出结果当终端(如果需要异步,请使用goroutine)
func ShellRun(cmd string) error {
p := NewProcess(getShell(), []string{getShellOption(), cmd})
return p.Run()
}
// 阻塞执行shell指令,并返回输出结果(如果需要异步,请使用goroutine)
func ShellExec(cmd string) (string, error) {
buf := bytes.NewBuffer(nil)
p := NewProcess(getShell(), []string{getShellOption(), cmd})
p.Stdout = buf
err := p.Run()
return buf.String(), err
}
// 检测环境变量中是否已经存在指定键名
func checkEnvKey(env []string, key string) bool {
for _, v := range env {
if len(v) >= len(key) && strings.EqualFold(v[0:len(key)], key) {
return true
}
}
return false
}
// 获取当前系统下的shell路径
func getShell() string {
switch runtime.GOOS {
case "windows":
return searchBinFromEnvPath("cmd.exe")
default:
path := searchBinFromEnvPath("bash")
if path == "" {
path = searchBinFromEnvPath("sh")
}
return path
}
}
// 获取当前系统默认shell执行指令的option参数
func getShellOption() string {
switch runtime.GOOS {
case "windows":
return "/c"
default:
return "-c"
}
}
// 从环境变量PATH中搜索可执行文件
func searchBinFromEnvPath(file string) string {
// 如果是绝对路径,或者相对路径下存在,那么直接返回
if gfile.Exists(file) {
return file
}
array := ([]string)(nil)
switch runtime.GOOS {
case "windows":
array = strings.Split(os.Getenv("Path"), ";")
if gfile.Ext(file) != ".exe" {
file += ".exe"
}
default:
array = strings.Split(os.Getenv("PATH"), ":")
}
if len(array) > 0 {
for _, v := range array {
path := v + gfile.Separator + file
if gfile.Exists(path) {
return path
}
}
}
return ""
}