You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
917 B
Go
25 lines
917 B
Go
package ncmd
|
|
|
|
import (
|
|
"os/exec"
|
|
"syscall"
|
|
)
|
|
|
|
// terminateProcess stops the command by sending its process group a SIGTERM signal.
|
|
// Stop is idempotent. An error should only be returning to the rare case that
|
|
// Stop is called immediately after the command ends but before Start can
|
|
// update its internal state.
|
|
func terminateProcess(pid int) error {
|
|
// Signal the process group (-pid), not just the process, so that the process
|
|
// and all its children are signaled. Else, child processes can keep running and
|
|
// keep the stdout/stderr fd open and cause cmd.Wait to hang.
|
|
return syscall.Kill(-pid, syscall.SIGTERM)
|
|
}
|
|
|
|
// Set process group ID so the cmd and all its children become a new
|
|
// process group. This allows Stop to SIGTERM the cmd's process group
|
|
// without killing this process (i.e. this code here).
|
|
func setProcessGroupID(cmd *exec.Cmd) {
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
}
|