Linux / Bash,使用 ps -o 按特定名称获取进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3790895/
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
Linux / Bash, using ps -o to get process by specific name?
提问by Rick
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 -o 命令来获取有关匹配某个名称的进程的特定信息。但是,我在这方面遇到了一些问题,当我尝试使用它甚至只是获取所有进程时,就像这样,它只返回正常返回的子集ps -ef
(它返回的结果数量几乎相同,因此它没有返回所有正在运行的进程)
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 -o 以从中获取特定信息(仅 PID)
ps -ef |grep `whoami`| grep firefox-bin
Any advice is appreciated as to how to do this properly, thanks
感谢您提供有关如何正确执行此操作的任何建议,谢谢
采纳答案by Alex Howansky
This will get you the PID of a process by name:
这将按名称为您提供进程的PID:
pidof name
Which you can then plug back in to ps for more detail:
然后您可以将其重新插入 ps 以获取更多详细信息:
ps -p $(pidof name)
回答by h7r
This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:
这有点旧,但我想你想要的是:ps -o pid -C PROCESS_NAME,例如:
ps -o pid -C bash
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:
编辑:取决于您期望的输出类型,pgrep
会更优雅。据我所知,这是 Linux 特定的,并导致与上述类似的输出。例如:
pgrep bash
回答by Gerald Hughes
ps -fC PROCESSNAME
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.
ps 和 grep 是一个危险的组合——grep 试图匹配每一行上的所有内容(因此太常见了:grep -v grep hack)。ps -C 不使用 grep,它使用进程表进行精确匹配。因此,您将获得一个准确的列表: ps -fC sh 而不是找到每个带有 sh 的进程就行。
回答by user3751385
Sometimes you need to grep the process by name - in that case:
有时您需要按名称 grep 进程 - 在这种情况下:
ps aux | grep simple-scan
Example output:
示例输出:
simple-scan 1090 0.0 0.1 4248 1432 ? S Jun11 0:00
回答by Spade
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
pgrep -x PROCESS_NAME
-x Require an exact match of the process name, or argument list if -f is given. The default is to match any substring.
-x Require an exact match of the process name, or argument list if -f is given. The default is to match any substring.
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.
如果您的原始进程创建了子进程(查询时可能是僵死的),这些子进程以它们自己的名称作为原始进程名称的前缀,并且您试图将它们从结果中排除,这将非常有用。有许多 UNIX 守护进程可以做到这一点。我的首选示例是ninja-dev-sync。