Search code examples
rpid

capture pid process via R when using system()


in windows, would like to capture java pid using R system() like the following:

system("java -jar .\app.jar john_doe 1600_50_555", wait = FALSE)

have tried:

grep("^java.exe",readLines(textConnection(system('tasklist',intern=TRUE))),value=TRUE)

would like to capture that pid without using grep() or additional filtering.


Solution

  • You can use ps package for that.

    library(ps)
    library(dplyr)
    
    system("java -jar .\app.jar john_doe 1600_50_555", wait = FALSE)
    
    pid <- ps() %>% 
      filter(name == "java.exe") %>% 
      pull(pid)