简体   繁体   English

来自 R system("pgrep -f ...") 调用的额外进程 ID

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

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.当从 R 会话调用system函数并传递pgrep命令以列出与特定名称匹配的进程 id 时, system调用的结果与直接在 shell 中使用的相同pgrep命令生成一个额外的 pid。
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.这可能是在system调用期间创建额外进程的问题,该进程也被pgrep并返回到 R 会话。

The question is how can I avoid this issue and find all the processes id matching to the name?问题是如何避免此问题并找到与名称匹配的所有进程 ID?

To reproduce start any process, I will use gedit process (ubuntu notepad app).要重现启动任何进程,我将使用gedit进程(ubuntu 记事本应用程序)。

Running from R:从 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)] .如果额外的 pid 总是最后一个返回,我可以使用x[-length(x)]

You can get your desired output by dropping the -f parameter in the call to pgrep .您可以通过在对pgrep的调用中删除-f参数来获得所需的输出。 This is what I get from my PC:这是我从我的电脑上得到的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM