mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
33 lines
559 B
Go
33 lines
559 B
Go
//go:build !windows
|
|
|
|
package tools
|
|
|
|
import (
|
|
"os/exec"
|
|
"syscall"
|
|
)
|
|
|
|
func prepareCommandForTermination(cmd *exec.Cmd) {
|
|
if cmd == nil {
|
|
return
|
|
}
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
}
|
|
|
|
func terminateProcessTree(cmd *exec.Cmd) error {
|
|
if cmd == nil || cmd.Process == nil {
|
|
return nil
|
|
}
|
|
|
|
pid := cmd.Process.Pid
|
|
if pid <= 0 {
|
|
return nil
|
|
}
|
|
|
|
// Kill the entire process group spawned by the shell command.
|
|
_ = syscall.Kill(-pid, syscall.SIGKILL)
|
|
// Fallback kill on the shell process itself.
|
|
_ = cmd.Process.Kill()
|
|
return nil
|
|
}
|