exec: 在 $PATH 中找不到可执行文件
exec: executable file not found in $PATH
我正在尝试将 HUP 信号发送到 Go 中的 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)
}
我已经尝试了很多版本(with/out args,with/out the 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()
运行 来自控制台的命令完美运行:
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".
这是我的 $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
我之前使用 git 命令完成了此操作,并且它可以无缝运行。我错过了什么吗?
Per the documentation,传递给 exec.Command
的第一个参数是可执行文件的名称 - 仅此而已。 shell 不解释它;它是您要分叉的可执行文件的名称。如果需要传入参数,可以将它们作为附加参数传递给Command
,也可以随后将它们传递给返回的对象。
在您的例子中,您使用了两个命令并将一个命令的标准输出通过管道传输到另一个命令的标准输入。您可以在纯 Go 中执行此操作(将一个的 Stdout reader 管道传输到另一个的 Stdin 编写器),或者您可以依靠 shell 来执行此操作。在后一种情况下,您的可执行文件将是 sh
或 bash
,并且参数将是 ["-c", "pidof tor | xargs kill -HUP"]
。例如:
cmd := exec.Command("bash", "-c", "pidof tor | xargs kill -HUP")
我正在尝试将 HUP 信号发送到 Go 中的 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)
}
我已经尝试了很多版本(with/out args,with/out the 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()
运行 来自控制台的命令完美运行:
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".
这是我的 $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
我之前使用 git 命令完成了此操作,并且它可以无缝运行。我错过了什么吗?
Per the documentation,传递给 exec.Command
的第一个参数是可执行文件的名称 - 仅此而已。 shell 不解释它;它是您要分叉的可执行文件的名称。如果需要传入参数,可以将它们作为附加参数传递给Command
,也可以随后将它们传递给返回的对象。
在您的例子中,您使用了两个命令并将一个命令的标准输出通过管道传输到另一个命令的标准输入。您可以在纯 Go 中执行此操作(将一个的 Stdout reader 管道传输到另一个的 Stdin 编写器),或者您可以依靠 shell 来执行此操作。在后一种情况下,您的可执行文件将是 sh
或 bash
,并且参数将是 ["-c", "pidof tor | xargs kill -HUP"]
。例如:
cmd := exec.Command("bash", "-c", "pidof tor | xargs kill -HUP")