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.

图片转代码服务由CSDN问答提供 功能建议

我正在Debian环境中运行Go应用程序。 我所有的开发工作都是在OSX上进行的,并且运行良好,但是我在通过Go代码在Shell上运行命令时遇到了问题。

  cmdName:=“ cwebp”  
cmdArgs:= [] string {srcPath,“ -o”,dstPath} 
log.Printf(“正在执行%s:%+ v”,cmdName,cmdArgs)
cmd:= exec.Command(cmdName,cmdArgs ...  )
_,错误:= cmd.StdoutPipe()
if错误!= nil {
 log.Printf(“错误:%s”,错误)
} 
err = cmd.Run()
if错误!=  nil {
 log.Printf(“错误:%s”,err)
} 
   
 
 

该应用程序进程已从我的root用户运行,并且该命令有效

更早的时候,我认为由于不正确的PATH环境变量,我正面临着这个问题。 在运行命令之前进行打印时,我得到了正确的路径。 此外,我尝试使用 LookPath 并获得正确的路径为 / 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题