Extra process id from R system("pgrep -f ...") call

194 views Asked by At

When calling system function from R session and passing pgrep command to list processes id matching the particular name the results from the system call produce one extra pid vs the same pgrep command used directly in shell.
This is likely the problem of creating an extra process during the system call which is also catched by pgrep and returned to R session.

The question is how can I avoid this issue and find all the processes id matching to the name?

To reproduce start any process, I will use gedit process (ubuntu notepad app).

Running from R:

system("pgrep -f gedit", intern = TRUE)
# [1] "4898" "5014"

Running from shell:

pgrep -f gedit
# 4898

If the extra pid is always last one returned I could use x[-length(x)].

1

There are 1 answers

0
nicola On BEST ANSWER

You can get your desired output by dropping the -f parameter in the call to pgrep. This is what I get from my PC:

system("pgrep gedit", intern = TRUE)
#[1] "2888"
system("pgrep -f gedit", intern = TRUE)
#[1] "2888" "5839"