server_darwin.go 676 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "syscall"
  9. )
  10. func getCmd(ctx context.Context, cmd string) *exec.Cmd {
  11. return exec.CommandContext(ctx, cmd, "serve")
  12. }
  13. func terminate(cmd *exec.Cmd) error {
  14. return cmd.Process.Signal(os.Interrupt)
  15. }
  16. func isProcessExited(pid int) (bool, error) {
  17. proc, err := os.FindProcess(pid)
  18. if err != nil {
  19. return false, fmt.Errorf("failed to find process: %v", err)
  20. }
  21. err = proc.Signal(syscall.Signal(0))
  22. if err != nil {
  23. if errors.Is(err, os.ErrProcessDone) || errors.Is(err, syscall.ESRCH) {
  24. return true, nil
  25. }
  26. return false, fmt.Errorf("error signaling process: %v", err)
  27. }
  28. return false, nil
  29. }