procutils: exec.Cmd setsid only for linux

This commit is contained in:
Yousong Zhou
2020-02-26 10:51:34 +08:00
parent 02de12ae5b
commit 0127b3872e
3 changed files with 24 additions and 6 deletions

View File

@@ -0,0 +1,15 @@
// +build !windows
package procutils
import (
"os/exec"
"syscall"
)
func cmdSetSid(cmd *exec.Cmd) {
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Setsid = true
}

View File

@@ -0,0 +1,7 @@
package procutils
import (
"os/exec"
)
func cmdSetSid(cmd *exec.Cmd) {}

View File

@@ -67,17 +67,13 @@ type defaultExecutor struct{}
func (e *defaultExecutor) Command(name string, args ...string) Cmd {
cmd := exec.Command(name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
cmdSetSid(cmd)
return &defaultCmd{cmd}
}
func (e *defaultExecutor) CommandContext(ctx context.Context, name string, args ...string) Cmd {
cmd := exec.CommandContext(ctx, name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
cmdSetSid(cmd)
return &defaultCmd{cmd}
}