exec:在$ PATH中找不到可执行文件

Mat*_*Nls 8 exec tor go

我试图在Go中将HUP信号发送给tor.

    command := exec.Command("pidof tor | xargs kill -HUP")
    command.Dir = "/bin"

    if cmdOut, err := command.CombinedOutput(); err != nil {
        log.Panic("There was an error running HUP ", string(cmdOut), err)
        panic(err)
    }
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多版本的这个(带/出args,带/出Dir,......)它总是会带来同样的错误:

2017/06/27 13:36:31 There was an error running HUP exec: "pidof tor | xargs kill -HUP": executable file not found in $PATH
panic: There was an error running HUP exec: "pidof tor | xargs kill -HUP": executable file not found in $PATH

goroutine 1 [running]:
panic(0x639ac0, 0xc42000d260)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
log.Panic(0xc420049f08, 0x3, 0x3)
        /usr/local/go/src/log/log.go:320 +0xc9
main.main()
Run Code Online (Sandbox Code Playgroud)

从控制台运行命令非常有效:

root@c8927c4a456e:/go/src/github.com/project# pidof tor | xargs kill -HUP
Jun 27 13:40:07.000 [notice] Received reload signal (hup). Reloading config and resetting internal state.
Jun 27 13:40:07.000 [notice] Read configuration file "/etc/tor/torrc".
Run Code Online (Sandbox Code Playgroud)

这是我的$ PATH

root@c8927c4a456e:/go/src/github.com/project# echo $PATH
/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Run Code Online (Sandbox Code Playgroud)

我之前使用git命令完成了这项工作,它无缝地工作.我错过了什么吗?

Adr*_*ian 19

根据文档,传递给的第一个参数exec.Command是可执行文件的名称 - 就是这样.它不是由shell解释的; 它是您想要分叉的可执行文件的名称.如果需要传入参数,可以将它们作为附加参数Command传递给,或者之后可以将它们传递给返回的对象.

在您的情况下,您使用两个命令并将一个stdout传递给另一个的stdin.您可以在纯Go中执行此操作(将Stdout读取器中的一个读取到另一个的Stdin编写器),或者您可以依赖shell来执行此操作.在后一种情况下,您的可执行文件将是shbash,并且参数将是["-c", "pidof tor | xargs kill -HUP"].例如:

cmd := exec.Command("bash", "-c", "pidof tor | xargs kill -HUP")
Run Code Online (Sandbox Code Playgroud)