server_unix.go 702 Bytes
Newer Older
1
2
3
4
5
6
//go:build !windows

package lifecycle

import (
	"context"
7
8
9
	"errors"
	"fmt"
	"os"
10
	"os/exec"
11
	"syscall"
12
13
14
15
16
)

func getCmd(ctx context.Context, cmd string) *exec.Cmd {
	return exec.CommandContext(ctx, cmd, "serve")
}
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

func terminate(cmd *exec.Cmd) error {
	return cmd.Process.Signal(os.Interrupt)
}

func isProcessExited(pid int) (bool, error) {
	proc, err := os.FindProcess(pid)
	if err != nil {
		return false, fmt.Errorf("failed to find process: %v", err)
	}

	err = proc.Signal(syscall.Signal(0))
	if err != nil {
		if errors.Is(err, os.ErrProcessDone) || errors.Is(err, syscall.ESRCH) {
			return true, nil
		}

		return false, fmt.Errorf("error signaling process: %v", err)
	}

	return false, nil
}