Linux / Bash,使用 ps -o 按特定名称获取进程?
我正在尝试使用 ps -o 命令来获取有关与某个名称匹配的进程的特定信息。但是,我在这方面遇到了一些问题,当我尝试使用它来获取所有进程时,就像这样,它只返回正常 ps -ef
将返回的子集(它不不会返回几乎相同数量的结果,因此它不会返回所有正在运行的进程)
ps -ef -o pid,time,comm
我想尝试类似的操作(如下),但合并 ps -o 以便从中获取特定信息(只是 PID)
ps -ef |grep `whoami`| grep firefox-bin
任何建议都值得赞赏如何正确执行此操作,谢谢
I am trying to use the ps -o command to get just specific info about processes matching a certain name. However, I am having some issues on this, when I try to use this even to just get all processes, like so, it just returns a subset of what a normal ps -ef
would return (it doesn't return nearly the same number of results so its not returning all running processes)
ps -ef -o pid,time,comm
I want to try something like this (below) but incorporate the ps -o to just get specific info from it (just the PID)
ps -ef |grep `whoami`| grep firefox-bin
Any advice is appreciated as to how to do this properly, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将通过名称获取进程的 PID:
然后您可以将其插回 ps 以获取更多详细信息:
This will get you the PID of a process by name:
Which you can then plug back in to ps for more detail:
这有点旧,但我想您想要的是: ps -o pid -C PROCESS_NAME,例如:
编辑:根据您期望的输出类型,
pgrep
会更优雅。据我所知,这是 Linux 特定的,并且会产生与上面类似的输出。例如:This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:
EDIT: Dependening on the sort of output you expect,
pgrep
would be more elegant. This, in my knowledge, is Linux specific and result in similar output as above. For example:ps 和 grep 是一个危险的组合——grep 尝试匹配每行上的所有内容(因此非常常见:grep -v grep hack)。 ps -C 不使用 grep,它使用进程表进行精确匹配。因此,您将获得一个准确的列表: ps -fC sh 而不是在线路上的某处使用 sh 查找每个进程。
ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.
有时您需要按名称 grep 进程 - 在这种情况下:
示例输出:
Sometimes you need to grep the process by name - in that case:
Example output:
抱歉,晚了,但我会在这里补充一点,如果您想捕获名称与搜索字符串相同的进程,您可以执行
pgrep -x PROCESS_NAME
如果您的原始进程创建了子进程(查询时可能是僵尸进程),这些子进程在其自己的名称中为原始进程的名称添加前缀,并且您试图将它们从结果中排除,那么这非常有用。有许多 UNIX 守护程序可以执行此操作。我的首选示例是ninja-dev-sync。
Sorry, much late to the party, but I'll add here that if you wanted to capture processes with names identical to your search string, you could do
pgrep -x PROCESS_NAME
This is extremely useful if your original process created child processes (possibly zombie when you query) which prefix the original process' name in their own name and you are trying to exclude them from your results. There are many UNIX daemons which do this. My go-to example is ninja-dev-sync.