-
Notifications
You must be signed in to change notification settings - Fork 5
/
processex.go
70 lines (57 loc) · 2.29 KB
/
processex.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
/*
Package processex - find a os.Process (operating system process) by Name (FindByName) or PID (Find), crossplatform, lightly, fast and full compatible with stdlib os.Process.
Usage
func main() {
processName := "explorer.exe"
process, _, err := processex.FindByName(processName)
if err == processex.ErrNotFound {
fmt.Printf("Process %v not running", processName)
os.Exit(0)
}
if err != nil {
fmt.Printf("Process %v find error: %v", processName, err)
os.Exit(1)
}
fmt.Printf("Process %v PID: %v", processName, process.Pid)
}
Contributing
Welcome pull requests, bug fixes and issue reports.
Before proposing a change, please discuss it first by raising an issue. */
package processex
import (
"os"
)
// Find looks for a running process by its pid.
//
// The Process it returns can be used to obtain information
// about the underlying operating system process.
//
// On Unix systems, FindProcess always succeeds and returns a Process
// for the given pid, regardless of whether the process exists.
func Find(pid int) (*os.Process, error) {
return os.FindProcess(pid)
}
// ------------------------------------------------------------------
// FindByName looks for a running process by its name.
//
// The Process it returns can be used to obtain information
// about the underlying operating system process.
func FindByName(name string) ([]*os.Process, []*ProcessEx, error) {
return NewFinder().FindByName(name)
}
// ------------------------------------------------------------------
// FindByPID looks for a running process by its PID.
//
// The Process it returns can be used to obtain information
// about the underlying operating system process.
func FindByPID(pid int) ([]*os.Process, []*ProcessEx, error) {
return NewFinder().FindByPID(pid)
}
// ------------------------------------------------------------------
// Start starts a new process with the program, arguments and attributes
// specified by name, argv and attr. The argv slice will become os.Args in the
// new process, so it normally starts with the program name.
func Start(name string, argv []string, attr *os.ProcAttr) (*os.Process, error) {
return os.StartProcess(name, argv, attr)
}
// ------------------------------------------------------------------