Skip to content

Commit

Permalink
Merge branch 'v2' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Sep 16, 2024
2 parents 1d94896 + 69cf9bb commit ee0afed
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 16 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,19 @@ import "github.com/duke-git/lancet/v2/system"
- **<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)]
[[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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

Expand Down
14 changes: 14 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,20 @@ import "github.com/duke-git/lancet/v2/system"
- **<big>GetOsBits</big>** : 获取当前操作系统位数(32/64)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system#GetOsBits)]
[[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 包实现一个元组数据类型。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>

Expand Down
138 changes: 136 additions & 2 deletions docs/api/packages/system.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# System

system 包含 os, runtime, shell command 相关函数。
system 包含 os, 运行time, shell command 相关函数。

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -31,6 +31,11 @@ import (
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
- [StartProcess](#StartProcess)
- [StopProcess](#StopProcess)
- [KillProcess](#KillProcess)
- [GetProcessInfo](#GetProcessInfo)


<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -248,7 +253,7 @@ func main() {

```go
type (
Option func(*exec.Cmd)
Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
```
Expand Down Expand Up @@ -308,3 +313,132 @@ func main() {
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)
}
```
138 changes: 136 additions & 2 deletions docs/en/api/packages/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
- [StartProcess](#StartProcess)
- [StopProcess](#StopProcess)
- [KillProcess](#KillProcess)
- [GetProcessInfo](#GetProcessInfo)


<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -249,7 +254,7 @@ The second parameter of the function is the cmd option control parameter. The ty

```go
type (
Option func(*exec.Cmd)
Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
```
Expand Down Expand Up @@ -288,7 +293,7 @@ func main() {

### <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>

Expand All @@ -309,3 +314,132 @@ func main() {
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)
}
```
Loading

0 comments on commit ee0afed

Please sign in to comment.