MCPcopy
hub / github.com/keploy/keploy / isProcessRunning

Function isProcessRunning

utils/utils.go:980–1019  ·  view source on GitHub ↗

isProcessRunning checks if a particular process is still running using os.FindProcess

(pid int)

Source from the content-addressed store, hash-verified

978
979// isProcessRunning checks if a particular process is still running using os.FindProcess
980func isProcessRunning(pid int) (bool, error) {
981 // On Windows use PowerShell to check PID presence because signalling with 0 fails.
982 if runtime.GOOS == "windows" {
983 // Use exit code instead of parsing output to avoid locale-dependent string matching.
984 // PowerShell exits with 0 when process exists, 1 when not found.
985 cmd := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command",
986 fmt.Sprintf("if (Get-Process -Id %d -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }", pid))
987 err := cmd.Run()
988 if err != nil {
989 // Non-zero exit code means process not found
990 if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != 0 {
991 return false, nil
992 }
993 // Other errors (like command not found) are treated as errors
994 return false, err
995 }
996 // Exit code 0 means process was found
997 return true, nil
998 }
999
1000 // POSIX: try to find the process and signal 0 to check existence
1001 process, err := os.FindProcess(pid)
1002 if err != nil {
1003 return false, err // Process lookup failed
1004 }
1005
1006 // Attempt to signal the process (0 means no action, just error check)
1007 err = process.Signal(syscall.Signal(0))
1008 if err != nil {
1009 // If error occurs when signaling, it means the process is no longer running
1010 // If there’s another error, consider the process as still running
1011 if errors.Is(err, os.ErrProcessDone) || err == syscall.ESRCH {
1012 return false, nil
1013 }
1014 return true, nil
1015 }
1016
1017 // No error means the process is running
1018 return true, nil
1019}
1020
1021func uniqueProcessGroups(pids []int) ([]int, error) {
1022 uniqueGroups := make(map[int]bool)

Callers 2

InterruptProcessTreeFunction · 0.85
waitForProcessExitFunction · 0.85

Calls 1

RunMethod · 0.65

Tested by

no test coverage detected