将 TOP 命令的输出限制为特定进程名称
如果调用 top 命令,您将获得所有正在运行的进程。但是我如何限制仅将输出限制为某个进程名称,例如“java”?
我试过这个 顶部 -l 2 | grep java 但通过这种方式,您只能获得快照,而不能获得持续更新的列表。和top -l 0 | grep java 不太清楚。
If you call the top command, you get all the running processes. But how can I limit the output only to a certain process name like "java"?
I've tried this
top -l 2 | grep java
but in this way you get only snapshots and not a continuously updated list. And top -l 0 | grep java is not really clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
使用观看命令
Use the watch command
我使用以下方法解决了我的问题:
在这种情况下:
-n 用于设置top将执行多少次进程
-b 用于显示所有 pid,
它可以防止出现以下错误:
顶部:超过 pid 限制 (20)
I solved my problem using:
in this case:
-n is used to set how many times top will what proccess
and -b is used to show all pids
it's prevents errors like :
top: pid limit (20) exceeded
以下代码通过 watch 命令每 5 秒更新一次进程列表:
watch -n 5 -t top -b -n 1 -p$(pgrep java | head -20 | tr "\\n" ", " | sed 's/,$//')
The following code updates a list of processes every 5 seconds via the watch command:
watch -n 5 -t top -b -n 1 -p$(pgrep java | head -20 | tr "\\n" "," | sed 's/,$//')
我运行它(例如):
top -b | egrep -w 'java|mysqld'
I run it (eg.):
top -b | egrep -w 'java|mysqld'
假设..如果我们有超过20个同名进程在服务器上运行...这将
无济于事头-n 20 | tr“\\n”“,”| sed 's/,$//'
它将尝试列出并提供 20 个进程的实时输出,其中我们很有可能错过其他消耗更多资源的进程......
我仍在寻找更好的选择在此
Suppose .. if we have more than 20 process running on the server with the same name ... this will not help
top -p
pgrep oracle | head -n 20 | tr "\\n" "," | sed 's/,$//'
It will try to list and provide real time output of 20 process where we have good chance of missing other prcesses which consumes more resource ....
I am still looking for better option on this
只是
top -bn 1 | grep java
将为你解决问题just
top -bn 1 | grep java
will do the trick for you您需要将
pgrep {proc_name}
的输出提供给top -pid {pid_No}
。top -pid
的问题是它期望在您想要监控的每个 pid 之前都有-pid
。例如:
在 Mac 上的 zsh 中,我可以处理这个问题, awk '{printf " -pid %d",$1}'`
这也不理想,因为
pgrep
会查找进程名称中的子字符串。例如,pgrep dd
可以返回icdd、hidd、cloudd
等的结果。该选项pgrep -x
应该仅返回精确匹配(如grep -w
)。但它在 Mac 终端中不起作用,尽管它在 Ubuntu 虚拟机中起作用。You need to feed the output of
pgrep {proc_name}
totop -pid {pid_No}
. The problem withtop -pid
is that it expects-pid
before each pid you want to monitor.On Mac in zsh I can handle this problem e.g. like that:
top `pgrep proc_name | awk '{printf " -pid %d",$1}'`
It's not ideal too, because
pgrep
looks for substrings in process names. E.g.pgrep dd
can return results foricdd, hidd, cloudd
etc. The optionpgrep -x
should return the exact matches only (likegrep -w
). But it doesn't work for me in Mac Terminal, although it does in Ubuntu virtual machine.一个更具体的案例,就像我实际上在寻找的那样:
对于Java进程,您还可以使用
jps -q
,其中jps是< em>$JAVA_HOME/bin 因此应该位于您的 $PATH 中。A more specific case, like I actually was looking for:
For Java processes you can also use
jps -q
whereby jps is a tool from $JAVA_HOME/bin and hence should be in your $PATH.我来这里是为了在 OSX 上寻找这个问题的答案。我最终用 bash 和 awk 得到了我想要的东西:
我在日志记录模式下循环 top 并用 awk 过滤它,从 pgrep 的输出构建一个关联数组。 awk 打印前 12 行,其中第 12 行是列标题,然后打印具有 pid(数组中的键)的每一行。转储文件用于更易于观察的循环。
I came here looking for the answer to this on OSX. I ended up getting what I wanted with bash and awk:
I loop top in logging mode and filter it with awk, building an associative array from the output of pgrep. Awk prints the first 12 lines, where line 12 is the column headers, and then every line which has a pid that's a key in the array. The dump file is used for a more watchable loop.
运行以下命令将在控制台中进行持续更新:
Running the below will give continuous update in console:
这是迄今为止 MacOS 的唯一解决方案:
尽管如果没有活动的
java
进程,这会报告无效的选项或语法:-pid
。说明
此处发布的其他解决方案使用格式
top -p id1,id2,id3
,但MacOS的top
仅支持笨重的格式顶部-pid id1-pid id2-pid id3
。因此,首先,我们获取进程名称为“java”的进程 ID 列表:
使用分隔符
" -pid "
连接结果并将其通过管道传输到
awk
,后者 这会留下一个尾随分隔符!例如,到目前为止我们可能已经获得了字符串“123 -pid 456 -pid 789 -pid”
。然后,我们只需使用 sed 来删除分隔符的最后 6 个字符。
我们准备将结果传递给
top
:Here's the only solution so far for MacOS:
though this will undesirably report
invalid option or syntax: -pid
if there are nojava
processes alive.EXPLANATION
The other solutions posted here use the format
top -p id1,id2,id3
, but MacOS'top
only supports the unwieldy formattop -pid id1 -pid id2 -pid id3
.So firstly, we obtain the list of process ids which have process name "java":
and we pipe this to
awk
which joins the results with delimitor" -pid "
Alas, this leaves a trailing delimitor! For example, we may so far have obtained the string
"123 -pid 456 -pid 789 -pid "
.We then just use
sed
to shave off the final 6 characters of the delimitor.We're ready to pass the results to
top
:获取进程的 pid:
告诉 top 要显示的进程 pid
示例:
Top 将仅显示 2 个“konsole”进程。
这在通过 ssh 的繁忙服务器上非常有用,而不必通过 grep 进行管道传输。
get pid of process:
tell top what process pid(s) to display
example:
Top will only display the 2 'konsole' processes.
This is useful on a busy server via ssh, not having to pipe thru grep.
使用 Rick Byers 的答案中提到的方法:
但我有超过 20 个进程正在运行,因此以下命令对于遇到类似情况的人可能会有所帮助。
pgrep
获取具有给定名称的进程列表 - 在本例中为 java。head
用于获取前 20 个 pid,因为使用 -p 参数时 top 无法处理超过 20 个 pid。最后paste
用','连接pid列表。您可以在上述命令中控制要查找的进程名称以及您有兴趣观看的具有该名称的进程数量。如果具有给定名称的进程数量少于 20,您可以忽略
head -n 20
部分。Using the approach mentioned in the answer by Rick Byers:
but I had more than 20 processes running so following command can be helpful for someone who encounter a similar situation.
pgrep
gets the list of processes with given name - java in this case.head
is used to get first 20 pids because top cannot handle more than 20 pids when using -p argument. Finallypaste
joins the list of pids with ','.You can control the process name you are looking for in the above command and the number of processes with that name you are interested to watch. You can ignore the
head -n 20
part if the number of your processes with the given name is less than 20.我更喜欢以下内容,这样我仍然可以交互地使用 top,而不必每次运行它时查找 pid:
当然,如果进程发生变化,您将必须重新运行该命令。
说明:
pgrep process-name
返回由换行符分隔的进程 id 列表tr "\\n" ","
将这些换行符转换为逗号,因为 top 想要一个以逗号分隔的进程 ID 列表sed
是一个流编辑器,sed 's/,$//'
用于删除结尾的逗号I prefer the following so I can still use top interactively without having to look up the pids each time I run it:
Of course if the processes change you'll have to re-run the command.
Explanation:
pgrep process-name
returns a list of process ids which are separated by newlinestr "\\n" ","
translates these newlines into commas, because top wants a comma-separated list of process idssed
is a stream editor, andsed 's/,$//'
is used here to remove the trailing comma找到要监视的进程的 pid,然后使用 -p 选项,该选项允许您向 top 命令提供 pid 列表。
示例:(
我相信您也可以传入逗号分隔的列表。)
Find the pids of the processes you want to monitor and then use the
-p
option which allows you to provide a list of pids to thetop
command.Example:
(I believe you can also pass in a comma-separated list.)
怎么样 top -b | grep java
how about top -b | grep java
扩展 @dogbane 的答案,您可以使用
pgrep
获取命名进程的所有 PID执行以下操作:Expanding on @dogbane's answer, you can get all the PIDs for a named process with
pgrep
to do the following:使用此处的答案我能够创建a one liner
这对我在 MacOS 10.12 (Sierra) 上有效
Using the answer from here I was able to create a one liner
This works for me on MacOS 10.12 (Sierra)