简体   繁体   English

如何在 Mac 终端中找到带有“top”的特定进程

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

I've tried top | grep skype我试过top | grep skype top | grep skype for example but it doesn't work.例如, top | grep skype但它不起作用。 I'm trying to find a specific process by name.我正在尝试按名称查找特定进程。

Use this instead: ps -ax | grep -i skype改用这个: ps -ax | grep -i skype ps -ax | grep -i skype

Use: top -l 0 | grep Skype使用: top -l 0 | grep Skype top -l 0 | grep Skype

The 0 is for infinite samples. 0 代表无限样本。 You can also limit the number of samples to a positive number.您还可以将样本数限制为正数。

On Linux, the top command supports the -p option to monitor specific PIDs.在 Linux 上, top命令支持-p选项来监视特定的 PID。 On MacOS, the -p option is called -pid instead.在 MacOS 上, -p选项被称为-pid

# 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 :如果你经常这样做,你可以把它变成一个函数并将它添加到~/.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 .现在您可以简单地改用topgrep Skype ,它会像往常一样运行,但只会显示进程匹配expression

if you really love top, you could try:如果你真的喜欢顶级,你可以试试:

top -b -n 1 | grep skype

eg例如

kent$  top -b -n 1 |grep dropbox
 4039 kent      20   0  184m  14m 5464 S    0  0.4   0:58.30 dropbox

使用ps而不是 top。

现在您可以使用pgrep skype来查找进程。

I would recommend using ps -ax | less我建议使用ps -ax | less ps -ax | less

From within less , you can type in / skype Enter to search for processes with names containing "skype".less ,您可以输入/ skype Enter以搜索名称包含“skype”的进程。

Tested on MacOSX Mojave.在 MacOSX Mojave 上测试。 It works a bit different than linux.它的工作原理与 linux 有点不同。

top -pid doesn't expect a comma separated list of pids, it expects only one pid. top -pid不需要逗号分隔的 pid 列表,它只需要一个 pid。 So I had to changed it a little to work with several pids.所以我不得不稍微改变它以使用几个pid。

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

filter all Python process on top.在顶部过滤所有 Python 进程。 It essentially becomes something like this:它基本上变成了这样:

top -pid 123 -pid 836 -pid 654

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM