import ( "os" "syscall" ) func main() { cmd := exec.Command("ls", "-al") if err := cmd.Run(); err != nil { panic(err) } if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok { if status.Exited() { exitCode := status.ExitStatus() // Do something with exitCode } } }
func main() { cmd := exec.Command("sleep", "10") if err := cmd.Start(); err != nil { panic(err) } go func() { time.Sleep(2 * time.Second) if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { panic(err) } }() if err := cmd.Wait(); err != nil { if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok { if status.Signaled() { sig := status.Signal() // Do something with sig } } } }In this example, we start a `sleep 10` command and send it a `SIGTERM` signal after 2 seconds. We then wait for the command to finish executing and check if it was terminated by a signal using `status.Signaled()` and get the signal number using `status.Signal()`. Overall, the Go syscall WaitStatus Exited is a useful feature for handling child process execution and termination in Go programs.