2017-06-27 9 views
1

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

私は(引数/アウトと、ディレクトリ/アウトと、...)は、この多数のバージョンを試してみたし、それが常に同じエラーで戻ってくる:

実行
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に渡されます。これは実行可能ファイルの名前です。それはシェルによって解釈されません。それはforkしたい実行ファイルの名前です。引数を渡す必要がある場合は、の追加のパラメータをCommandに渡すことができます。または、後で返されるオブジェクトに渡すこともできます。

あなたの場合、2つのコマンドを使用して、1つのstdoutを別のもののstdinにパイプしています。これを純粋なGo(Stdoutのリーダーをもう一方のStdinのライターに配管する)で行うこともできますし、シェルを使って行うこともできます。後者の場合、実行ファイルはshまたはbashになり、引数は["-c", "pidof tor | xargs kill -HUP"]になります。例:

cmd := exec.Command("bash", "-c", "pidof tor | xargs kill -HUP") 
関連する問題