mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
Merge branch 'v2' into rc
This commit is contained in:
13
README.md
13
README.md
@@ -1879,6 +1879,19 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- **<big>GetOsBits</big>** : return current os bits (32 or 64).
|
- **<big>GetOsBits</big>** : return current os bits (32 or 64).
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#GetOsBits)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#GetOsBits)]
|
||||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||||
|
- **<big>StartProcess</big>** : start a new process with the specified name and arguments.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#StartProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>StopProcess</big>** : stop a process by pid.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#StopProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>KillProcess</big>** : kill a new process with the specified name and arguments.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#KillProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>GetProcessInfo</big>** : retrieves detailed process information by pid.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#GetProcessInfo)]
|
||||||
|
[[play](todo)]
|
||||||
|
|
||||||
|
|
||||||
<h3 id="tuple"> 23. Tuple package implements tuple data type and some operations on it. <a href="#index">index</a></h3>
|
<h3 id="tuple"> 23. Tuple package implements tuple data type and some operations on it. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -1881,6 +1881,20 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- **<big>GetOsBits</big>** : 获取当前操作系统位数(32/64)。
|
- **<big>GetOsBits</big>** : 获取当前操作系统位数(32/64)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system#GetOsBits)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system#GetOsBits)]
|
||||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||||
|
- **<big>StartProcess</big>** :创建进程。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#StartProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>StopProcess</big>** : 停止进程。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#StopProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>KillProcess</big>** : 杀掉进程。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#KillProcess)]
|
||||||
|
[[play](todo)]
|
||||||
|
- **<big>GetProcessInfo</big>** : 根据进程id获取进程信息。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#GetProcessInfo)]
|
||||||
|
[[play](todo)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<h3 id="tuple"> 23. Tuple 包实现一个元组数据类型。 <a href="#index">回到目录</a></h3>
|
<h3 id="tuple"> 23. Tuple 包实现一个元组数据类型。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
system 包含 os, runtime, shell command 相关函数。
|
system 包含 os, 运行time, shell command 相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -31,6 +31,11 @@ import (
|
|||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
- [StartProcess](#StartProcess)
|
||||||
|
- [StopProcess](#StopProcess)
|
||||||
|
- [KillProcess](#KillProcess)
|
||||||
|
- [GetProcessInfo](#GetProcessInfo)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -248,7 +253,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type (
|
type (
|
||||||
Option func(*exec.Cmd)
|
Option func(*exec.Cmd)
|
||||||
)
|
)
|
||||||
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
@@ -308,3 +313,132 @@ func main() {
|
|||||||
fmt.Println(osBit) // 32 or 64
|
fmt.Println(osBit) // 32 or 64
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="StartProcess">StartProcess</span>
|
||||||
|
|
||||||
|
<p>创建进程。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StartProcess(command string, args ...string) (int, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "2")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(pid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="StopProcess">StopProcess</span>
|
||||||
|
|
||||||
|
<p>停止进程。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StopProcess(pid int) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "10")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = system.StopProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="KillProcess">KillProcess</span>
|
||||||
|
|
||||||
|
<p>杀掉进程。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func KillProcess(pid int) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "10")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = system.KillProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GetProcessInfo">GetProcessInfo</span>
|
||||||
|
|
||||||
|
<p>根据进程id获取进程信息。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetProcessInfo(pid int) (*ProcessInfo, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("ls", "-a")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo, err := system.GetProcessInfo(pid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(processInfo)
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -31,6 +31,11 @@ import (
|
|||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
- [StartProcess](#StartProcess)
|
||||||
|
- [StopProcess](#StopProcess)
|
||||||
|
- [KillProcess](#KillProcess)
|
||||||
|
- [GetProcessInfo](#GetProcessInfo)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -249,7 +254,7 @@ The second parameter of the function is the cmd option control parameter. The ty
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type (
|
type (
|
||||||
Option func(*exec.Cmd)
|
Option func(*exec.Cmd)
|
||||||
)
|
)
|
||||||
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
@@ -288,7 +293,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
<p>Get current os bits, 32bit or 64bit. return 32 or 64.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -309,3 +314,132 @@ func main() {
|
|||||||
fmt.Println(osBit) // 32 or 64
|
fmt.Println(osBit) // 32 or 64
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="StartProcess">StartProcess</span>
|
||||||
|
|
||||||
|
<p>Start a new process with the specified name and arguments.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StartProcess(command string, args ...string) (int, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "2")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(pid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="StopProcess">StopProcess</span>
|
||||||
|
|
||||||
|
<p>Stop a process by pid.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StopProcess(pid int) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "10")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = system.StopProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="KillProcess">KillProcess</span>
|
||||||
|
|
||||||
|
<p>Kill a process by pid.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func KillProcess(pid int) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("sleep", "10")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = system.KillProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GetProcessInfo">GetProcessInfo</span>
|
||||||
|
|
||||||
|
<p>Retrieves detailed process information by pid.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetProcessInfo(pid int) (*ProcessInfo, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pid, err := system.StartProcess("ls", "-a")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo, err := system.GetProcessInfo(pid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(processInfo)
|
||||||
|
}
|
||||||
|
```
|
||||||
190
system/os.go
190
system/os.go
@@ -6,9 +6,12 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/validator"
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
@@ -132,3 +135,190 @@ func byteToString(data []byte, charset string) string {
|
|||||||
func GetOsBits() int {
|
func GetOsBits() int {
|
||||||
return 32 << (^uint(0) >> 63)
|
return 32 << (^uint(0) >> 63)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartProcess start a new process with the specified name and arguments.
|
||||||
|
// Play: todo
|
||||||
|
func StartProcess(command string, args ...string) (int, error) {
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
|
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd.Process.Pid, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopProcess stop a process by pid.
|
||||||
|
// Play: todo
|
||||||
|
func StopProcess(pid int) error {
|
||||||
|
process, err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return process.Signal(os.Kill)
|
||||||
|
}
|
||||||
|
|
||||||
|
// KillProcess kill a process by pid.
|
||||||
|
// Play: todo
|
||||||
|
func KillProcess(pid int) error {
|
||||||
|
process, err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return process.Kill()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessInfo contains detailed information about a process.
|
||||||
|
type ProcessInfo struct {
|
||||||
|
PID int
|
||||||
|
CPU string
|
||||||
|
Memory string
|
||||||
|
State string
|
||||||
|
User string
|
||||||
|
Cmd string
|
||||||
|
Threads []string
|
||||||
|
IOStats string
|
||||||
|
StartTime string
|
||||||
|
ParentPID int
|
||||||
|
NetworkConnections string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProcessInfo retrieves detailed process information by pid.
|
||||||
|
// Play: todo
|
||||||
|
func GetProcessInfo(pid int) (*ProcessInfo, error) {
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/FO", "CSV", "/V")
|
||||||
|
} else {
|
||||||
|
cmd = exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "pid,%cpu,%mem,state,user,comm")
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo, err := parseProcessInfo(output, pid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
processInfo.Threads, _ = getThreadsInfo(pid)
|
||||||
|
processInfo.IOStats, _ = getIOStats(pid)
|
||||||
|
processInfo.StartTime, _ = getProcessStartTime(pid)
|
||||||
|
processInfo.ParentPID, _ = getParentProcess(pid)
|
||||||
|
processInfo.NetworkConnections, _ = getNetworkConnections(pid)
|
||||||
|
}
|
||||||
|
|
||||||
|
return processInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseProcessInfo parses the output of `ps` or `tasklist` to fill the ProcessInfo structure.
|
||||||
|
func parseProcessInfo(output []byte, pid int) (*ProcessInfo, error) {
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
|
||||||
|
if len(lines) < 2 {
|
||||||
|
return nil, fmt.Errorf("no process found with PID %d", pid)
|
||||||
|
}
|
||||||
|
|
||||||
|
var processInfo ProcessInfo
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
fields := strings.Split(lines[1], "\",\"")
|
||||||
|
if len(fields) < 9 {
|
||||||
|
return nil, fmt.Errorf("unexpected tasklist output format")
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo = ProcessInfo{
|
||||||
|
PID: pid,
|
||||||
|
CPU: "N/A",
|
||||||
|
Memory: fields[4], // Memory usage in K
|
||||||
|
State: fields[5],
|
||||||
|
User: "N/A",
|
||||||
|
Cmd: fields[8],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fields := strings.Fields(lines[1])
|
||||||
|
if len(fields) < 6 {
|
||||||
|
return nil, fmt.Errorf("unexpected ps output format")
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo = ProcessInfo{
|
||||||
|
PID: pid,
|
||||||
|
CPU: fields[1],
|
||||||
|
Memory: fields[2],
|
||||||
|
State: fields[3],
|
||||||
|
User: fields[4],
|
||||||
|
Cmd: fields[5],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &processInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getThreadsInfo(pid int) ([]string, error) {
|
||||||
|
cmd := exec.Command("ps", "-T", "-p", strconv.Itoa(pid))
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
|
||||||
|
var threads []string
|
||||||
|
for _, line := range lines[1:] {
|
||||||
|
if strings.TrimSpace(line) != "" {
|
||||||
|
threads = append(threads, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return threads, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIOStats(pid int) (string, error) {
|
||||||
|
filePath := fmt.Sprintf("/proc/%d/io", pid)
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(data), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProcessStartTime(pid int) (string, error) {
|
||||||
|
cmd := exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "lstart=")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(string(output)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getParentProcess(pid int) (int, error) {
|
||||||
|
cmd := exec.Command("ps", "-o", "ppid=", "-p", strconv.Itoa(pid))
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ppid, err := strconv.Atoi(strings.TrimSpace(string(output)))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ppid, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNetworkConnections(pid int) (string, error) {
|
||||||
|
cmd := exec.Command("lsof", "-p", strconv.Itoa(pid), "-i")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(output), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func ExampleSetOsEnv() {
|
func ExampleSetOsEnv() {
|
||||||
err := SetOsEnv("foo", "abc")
|
err := SetOsEnv("foo", "abc")
|
||||||
@@ -75,3 +78,56 @@ func ExampleGetOsBits() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 64
|
// 64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleStartProcess() {
|
||||||
|
pid, err := StartProcess("sleep", "2")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(pid)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleStopProcess() {
|
||||||
|
pid, err := StartProcess("sleep", "10")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = StopProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleKillProcess() {
|
||||||
|
pid, err := StartProcess("sleep", "3")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = KillProcess(pid)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGetProcessInfo() {
|
||||||
|
pid, err := StartProcess("ls", "-a")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
processInfo, err := GetProcessInfo(pid)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(processInfo)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
@@ -53,7 +55,9 @@ func TestExecCommand(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestExecCommand")
|
assert := internal.NewAssert(t, "TestExecCommand")
|
||||||
|
|
||||||
// linux or mac
|
// linux or mac
|
||||||
stdout, stderr, err := ExecCommand("ls")
|
stdout, stderr, err := ExecCommand("ls", func(cmd *exec.Cmd) {
|
||||||
|
cmd.Dir = "/"
|
||||||
|
})
|
||||||
t.Log("std out: ", stdout)
|
t.Log("std out: ", stdout)
|
||||||
t.Log("std err: ", stderr)
|
t.Log("std err: ", stderr)
|
||||||
assert.Equal("", stderr)
|
assert.Equal("", stderr)
|
||||||
@@ -74,16 +78,6 @@ func TestExecCommand(t *testing.T) {
|
|||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestExecCommandWithOption(t *testing.T) {
|
|
||||||
// assert := internal.NewAssert(t, "TestExecCommandWithOption")
|
|
||||||
|
|
||||||
// stdout, stderr, err := ExecCommand("ls", WithForeground())
|
|
||||||
// t.Log("std out: ", stdout)
|
|
||||||
// t.Log("std err: ", stderr)
|
|
||||||
// assert.Equal("", stderr)
|
|
||||||
// assert.IsNil(err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
func TestGetOsBits(t *testing.T) {
|
func TestGetOsBits(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -95,3 +89,56 @@ func TestGetOsBits(t *testing.T) {
|
|||||||
t.Error("os is not 32 or 64 bits")
|
t.Error("os is not 32 or 64 bits")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStartProcess(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestStartProcess")
|
||||||
|
|
||||||
|
pid, err := StartProcess("ls", "-a")
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(true, pid > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKillProcess(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestKillProcess")
|
||||||
|
|
||||||
|
pid, err := StartProcess("ls")
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(true, pid > 0)
|
||||||
|
|
||||||
|
err = KillProcess(pid)
|
||||||
|
assert.IsNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStopProcess(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestStopProcess")
|
||||||
|
|
||||||
|
pid, err := StartProcess("ls")
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(true, pid > 0)
|
||||||
|
|
||||||
|
err = StopProcess(pid)
|
||||||
|
assert.IsNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetProcessInfo(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestGetProcessInfo")
|
||||||
|
|
||||||
|
pid, err := StartProcess("ls", "-a")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
processInfo, err := GetProcessInfo(pid)
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log(processInfo)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user