gobash

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 7 Imported by: 0

README

gobash

在go环境中执行命令、脚本、可执行文件,日志实时输出。


使用示例

Run

Run执行命令,可以主动结束命令,实时返回日志和错误信息,推荐使用


    command := "for i in $(seq 1 5); do echo 'test cmd' $i;sleep 1; done"
    ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) // 超时控制
	
    // 执行
    result := Run(ctx, command)
    // 实时输出日志和错误信息
    for v := range result.StdOut {
        fmt.Printf(v)
    }
    if result.Err != nil {
        fmt.Println("exec command failed,", result.Err.Error())
    }

Exec

Exec 适合执行单条非阻塞命令,输出标准和错误日志,但日志输出不是实时,注:如果执行命令永久阻塞,会造成协程泄露

    command := "for i in $(seq 1 5); do echo 'test cmd' $i;sleep 1; done"
    out, err := gobash.Exec(command)
    if err != nil {
        return
    }
    fmt.Println(string(out))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(command string) ([]byte, error)

Exec 适合执行单条非阻塞命令,输出标准和错误日志,但日志输出不是实时, 注:如果执行命令永久阻塞,会造成协程泄露

func SetExecutorPath

func SetExecutorPath(path string)

SetExecutorPath 设置执行器

Types

type Result

type Result struct {
	StdOut chan string
	Err    error // 执行完毕命令后,如果为nil,执行命令成功
}

Result 执行命令的结果

func Run added in v1.4.0

func Run(ctx context.Context, command string) *Result

Run 执行命令,可以主动结束命令,执行结果实时返回在Result.StdOut中

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL