3

Trying to filter results of top command by Filtering in a Window, pressing o then typing filter, like described in a tutorial here: http://man7.org/linux/man-pages/man1/top.1.html

but when I typing for example COMMAND=iTerm2 or any other command I get invalid order error.

You can see animated gif with an issue here: https://i.stack.imgur.com/Qhhtl.jpg

  • OS: Mac OS Catalina version: 10.15.2

2 Answers 2

1

o isn't a filter on BSD top, it's a sort.

     o       Change the order in which the display is sorted.  The sort key
     names include cpu, res, size, time.  The default is cpu.

I'm not sure if there is a way to filter it the way you want.

So you could press o then type COMMAND but COMMAND=iTerm2 is invalid.

Alternatively you could run top with the -pid option to filter out a single pid but it's likely that iTerm2 is running more than one process.

top -pid $(pgrep iTerm2 | head -1)
3
  • I apologize but tutorial says total different things: ` o | O :Other-Filtering You will be prompted for the selection criteria which then determines which tasks will be shown in the current' window. Your criteria can be made case sensitive or case can be ignored. And you determine if top should include or exclude matching tasks.
    – Anatoly
    Jan 9, 2020 at 18:20
  • 1
    @Anatoly: it's not a tutorial, it's a man page. And it's a man page for software that you are not using. Since you are on macos you are using BSD top.
    – jesse_b
    Jan 9, 2020 at 18:21
  • Now it makes sense, can you please mention this in answer, so I will be able to accept it, please?
    – Anatoly
    Jan 9, 2020 at 18:24
0

On the Mac Terminal Zsh the top command has many differences with traditional linux implementations, as exemplified with the OP's question. I found a nice solution for filtering all iTerm2 processes:

top -pid $(pgrep -d " -pid " iTerm2)

The list of pids are returned with a -pid flag as a separator. The first -pid flag covers the bare initial pid.


Edit: I wanted to make it a little easier for myself to utilize this approach, so I created a zsh function topg. I welcome any feedback on my shell scripting. It is essentially a wrapper around top that adds the flags [-g|--grep]. I'll post updates as I refine it.

As for usage, you can either pass string of different process names separated by a space or append several flags together. Any other commands that top would respond to are passed through.

Example Usage:

topg -g ssh --grep firefox -g "WindowServer Gitify"

function topg () {
    emulate -L zsh
    zmodload zsh/zutil || return

    # Default option values can be specified as (value).
    local help filter_commands

    zparseopts -D -F -K -a -- \
        {h,-help}=help    \
        {g,-grep}+:=filter_commands || return

    if (( $#help )); then
        local top_help=`top -h`
        local lines
        lines=( ${(f)top_help} )
        lines[1]=${lines[1]:gs/top/tops}
        lines[3]=(${lines[3]}, "$(echo "\t\t")[-g <pattern> [<pattern2>] | --grep <pattern> [<pattern2>]]")

        print -rC1 -- \
        ${(F)lines}
        return
    fi

    if (( $#filter_commands )); then
        local -a array
        local flag vals

        for flag vals in "${(@)filter_commands}"; do
            array+=(${=vals})
        done

        eval "top -pid $(pgrep -d " -pid " "${(@)array}") "${@:1}""
        return
    else
        eval "top "${@:1}""
        return
    fi

}

Like I said, I welcome any feedback.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .