open-consul/agent/exec/exec_unix.go

32 lines
685 B
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build !windows
// +build !windows
2017-10-25 17:24:24 +00:00
package exec
import (
"os"
"os/exec"
"syscall"
)
// Script returns a command to execute a script through a shell.
func Script(script string) (*exec.Cmd, error) {
shell := "/bin/sh"
if other := os.Getenv("SHELL"); other != "" {
shell = other
}
return exec.Command(shell, "-c", script), nil
}
func SetSysProcAttr(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
// KillCommandSubtree kills the command process and any child processes
func KillCommandSubtree(cmd *exec.Cmd) error {
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}