linux中的top -c命令根据进程名过滤列出的进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12075591/
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
top -c command in linux to filter processes listed based on processname
提问by
top -c
Top lists all the processes, there are good options to filter the processes by username by using the option -u but I am wondering if there is any easy way to filter the processes based on processname listed under COMMAND column of the top output.
Top 列出了所有进程,有一些很好的选项可以使用选项 -u 按用户名过滤进程,但我想知道是否有任何简单的方法可以根据顶部输出的 COMMAND 列下列出的进程名过滤进程。
For Example I would want like top -some option -substring of processname and top displays pids only having this substring in its command name
例如,我想要 top -some option -substring of processname 和 top 显示仅在其命令名称中包含此子字符串的pid
采纳答案by perreal
Using pgrep to get pid's of matching command lines:
使用 pgrep 获取匹配命令行的 pid:
top -c -p $(pgrep -d',' -f string_to_match_in_cmd_line)
top -p
expects a comma separated list of pids so we use -d','
in pgrep. The -f
flag in pgrep makes it match the command line instead of program name.
top-p
需要一个逗号分隔的 pid 列表,所以我们-d','
在 pgrep 中使用。-f
pgrep 中的标志使其匹配命令行而不是程序名称。
回答by Satish
what about this?
那这个呢?
top -c -p <PID>
回答by BBW Before Windows
@perreal's command works great! If you forget, try in two steps...
@perreal 的命令效果很好!如果您忘记了,请分两步尝试...
example: filter top to display only application called yakuake:
示例:过滤顶部以仅显示名为 yakuake 的应用程序:
$ pgrep yakuake
1755
$ top -p 1755
useful top interactive commands 'c' : toggle full path vs. command name 'k' : kill by PID 'F' : filter by... select with arrows... then press 's' to set the sort
有用的顶级交互式命令 'c' :切换完整路径与命令名称 'k' :按 PID 杀死 'F' :过滤... 用箭头选择...然后按 's' 设置排序
the answer below is good too... I was looking for that today but couldn't find it. Thanks
下面的答案也很好......我今天正在寻找,但找不到。谢谢
回答by nos
It can be done interactively
它可以交互式地完成
After running top -c
, hit oand write a filter on a column, e.g. to show rows where COMMAND column contains the string foo, write COMMAND=foo
运行后top -c
,点击o并在列上写一个过滤器,例如显示 COMMAND 列包含字符串 foo 的行,写COMMAND=foo
If you just want some basic output this might be enough:
如果您只想要一些基本输出,这可能就足够了:
top -bc |grep name_of_process
回答by Val
After looking for so many answers on StackOverflow, I haven't seen an answer to fit my needs.
在 StackOverflow 上寻找了这么多答案后,我还没有看到满足我需求的答案。
That is, to make top command to keep refreshing with given keyword, and we don't have to CTRL+C / top again and again when new processes spawn.
也就是说,要使 top 命令以给定的关键字保持刷新,并且我们不必在新进程产生时一次又一次地 CTRL+C / top 。
Thus I make a new one...
所以我做了一个新的......
Here goes the no-restart-needed version.
这是不需要重启的版本。
__keyword=name_of_process; (while :; do __arg=$(pgrep -d',' -f $__keyword); if [ -z "$__arg" ]; then top -u 65536 -n 1; else top -c -n 1 -p $__arg; fi; sleep 1; done;)
Modify the __keyword and it should works. (Ubuntu 2.6.38 tested)
修改 __keyword,它应该可以工作。(Ubuntu 2.6.38 测试)
2.14.2015 added: The system workload part is missing with the code above. For people who cares about the "load average" part:
2.14.2015 添加:系统工作负载部分与上述代码缺失。对于关心“平均负载”部分的人:
__keyword=name_of_process; (while :; do __arg=$(pgrep -d',' -f $__keyword); if [ -z "$__arg" ]; then top -u 65536 -n 1; else top -c -n 1 -p $__arg; fi; uptime; sleep 1; done;)
回答by Jester
I ended up using a shell script with the following code:
我最终使用了带有以下代码的 shell 脚本:
#!/bin/bash
while [ 1 == 1 ]
do
clear
ps auxf |grep -ve "grep" |grep -E "MSG[^\ ]*" --color=auto
sleep 5
done
回答by Don Kirkby
You can add filters to top
while it is running, just press the okey and then type in a filter expression. For example, to monitor all java processes use the filter expression COMMAND=java
. You can add multiple filters by pressing the key again, you can filter by user with the ukey, and you can clear all filters with the =key.
您可以在top
运行时添加过滤器,只需按o键,然后输入过滤器表达式即可。例如,要监视所有 java 进程,请使用过滤器表达式COMMAND=java
。再次按 键可以添加多个过滤器,可以用u键按用户过滤,用键可以清除所有过滤器=。
回答by Manwe
Most of the answers fail here, when process list exceeds 20 processes. That is top -p
option limit.
For those with older top that does not support filtering with o
options, here is a scriptable example to get full screen/console outuput (summary information is missing from this output).
当进程列表超过 20 个进程时,大多数答案都失败了。那是top -p
期权限制。对于那些不支持使用o
选项过滤的较旧顶部的用户,这里有一个可编写脚本的示例来获取全屏/控制台输出(此输出中缺少摘要信息)。
__keyword="YOUR_FILTER" ; ( FILL=""; for i in $( seq 1 $(stty size|cut -f1 -d" ")); do FILL=$'\n'$FILL; done ; while :; do HSIZE=$(( $(stty size|cut -f1 -d" ") - 1 )); (top -bcn1 | grep "$__keyword"; echo "$FILL" )|head -n$HSIZE; sleep 1;done )
Some explanations
一些解释
__keyword = your grep filter keyword
HSIZE=console height
FILL=new lines to fill the screen if list is shorter than console height
top -bcn1 = batch, full commandline, repeat once
回答by serv-inc
In htop
, you can simply search with
在 中htop
,您可以简单地搜索
/process-name