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

前端 未结 8 1720
执笔经年
执笔经年 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: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 
    
    # 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 "
        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.

提交回复
热议问题