[Rd] Detecting whether a process exists or not by its PID?
Henrik Bengtsson
henrik@bengt@@on @ending from gm@il@com
Fri Aug 31 01:18:29 CEST 2018
Hi, I'd like to test whether a (localhost) PSOCK cluster node is still
running or not by its PID, e.g. it may have crashed / core dumped.
I'm ok with getting false-positive results due to *another* process
with the same PID has since started.
I can the PID of each cluster nodes by querying them for their
Sys.getpid(), e.g.
pids <- parallel::clusterEvalQ(cl, Sys.getpid())
Is there a function in core R for testing whether a process with a
given PID exists or not? From trial'n'error, I found that on Linux:
pid_exists <- function(pid) as.logical(tools::pskill(pid, signal = 0L))
returns TRUE for existing processes and FALSE otherwise, but I'm not
sure if I can trust this. It's not a documented feature in
?tools::pskill, which also warns about 'signal' not being standardized
across OSes.
The other Linux alternative I can imagine is:
pid_exists <- function(pid) system2("ps", args = c("--pid", pid),
stdout = FALSE) == 0L
Can I expect this to work on macOS as well? What about other *nix systems?
And, finally, what can be done on Windows?
I'm sure there are packages on CRAN that provides this, but I'd like
to keep dependencies at a minimum.
I appreciate any feedback. Thxs,
Henrik
More information about the R-devel
mailing list