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

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

问题描述

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

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.

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

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

I have a Ruby script which calls pgrep like this:

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

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

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

现在,由于一些无关的愚蠢,我几乎立即对返回的每个 pid 执行了 ps -p.有一段时间,这让我感到非常悲伤,因为 ps 有时什么也不返回.最终我能够发现 pid 上的 ps 返回 pgrep 命令的情况.但它是 pgrep 命令本身,而不是像 sh -c "p​​grep -f blah"

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"

总结:

pgrep 永远不会返回自身.但是 sh 与 bash 的差异会导致它显示一个子shell.但是我验证了 sh 是指向 bash 的链接,因此行为上应该没有区别.

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.

我怀疑(并且正在寻找确认)是由于 Ruby 反引号正在创建一个额外的子命令,这就是 pgrep 命令接收的(仅有时.. 时间问题?).

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. 附加一个<代码>|grep -v grep 到我的命令末尾
  2. 在 Ruby 脚本中循环返回结果时,抛出任何包含grep"的结果

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

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?

感谢您的时间!

推荐答案

答案已经在我的问题的评论中,但我想我会用一个正式的答案来结束这个问题.

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

我遗漏的信息是

当 bash 被调用为 sh 时,它表现为 POSIX sh,而不是 bash.– Jörg W Mittag 1 月 23 日 23:31

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

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

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天全站免登陆