server_unix.go 702 B

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