2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build !windows
|
2017-08-02 15:01:21 +00:00
|
|
|
// +build !windows
|
2017-10-25 17:24:24 +00:00
|
|
|
|
2017-10-25 09:18:07 +00:00
|
|
|
package exec
|
2017-08-02 15:01:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-10-11 18:57:39 +00:00
|
|
|
"syscall"
|
2017-08-02 15:01:21 +00:00
|
|
|
)
|
|
|
|
|
2017-10-25 09:18:07 +00:00
|
|
|
// Script returns a command to execute a script through a shell.
|
|
|
|
func Script(script string) (*exec.Cmd, error) {
|
2017-08-02 15:01:21 +00:00
|
|
|
shell := "/bin/sh"
|
|
|
|
if other := os.Getenv("SHELL"); other != "" {
|
|
|
|
shell = other
|
|
|
|
}
|
|
|
|
return exec.Command(shell, "-c", script), nil
|
|
|
|
}
|
2017-10-11 18:57:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|