go执行linuxshell

go执行linux系统命令

package main
import (
	"os/exec"
	"context"
	"time"
	"fmt"
)

//执行linux命令并获取标准输出和标准错误
func Cmd(command string) (string,error) {
	cmd := exec.Command("bash","-c",command)
	out, err := cmd.CombinedOutput()
	result := string(out)
	return result,err
}

//执行linux命令获取标准输出和标准错误并设置超时时间
func CmdTime(command string,ts time.Duration)(string,error)  {
	ctx, _ := context.WithTimeout(context.Background(), time.Second*ts)
    cmd := exec.CommandContext(ctx, "bash","-c",command)
	out, err := cmd.CombinedOutput()
	result := string(out)
	return result,err
}

func main () {
	re,err := Cmd("echo hello word")
	fmt.Println(re,err)
	re,err = CmdTime("bash /root/workplace/src/go-exec/i.sh",5)
	fmt.Println(re,err)
}

输出结果:
go执行linuxshell_第1张图片
有一个第三方模块写的挺牛逼的:https://github.com/codeskyblue/go-sh
同时附上几个参考链接供读者享用
https://www.jianshu.com/p/149e42688027
https://studygolang.com/articles/3329

官方文档:https://golang.org/pkg/os/exec/

你可能感兴趣的:(go)