dongpao1921 2015-12-17 10:39
浏览 54
已采纳

exec未从Golang应用程序运行命令

I am running a Go application in a Debian environment. All my development has been carried out on OSX and it ran fine, but I am having a problem running a command on shell from my Go code.

cmdName := "cwebp"
cmdArgs := []string{srcPath, "-o", dstPath}
log.Printf("Executing %s : %+v", cmdName, cmdArgs)
cmd := exec.Command(cmdName, cmdArgs...)
_, err := cmd.StdoutPipe()
if err != nil {
    log.Printf("Error: %s", err)
}
err = cmd.Run()
if err != nil {
    log.Printf("Error: %s", err)
}

The application process is running from my root user and the command works fine when I run it from shell.

Earlier, I thought that I am facing the issue due to an incorrect PATH environment variable. On printing that before running the command, I get the correct path. Moreover, I have tried to use LookPath and got the correct path as /usr/local/bin/cwebp.

  • 写回答

1条回答 默认 最新

  • duanjie6912 2015-12-18 05:37
    关注

    The problem was not with the execution of the command but somehow Go was not able to find the correct library. On checking the output of the command, I got the following:

    cwebp: error while loading shared libraries: libwebp.so.5: cannot open shared object file: No such file or directory
    

    This led me to the direction that the installation of libwebp must have been faulty. Earlier, I had built libwebp from source. So, I installed it via apt-get install libwebp-dev and the command ran successfully.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?