Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get exit code - Go

Tags:

go

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 read the output though

ie.

package main  import(     "os/exec"     "bytes"     "fmt"     "log"     )  func main() {     cmd := exec.Command("somecommand", "parameter")     var out bytes.Buffer     cmd.Stdout = &out     if err := cmd.Run() ; err != nil {         //log.Fatal( cmd.ProcessState.Success() )         log.Fatal( err )     }     fmt.Printf("%q\n", out.String() ) } 
like image 286
OscarRyz Avatar asked Apr 30 '12 14:04

OscarRyz


People also ask

How do you exit an error in Golang?

Typically, an exit code of 0 means that the program executes successfully. Any other numerical value between 1 and 125 (golang) shows the program encountered an error. We can use the os package to exit a function with a specific exit code in Go. Follow this short to understand how to work with the exit() function.

What is exit status 2 in Golang?

Exit code 2 is supposed to mean 'incorrect arguments' in the Unix tradition. Something like 127 or 255 would be better. -rob. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group.

What does exit status 1 mean?

The "Exit Code 1" is simply a "Generic Exit Code" which means the job failed and this can be for any reason.


2 Answers

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 :)

like image 187
tux21b Avatar answered Sep 28 '22 12:09

tux21b


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()     } } 
like image 35
David Avatar answered Sep 28 '22 11:09

David