Limit the output of the TOP command to a specific

2019-01-20 22:04发布

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.

15条回答
等我变得足够好
2楼-- · 2019-01-20 22:34

Running the below will give continuous update in console:

bcsmc2rtese001 [~]$ echo $SHELL  
/bin/bash  
bcsmc2rtese001 [~]$ top | grep efare  or watch -d 'top | grep efare' or top -p pid
27728 efare     15   0 75184 3180 1124 S  0.3  0.0 728:28.93 tclsh  
27728 efare     15   0 75184 3180 1124 S  0.7  0.0 728:28.95 tclsh
查看更多
神经病院院长
3楼-- · 2019-01-20 22:35

Use the watch command

watch -d 'top -n1 | grep mysql'
查看更多
Ridiculous、
4楼-- · 2019-01-20 22:37

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.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-20 22:38

I came here looking for the answer to this on OSX. I ended up getting what I wanted with bash and awk:

topfiltered() {
  [[ -z "$1" ]] && return
  dump="/tmp/top_dump"
  rm -f "$dump"
  while :; do
    clear
    [[ -s "$dump" ]] && head -n $(( $LINES - 1 )) "$dump"
    top -l 1 -o cpu -ncols $(( $COLUMNS / 8 )) | awk -v p="$(pgrep -d ' ' $@)" '
        BEGIN { split(p, arr); for (k in arr) pids[arr[k]]=1 }
        NR<=12 || ($1 in pids)
    ' >"$dump"
  done
}

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.

查看更多
Melony?
6楼-- · 2019-01-20 22:40

just top -bn 1 | grep java will do the trick for you

查看更多
三岁会撩人
7楼-- · 2019-01-20 22:44

I run it (eg.): top -b | egrep -w 'java|mysqld'

查看更多
登录 后发表回答