0

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)].

oguz ismail
  • 1
  • 16
  • 47
  • 69
jangorecki
  • 16,384
  • 4
  • 79
  • 160
  • 1
    Without the `-f` parameter I get just one result. Why do you need it? – nicola Sep 02 '15 at 12:21
  • @nicola Wow, so simple, I was thinking `-f` will make it more strict and didn't even check that. I use it as a helper methods [here](https://github.com/jangorecki/rbitcoind/blob/412afec1fcce4627e57b368836c2cf8e95eb22d3/R/bitcoind.R#L99). Please post your comment as answer. – jangorecki Sep 02 '15 at 12:25

1 Answers1

1

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"
nicola
  • 24,005
  • 3
  • 35
  • 56