2023-12-27 00:03:45 +00:00
|
|
|
//go:build !windows
|
|
|
|
|
|
|
|
package lifecycle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-14 22:33:25 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-12-27 00:03:45 +00:00
|
|
|
"os/exec"
|
2024-04-14 22:33:25 +00:00
|
|
|
"syscall"
|
2023-12-27 00:03:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getCmd(ctx context.Context, cmd string) *exec.Cmd {
|
|
|
|
return exec.CommandContext(ctx, cmd, "serve")
|
|
|
|
}
|
2024-04-14 22:33:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|