mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
feat: add StartProcess, StopProcess, KillProcess for system package
This commit is contained in:
@@ -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>
|
||||||
|
|
||||||
@@ -308,3 +308,99 @@ 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>
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -31,6 +31,9 @@ import (
|
|||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
- [StartProcess](#StartProcess)
|
||||||
|
- [StopProcess](#StopProcess)
|
||||||
|
- [KillProcess](#KillProcess)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -249,7 +252,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 +291,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 +312,99 @@ 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>
|
||||||
|
}
|
||||||
|
```
|
||||||
34
system/os.go
34
system/os.go
@@ -132,3 +132,37 @@ 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()
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,42 @@ 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>
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -53,7 +54,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 +77,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 +88,40 @@ 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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user