package main import ( "os" "os/exec" ) func main() { cmd := exec.Command("echo", "Hello, world!") cmd.SysProcAttr = &syscall.SysProcAttr{ Env: []string{"LANG=en_US.UTF-8", "LC_ALL=en_US.UTF-8"}, } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { log.Fatalf("cmd.Run() failed with %s\n", err) } }
package main import ( "os" "os/exec" "syscall" ) func main() { cmd := exec.Command("sleep", "3600") cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, Pgid: 0, } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Start() if err != nil { log.Fatalf("cmd.Start() failed with %s\n", err) } // Send a signal to the process group syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM) }In this example, we create a command to execute the `sleep` command for 1 hour. We set the `SysProcAttr` field to set the process group ID (`Setpgid`) to `true` and the process group ID to `0`. We then start the command and immediately send a `SIGTERM` signal to the process group to terminate it. The `os/exec` package is part of the standard Go library.