Get exit code - Go

前端 未结 4 410
梦毁少年i
梦毁少年i 2020-12-04 13:52

I\'m using the package: os/exec http://golang.org/pkg/os/exec/ to execute a command in the operating system but I don\'t seem to find the way to get the exit code. I can rea

相关标签:
4条回答
  • 2020-12-04 14:35

    It's easy to determine if the exit code was 0 or something else. In the first case, cmd.Wait() will return nil (unless there is another error while setting up the pipes).

    Unfortunately, there is no platform independent way to get the exit code in the error case. That's also the reason why it isn't part of the API. The following snippet will work with Linux, but I haven't tested it on other platforms:

    package main
    
    import "os/exec"
    import "log"
    import "syscall"
    
    func main() {
        cmd := exec.Command("git", "blub")
    
        if err := cmd.Start(); err != nil {
            log.Fatalf("cmd.Start: %v", err)
        }
    
        if err := cmd.Wait(); err != nil {
            if exiterr, ok := err.(*exec.ExitError); ok {
                // The program has exited with an exit code != 0
    
                // This works on both Unix and Windows. Although package
                // syscall is generally platform dependent, WaitStatus is
                // defined for both Unix and Windows and in both cases has
                // an ExitStatus() method with the same signature.
                if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
                    log.Printf("Exit Status: %d", status.ExitStatus())
                }
            } else {
                log.Fatalf("cmd.Wait: %v", err)
            }
        }
    }
    

    Just follow the api docs to find out more :)

    0 讨论(0)
  • 2020-12-04 14:37

    Here's my enhanced version based on @tux21b 's answer

    utils/cmd.go

    package utils
    
    import (
        "bytes"
        "log"
        "os/exec"
        "syscall"
    )
    
    const defaultFailedCode = 1
    
    func RunCommand(name string, args ...string) (stdout string, stderr string, exitCode int) {
        log.Println("run command:", name, args)
        var outbuf, errbuf bytes.Buffer
        cmd := exec.Command(name, args...)
        cmd.Stdout = &outbuf
        cmd.Stderr = &errbuf
    
        err := cmd.Run()
        stdout = outbuf.String()
        stderr = errbuf.String()
    
        if err != nil {
            // try to get the exit code
            if exitError, ok := err.(*exec.ExitError); ok {
                ws := exitError.Sys().(syscall.WaitStatus)
                exitCode = ws.ExitStatus()
            } else {
                // This will happen (in OSX) if `name` is not available in $PATH,
                // in this situation, exit code could not be get, and stderr will be
                // empty string very likely, so we use the default fail code, and format err
                // to string and set to stderr
                log.Printf("Could not get exit code for failed program: %v, %v", name, args)
                exitCode = defaultFailedCode
                if stderr == "" {
                    stderr = err.Error()
                }
            }
        } else {
            // success, exitCode should be 0 if go is ok
            ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
            exitCode = ws.ExitStatus()
        }
        log.Printf("command result, stdout: %v, stderr: %v, exitCode: %v", stdout, stderr, exitCode)
        return
    }
    

    I have tested it on OSX, if it's not working as expected on other platforms, please tell me so we can make it better.

    0 讨论(0)
  • 2020-12-04 14:43

    Since golang version 1.12, the exit code is available natively and in a cross-platform manner. See ExitError and ExitCode().

    ExitCode returns the exit code of the exited process, or -1 if the process hasn't exited or was terminated by a signal.

    if err := cmd.Run() ; err != nil {
        if exitError, ok := err.(*exec.ExitError); ok {
            return exitError.ExitCode()
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:43

    September 2019, Go 1.13 introduced errors.As which supports error "unwrapping" - handy for finding precise errors in a nested call-chain.

    So to extract and inspect the two most common errors when running an external command:

    • os.PathError
    • exec.ExitError

    err := cmd.Run()
    
    var (
        ee *exec.ExitError
        pe *os.PathError
    )
    
    if errors.As(err, &ee) {
        log.Println("exit code error:", ee.ExitCode()) // ran, but non-zero exit code
    
    } else if errors.As(err, &pe) {
        log.Printf("os.PathError: %v", pe) // "no such file ...", "permission denied" etc.
    
    } else if err != nil {
        log.Printf("general error: %v", err) // something really bad happened!
    
    } else {
        log.Println("success!") // ran without error (exit code zero)
    }
    
    0 讨论(0)
提交回复
热议问题