package main import ( "fmt" "os" ) func main() { cmd := exec.Command("ls", "-l") err := cmd.Run() if err != nil { fmt.Println("Error running command: ", err) os.Exit(1) } state, err := cmd.Process.Wait() if err != nil { fmt.Println("Error waiting for process: ", err) os.Exit(1) } fmt.Printf("Process exited with code %d\n", state.ExitCode()) }
package main import ( "fmt" "os" "os/exec" ) func main() { cmd1 := exec.Command("ls", "-l") cmd2 := exec.Command("echo", "Hello, World!") cmd3 := exec.Command("sleep", "5") err1 := cmd1.Start() err2 := cmd2.Start() err3 := cmd3.Start() if err1 != nil || err2 != nil || err3 != nil { fmt.Println("Error starting commands: ", err1, err2, err3) os.Exit(1) } state1, _ := cmd1.Process.Wait() state2, _ := cmd2.Process.Wait() state3, _ := cmd3.Process.Wait() fmt.Printf("Process 1 exited with code %d\n", state1.ExitCode()) fmt.Printf("Process 2 exited with code %d\n", state2.ExitCode()) fmt.Printf("Process 3 exited with code %d\n", state3.ExitCode()) }In this example, we create three commands to run "ls -l", "echo 'Hello, World!'", and "sleep 5" commands. We then start each command and wait for them to exit. Finally, we obtain the process state for each command and print out their respective exit codes.