严格名称的 Bash 通用“ps aux”进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16472509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash generic "ps aux" process by strict name
提问by trox
I'm trying to have a ps aux command listing only real ssh-agent process. I've noticed that on some distros, I have unwanted process showing up, like command colors and such. I need to do this because I need to ensure the real ssh-agent process is running in a script (don't bother, I already have a loop for it...).
我正在尝试使用 ps aux 命令仅列出真正的 ssh-agent 进程。我注意到在某些发行版上,出现了不需要的进程,例如命令颜色等。我需要这样做,因为我需要确保真正的 ssh-agent 进程在脚本中运行(别打扰,我已经有了一个循环......)。
So I figured out I need to use something like that in my test routine:
所以我想我需要在我的测试程序中使用类似的东西:
#!/bin/bash
ps aux | grep ssh-agent | grep -v grep | awk '{print }'
My question is: Would the awk $12 work on any unix/linux env using bash with any bash versions?
我的问题是:awk $12 是否可以在任何使用 bash 和任何 bash 版本的 unix/linux env 上工作?
Also, if I remove "grep -v grep" and do this:
另外,如果我删除“grep -v grep”并执行以下操作:
ps aux | grep ssh-agent | awk '{print }'
output is:
输出是:
ssh-agent
ssh-agent
Now:
现在:
ps aux | grep ssh-agent
output is:
输出是:
foo 18153478 0.0 0.0 536 844 - A Apr 25 0:00 ssh-agent
foo 31260886 0.0 0.0 252 264 pts/0 A 22:38:41 0:00 grep ssh-agent
That means, the space between "grep ssh-agent" is interpreted as a delimiter by the awk command. Aynthing possible to overcome that? I already tried using tab delimiter but that's not it. It seems the delimiter of the command are simple "space" characters. Is it possible to enforce tab delimiters for "ps" command output?
这意味着,“grep ssh-agent”之间的空格被 awk 命令解释为分隔符。Aynthing可能克服吗?我已经尝试过使用制表符分隔符,但事实并非如此。命令的分隔符似乎是简单的“空格”字符。是否可以为“ps”命令输出强制使用制表符分隔符?
Any idea?
任何的想法?
采纳答案by trox
OK I guess I found it, really easy and straightforward:
好吧,我想我找到了,非常简单明了:
ps -e -o pid,comm | grep ssh-agent
Works just fine.
工作得很好。
Answer found here: https://unix.stackexchange.com/questions/22892/how-do-use-awk-along-with-a-command-to-show-the-process-id-with-the-ps-command/22895#22895
在这里找到答案:https: //unix.stackexchange.com/questions/22892/how-do-use-awk-along-with-a-command-to-show-the-process-id-with-the-ps-命令/22895#22895
And adapted with a | grep ssh-agent
并与 | grep ssh-agent
Also suggested by Martin. Thank you all for sharing your experience!
也是马丁建议的。谢谢大家分享你的经验!
回答by Ahmed Masud
First $12 is tied to how many fields ps outputs. It has nothing to with bash.
第一个 $12 与 ps 输出的字段数相关。它与 bash 无关。
grep -v grepis a good way to remove the grep process so leave that in;
grep -v grep是删除 grep 进程的好方法,因此请保留它;
Now you are not certain if the last field is field 12 or what, I'm not quite certain
现在你不确定最后一个字段是字段 12 还是什么,我不太确定
ps aux | grep ssh-agent | grep -v grep | awk '{ print $NF }'
assuming you are looking to only see ssh-agent (and it has no command-line parameters)
假设您只想查看 ssh-agent(并且它没有命令行参数)
Here is something quick and dirty that will spit out the pid and the complete command-line
这是一些快速而肮脏的东西,它会吐出 pid 和完整的命令行
ps aux | grep ssh-agent | grep -v grep | awk '{ print " "; for (k = 12; k < NF; k++ ) { printf "%s", k; } printf "\n"; }'
NF is the number of fields in awk so $NF is the last field.
NF 是 awk 中的字段数,所以 $NF 是最后一个字段。
see if this works for you. There are much cleaner ways of doing this though
看看这是否适合你。有很多更简洁的方法可以做到这一点
回答by Alfe
I propose to grepfor sth like '[s]sh-agent'. This way you prevent getting the grepitself.
我提议grep为某事'[s]sh-agent'。这样你就可以防止得到它grep本身。
I also propose to not use awk(to print the 12th column) but to use cutto print a specific character range of the lines, e. g.:
我还建议不使用awk(打印第 12 列)而是使用cut打印行的特定字符范围,例如:
ps aux | grep '[s]sh-agent' | cut -c 66-
This is depending on the output format of psof course.
这ps当然取决于输出格式。
You could have a look at the /proc/file system instead:
您可以查看/proc/文件系统:
(
cd /proc
for p in [0-9]*
do
[ "$(readlink $p/exe)" = "/bin/bash" ] &&
echo "$p $(cat $p/cmdline | tr '##代码##' ' ')"
done
)
(Use sth more appropriate instead of /bin/bashof course.)
(使用 sth 更合适,而不是/bin/bashcourse。)
This on the other hand will depend on the existence of the /proc/file system. Decide for yourself which is better for you.
另一方面,这将取决于/proc/文件系统的存在。自己决定哪个更适合你。

