您的位置:首页 > 运维架构 > Shell

go 执行shell 同步与异步方式

2017-12-19 14:50 2371 查看
if cmdSaved != "" {
lsCmd := exec.Command("/bin/sh", "-c", cmdSaved)
err = lsCmd.Start()
if err != nil {
log.Error("command not ok")
outM := &outMessage{
NOK_FILE_EXCEPTION,
taskid,
"cmd saved not executed ok",
}
b, err := json.Marshal(outM)
if err != nil {
log.Fatal("json encode message failed")
w.Write([]byte("{\"result\":10,\"message\":\"encode resp message error\"}"))
} else {
w.Write([]byte(string(b)))
}
return
}
}


func Command

原型:

func Command(name string, arg ...string) *Cmd
1
[/code]
作用:Command returns the Cmd struct to execute the named program with the given arguments.

例如:

cmd := exec.Command(“/bin/bash”, “-c”, “ls”)

cmd := exec.Command(“tr”, “a-z”, “A-Z”)

func (*Cmd) Run

func (c *Cmd) Output() ([]byte, error)
1
[/code]
作用:开始运行并等待结束。

这里需要注意Run和Start的区别:

Start starts the specified command but does not wait for it to complete.

开始运行不等待结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: