Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/exec: exec.Command docker run exit status 1 #14097

Closed
iexploree opened this issue Jan 26, 2016 · 4 comments
Closed

os/exec: exec.Command docker run exit status 1 #14097

iexploree opened this issue Jan 26, 2016 · 4 comments

Comments

@iexploree
Copy link

package main

/*
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

void executeCmd(const char* cmd)
{
    FILE *fp;
    char buf[1024];
    int readSize;
    int bufSize;

    bufSize = sizeof(buf) - 1;
    fp = popen(cmd, "r");
    if (fp != NULL)
    {
        while(1)
        {
            readSize = fread(buf, sizeof(char), bufSize, fp);
            if(readSize <= 0)
            {
                break;
            }

            buf[readSize] = '\0';
            printf("%s", buf);
        }

        pclose(fp);
    }
    else
        printf("executeCmd popen null:%s\n",strerror(errno));
}
*/
import "C"

import (
    "fmt"
    "os/exec"
    "unsafe"
)

func RunExecCommand() {
    fmt.Println("RunExecCommand")
    bs, err := exec.Command("docker", "run ubuntu:14.04 echo hello").Output()
    if err != nil {
        fmt.Println("docker error:", err)
    } else {
        text := string(bs)
        fmt.Println(text)
    }
}

func RunCPopen() {
    fmt.Println("RunCPopen")

    gostr := "docker run ubuntu:14.04 echo hello"
    cstr := C.CString(gostr)
    C.executeCmd(cstr)
    C.free(unsafe.Pointer(cstr))
}

func main() {
    RunExecCommand()
    RunCPopen()
}

The output is:
RunExecCommand
docker error: exit status 1
RunCPopen
hello

I expect “exec.Command("docker", "run ubuntu:14.04 echo hello").Output()" output is "hello", but it is "docker error: exit status 1". So i write c function "executeCmd" to test it, and it works fine.

My question is why exec.Command is incorrect?

@iexploree
Copy link
Author

go version go1.5.3 linux/amd64
os: ubuntu 14.04

@mattn
Copy link
Member

mattn commented Jan 26, 2016

could you please put three backticks.

```c
code
```

or

```go
code
```

@mattn
Copy link
Member

mattn commented Jan 26, 2016

I don't make sure, but as far as I can see, you need to separate arguments.

bs, err := exec.Command("docker", "run", "ubuntu:14.04", "echo", "hello").Output()

@iexploree
Copy link
Author

@mattn You are right! It need to separate arguments! Thank you ! And thank you for teaching me how to style the code! ^_^

@golang golang locked and limited conversation to collaborators Feb 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants