package main import ( "fmt" "os" "os/exec" ) func main() { cmd := exec.Command("sleep", "10") err := cmd.Start() if err != nil { fmt.Println("Error starting command:", err) os.Exit(1) } fmt.Println("Process ID:", cmd.Process.Pid) err = cmd.Process.Kill() if err != nil { fmt.Println("Error killing process:", err) os.Exit(1) } }
package main import ( "fmt" "os" "os/signal" "syscall" ) func main() { cmd := exec.Command("sleep", "10") err := cmd.Start() if err != nil { fmt.Println("Error starting command:", err) os.Exit(1) } fmt.Println("Process ID:", cmd.Process.Pid) sig := make(chan os.Signal, 1) signal.Notify(sig, syscall.SIGINT) <-sig err = cmd.Process.Kill() if err != nil { fmt.Println("Error killing process:", err) os.Exit(1) } }This example also uses the exec package to start a new process that sleeps for 10 seconds. However, instead of immediately terminating the process, it waits for a signal (in this case, SIGINT) and then kills the process. Package: os