2026-03-28 17:14:39 +00:00
|
|
|
//go:build !windows
|
|
|
|
|
|
|
|
|
|
package pid
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-07 08:34:42 +00:00
|
|
|
"errors"
|
2026-03-28 17:14:39 +00:00
|
|
|
"os"
|
|
|
|
|
"syscall"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// isProcessRunning checks whether a process with the given PID is alive
|
|
|
|
|
// on Unix-like systems using signal(0).
|
|
|
|
|
func isProcessRunning(pid int) bool {
|
|
|
|
|
if pid <= 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// Signal(nil) does not kill the process but checks existence on Unix.
|
2026-04-07 08:34:42 +00:00
|
|
|
err = p.Signal(syscall.Signal(0))
|
|
|
|
|
if err == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
var errno syscall.Errno
|
|
|
|
|
// EPERM means the process exists but we are not allowed to signal it.
|
|
|
|
|
return errors.As(err, &errno) && errno == syscall.EPERM
|
2026-03-28 17:14:39 +00:00
|
|
|
}
|