关于pgrep返回自身的确认 [英] Confirmation about pgrep returning itself

查看:66
本文介绍了关于pgrep返回自身的确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了几篇关于pgrep似乎返回自身的情况的帖子,即使它永远都不会返回.关键似乎在于bash和sh的功能之间的区别.除了我的情况,我已经确认sh确实是bash的链接.

  • 我正在SuSE 12 x86_64上运行
  • /bin/sh是bash的链接
  • /bin/bash是真正的二进制文件

我有一个像这样调用pgrep的Ruby脚本:

  cmd ="/usr/bin/pgrep -lf \"#{target} \"pidList =`#{cmd}` 

我需要使用完整的命令行,因为我实际上是使用一个参数来唯一标识特定的"java"进程.

现在,由于一些无关的愚蠢,我几乎立即对返回的每个PID进行了 ps -p .有一阵子,这让我非常悲痛,因为ps有时什么也不会返回.最终,我能够捕捉到pid上的ps返回pgrep命令的情况.但这是pgrep命令本身,而不是 sh -c"pgrep -f blah"

回顾:

pgrep永远不会返回自己.但是sh vs bash的差异可能导致其显示子外壳.但我证实sh是bash的链接,因此行为上不应有任何区别.

我怀疑(并且正在寻找确认)的原因是,由于Ruby的反引号,正在创建一个额外的子命令,而这正是pgrep命令所拾取的(有时是..时序问题?)./p>

这确实是一个痛苦,我想确保我实施的修复程序能够真正解决问题.鉴于我正在使用的代码,我要么去

  1. 附加 |grep -v grep 到命令末尾
  2. 在Ruby脚本中遍历返回的结果时,丢弃所有包含'grep'的结果

我认为#2更快,但仍然让我感到烦恼,我必须过滤掉pgrep本身.

我是在正确的轨道上吗?还是您认为其他事情正在发挥作用?

感谢您的时间!

解决方案

答案已经在我的问题的注释中,但是我认为我将以正式答案将其结束.

我所缺少的信息是

当bash作为sh调用时,它的行为就像POSIX sh,而不是bash.–JörgW Mittag 1月23日23:31

所以是的,pgrep表现正常.但是,当您通过反引号从Ruby脚本调用它时,您仍然需要过滤掉"pgrep"

I have read several posts here about cases where pgrep 'seems' to return itself even though it never should. The key seems to be the difference between how bash and sh function. Except that in my case, I have confirmed that sh really is a link to bash.

  • I'm running on SuSE 12 x86_64
  • /bin/sh is a link to bash
  • /bin/bash is the real binary

I have a Ruby script which calls pgrep like this:

  cmd="/usr/bin/pgrep -lf \"#{target}\""
  pidList=`#{cmd}`

I need to use the full command line because I'm actually using an argument to uniquely identify a specific 'java' process.

Now, due to some unrelated foolishness, I almost immediately do a ps -p on each of the pids returned. For a while, this was causing me great grief because the ps would sometimes return nothing. Eventually I was able to catch a case where the ps on the pid returned the pgrep command. But it was the pgrep command itself, not something like sh -c "pgrep -f blah"

To recap:

pgrep never returns itself. But differences in sh vs bash can cause it to show a subshell. But I verified that sh is a link to bash, so there should be no difference in behavior.

What I suspect (and am looking for confirmation for) is that an extra subcommand is being created because of the Ruby backticks and that is what is (only sometimes.. timing issues?) being picked up by the pgrep command.

This has been a real pain and I want to make sure the fix I implement will truly make the problem go away. Given the code I'm working with, I'm either going to

  1. append a | grep -v grep to the end of my command
  2. throw out any results containing 'grep' while looping through the returned results within the Ruby script

I figure #2 is faster, but it still irks me that I have to filter out pgrep itself.

Am I on the right track or do you think something else is at play?

Thanks for your time!

解决方案

The answer is already in the comments to my question, but I figure I'll close this out with an official answer.

The piece of information I was missing is that

When bash is invoked as sh it behaves as a POSIX sh, not bash. – Jörg W Mittag Jan 23 at 23:31

So yes, pgrep was behaving normally. But when you call it from a Ruby script via backticks, you still need to filter out 'pgrep'

这篇关于关于pgrep返回自身的确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆