2017-06-27 77 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") 
相關問題