How can I find a specific process with “top” in a Mac terminal

前端 未结 8 1545
执笔经年
执笔经年 2021-02-01 02:18

I\'ve tried top | grep skype for example but it doesn\'t work. I\'m trying to find a specific process by name.

相关标签:
8条回答
  • 2021-02-01 02:41

    Now you can use pgrep skype to find the process.

    0 讨论(0)
  • 2021-02-01 02:42

    Use this instead: ps -ax | grep -i skype

    0 讨论(0)
  • 2021-02-01 02:47

    On Linux, the top command supports the -p option to monitor specific PIDs. On MacOS, the -p option is called -pid instead.

    # Get the PID of the process
    pgrep Skype
    
    # Then
    top -pid <put PID here>
    
    # Or more succinctly:
    top -pid `pgrep Skype`
    

    If you do this a lot, you could turn this into a function and add it to ~/.bash_profile:

    # Add this to ~/.bash_profile
    function topgrep() {
        if [[ $# -ne 1 ]]; then 
            echo "Usage: topgrep <expression>"
        else 
            top -pid `pgrep $1`
        fi
    }
    

    Now you can simply use topgrep Skype instead, which will run like usual but it will only show the process(es) matching expression.

    0 讨论(0)
  • 2021-02-01 02:48

    Tested on MacOSX Mojave. It works a bit different than linux.

    top -pid doesn't expect a comma separated list of pids, it expects only one pid. So I had to changed it a little to work with several pids.

    top -pid $(pgrep -d' -pid ' -f Python)
    

    filter all Python process on top. It essentially becomes something like this:

    top -pid 123 -pid 836 -pid 654
    
    0 讨论(0)
  • 2021-02-01 02:49

    Use: top -l 0 | grep Skype

    The 0 is for infinite samples. You can also limit the number of samples to a positive number.

    0 讨论(0)
  • 2021-02-01 02:57

    if you really love top, you could try:

    top -b -n 1 | grep skype
    

    e.g.

    kent$  top -b -n 1 |grep dropbox
     4039 kent      20   0  184m  14m 5464 S    0  0.4   0:58.30 dropbox
    
    0 讨论(0)
提交回复
热议问题