Limit the output of the TOP command to a specific process name

后端 未结 17 2507
臣服心动
臣服心动 2020-12-12 09:48

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

相关标签:
17条回答
  • 2020-12-12 09:55

    Use the watch command

    watch -d 'top -n1 | grep mysql'
    
    0 讨论(0)
  • 2020-12-12 09:55

    get pid of process:

    # pidof <process>
    

    tell top what process pid(s) to display

    # top -p <pid1>,<pid2>, etc
    

    example:

    landis@linux-e6510:~>pidof konsole
    1841 1709
    landis@linux-e6510:~>top -p 1841,1709
    

    Top will only display the 2 'konsole' processes. This is useful on a busy server via ssh, not having to pipe thru grep.

    0 讨论(0)
  • 2020-12-12 09:57

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

    0 讨论(0)
  • 2020-12-12 10:03

    I prefer the following so I can still use top interactively without having to look up the pids each time I run it:

    top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'`
    

    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 newlines
    • tr "\\n" "," translates these newlines into commas, because top wants a comma-separated list of process ids
    • sed is a stream editor, and sed 's/,$//' is used here to remove the trailing comma
    0 讨论(0)
  • how about top -b | grep java

    0 讨论(0)
  • 2020-12-12 10:05

    Expanding on @dogbane's answer, you can get all the PIDs for a named process with pgrep to do the following:

    top -p "$(pgrep -d ',' java)"
    
    0 讨论(0)
提交回复
热议问题