MCPcopy Create free account
hub / github.com/cronitorio/cronitor-cli / verifyProcessExists

Method verifyProcessExists

lib/process_validator.go:408–440  ·  view source on GitHub ↗

verifyProcessExists checks if a process with the given PID exists

(pid int)

Source from the content-addressed store, hash-verified

406
407// verifyProcessExists checks if a process with the given PID exists
408func (pv *ProcessValidator) verifyProcessExists(pid int) error {
409 switch runtime.GOOS {
410 case "linux":
411 // Check /proc/{pid}/stat file on Linux
412 statFile := fmt.Sprintf("/proc/%d/stat", pid)
413 if _, err := os.Stat(statFile); os.IsNotExist(err) {
414 return fmt.Errorf("process does not exist")
415 }
416 case "darwin":
417 // On macOS, use ps command to check if process exists
418 cmd := exec.Command("ps", "-p", fmt.Sprintf("%d", pid))
419 if err := cmd.Run(); err != nil {
420 return fmt.Errorf("process does not exist")
421 }
422 case "windows":
423 // On Windows, use os.FindProcess
424 process, err := os.FindProcess(pid)
425 if err != nil {
426 return fmt.Errorf("process does not exist: %v", err)
427 }
428 // On Windows, FindProcess always succeeds, so we can't easily verify existence
429 // without additional system calls
430 _ = process
431 default:
432 // For other systems, use ps command as fallback
433 cmd := exec.Command("ps", "-p", fmt.Sprintf("%d", pid))
434 if err := cmd.Run(); err != nil {
435 return fmt.Errorf("process does not exist")
436 }
437 }
438
439 return nil
440}
441
442// isKernelThread checks if a process is a kernel thread (Linux-specific)
443func (pv *ProcessValidator) isKernelThread(pid int) (bool, error) {

Callers 1

ValidatePIDMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected