【问题标题】:exec: executable file not found in $PATHexec:在 $PATH 中找不到可执行文件
【发布时间】:2017-06-27 17:42:06
【问题描述】:

我正在尝试将 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)
    }

我已经尝试了很多版本(带/不带参数,带/不带目录,...),但它总是返回相同的错误:

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 命令完成了这项工作,并且可以无缝运行。我错过了什么吗?

【问题讨论】:

    标签: go exec tor


    【解决方案1】:

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

    在您的情况下,您正在使用两个命令并将一个命令的标准输出传送到另一个命令的标准输入。您可以在纯 Go 中执行此操作(将一个的 Stdout 读取器连接到另一个的 Stdin 写入器),或者您可以依赖 shell 来执行此操作。在后一种情况下,您的可执行文件将是sh 或bash,而参数将是["-c", "pidof tor | xargs kill -HUP"]。例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多