Search code examples
linuxbashtop-command

Linux Top command with more than 20 commands


I want to use top in order to monitor numerous processes by process name. I already know about doing $ top -p $(pgrep -d ',' <pattern>) but top only limits me to 20 pids. Is there a way to allow for more than 20 pids?

Do I have to use a combination of ps and watch to get similar results?


Solution

  • From top/top.c:

    if (Monpidsidx >= MONPIDMAX)
        error_exit(fmtmk(N_fmt(LIMIT_exceed_fmt), MONPIDMAX));
    

    (where LIMIT_exceed_fmt is the error message you're getting).

    And in top/top.h:

    #define MONPIDMAX  20
    

    I changed this number to 80, and that seems to work okay. Not sure why this hardcoded limit is so low.

    So, if manually compiling procps-ng is an option, then you could do that. You don't need to replace the system top (or need root privileges), you can just put it in your homedir.

    Another workaround might be using tmux or screen and multiple top instances.

    Yet another possible solution might be using ps with a loop, ie.

    #!/bin/sh
    
    while :; do
        clear
        ps $*
        sleep 1
    done
    

    Invoke it as: ./psloop.sh 42 666

    You may want to add more flags to ps for additional info. Also be aware this is less efficient, since it will invoke 3 binaries every second.