2017-06-27 96 views
1

我正试图将HUP信号发送给tor中的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) 
    } 

我已经试过这众多的版本(与输入/输出指定参数时,与输入/输出迪尔,...),它总是以同样的错误回来:

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() 

运行从控制台命令完美的作品:

[email protected]:/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". 

这里是我的$ PATH

[email protected]:/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 

我以前用git命令做过这件事,并且它可以无缝工作。我错过了什么吗?

回答

9

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

就你而言,你使用了两条命令,并将一条标准输出到另一条标准输入。你可以在纯Go中做到这一点(将一个Stdout阅读器连接到另一个的Stdin作者),或者你可以依靠shell来完成它。在后一种情况下,您的可执行文件为shbash,参数为["-c", "pidof tor | xargs kill -HUP"]。例如:

cmd := exec.Command("bash", "-c", "pidof tor | xargs kill -HUP") 
相关问题