fork/exec。没有这样的文件或目录退出状态 1

fork/exec . no such file or directory exit status 1

我在 Mac (darwin/amd64) 上使用 Go 1.10.2 并遇到此错误。它说没有这样的文件或目录。

这是我的代码,

func loop1(gor_name string, ras_ip string) {
    var a string
    var c string
    a = search_path()
    fmt.Printf("当前路径为", a)
    fmt.Println(os.Chdir(a))

    c = fmt.Sprintf("%s %s %s %s", "./goreplay  --input-file ", gor_name, " --input-file-loop --output-http ", ras_ip)
    fmt.Printf("c:  ", c)
    cmd := exec.Command(c)
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    channel <- 1

}

非常感谢您的任何建议。

exec.Command 的函数签名是:

func Command(name string, args ...string) *Cmd

其中 name 是程序名称,args 是参数。试试这个:

cmd := exec.Command("./goreplay", "--input-file", gor_name, "--input-file-loop", "--output-http", ras_ip)

此答案只是关于@cerise-limon 答案的信息。

exec 命令需要 command,然后是 arguments
命令和参数的字符串将抛出相同的错误。

将命令传递给 exec,然后用逗号分隔 args

另一种方法是:

type Client struct {
    logger *logrus.Entry
}

const shell = "/bin/bash"

// Execute executes the provided command
func (c *Client) Execute(command []string) (bool, error) {

    c.logger.Info("Executing command ", shell, " -c ", strings.Join(command, " "))

    output, err := exec.Command(shell, "-c", strings.Join(command, " ")).Output()

    if err != nil {
        return false, err
    }

    c.logger.Info(string(output))

    return true, nil
}

func GetBashClient() *Client {
    logger := logrus.NewEntry(logrus.StandardLogger())
    return &Client{logger: logger}
}

现在可以打电话了

command := []string{
    "/usr/bin/<your script>.sh",
    args1,
    args2
}

GetBashClient().Execute(command)