netclientutils_freebsd.go 678 B

1234567891011121314151617181920212223242526272829
  1. package ncutils
  2. import (
  3. "context"
  4. "log"
  5. "os/exec"
  6. "strings"
  7. "syscall"
  8. "time"
  9. )
  10. // Runs Commands for FreeBSD
  11. func RunCmd(command string, printerr bool) (string, error) {
  12. args := strings.Fields(command)
  13. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  14. defer cancel()
  15. cmd := exec.Command(args[0], args[1:]...)
  16. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  17. go func() {
  18. <-ctx.Done()
  19. _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
  20. }()
  21. out, err := cmd.CombinedOutput()
  22. if err != nil && printerr {
  23. log.Println("error running command:", command)
  24. log.Println(strings.TrimSuffix(string(out), "\n"))
  25. }
  26. return string(out), err
  27. }