Search code examples
gowebp

exec not running command from Golang application


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.


Solution

  • 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.