github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/worker/exec/cmd_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package exec
     5  
     6  import (
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"syscall"
    11  )
    12  
    13  // CreateCmd creates an exec.Cmd.
    14  func CreateCmd(cmdStr, workDir string) *exec.Cmd {
    15  	cwd := workDir
    16  	if info, err := os.Stat(workDir); err == nil && !info.IsDir() {
    17  		cwd = filepath.Dir(workDir)
    18  	}
    19  	c := exec.Command(cmdStr, workDir)
    20  	c.Dir = cwd
    21  	c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    22  	return c
    23  }
    24  
    25  // KillCmd sends a KILL signal to the command.
    26  func KillCmd(c *exec.Cmd) error {
    27  	return syscall.Kill(-c.Process.Pid, syscall.SIGKILL)
    28  }