Limit the output of the TOP command to a specific process name

后端 未结 17 2509
臣服心动
臣服心动 2020-12-12 09:48

If you call the top command, you get all the running processes. But how can I limit the output only to a certain process name like \"java\"?

I\'ve

相关标签:
17条回答
  • 2020-12-12 10:15

    Running the below will give continuous update in console:

    bcsmc2rtese001 [~]$ echo $SHELL  
    /bin/bash  
    bcsmc2rtese001 [~]$ top | grep efare  or watch -d 'top | grep efare' or top -p pid
    27728 efare     15   0 75184 3180 1124 S  0.3  0.0 728:28.93 tclsh  
    27728 efare     15   0 75184 3180 1124 S  0.7  0.0 728:28.95 tclsh
    
    0 讨论(0)
  • 2020-12-12 10:15

    Here's the only solution so far for MacOS:

    top -pid `pgrep java | awk 'ORS=" -pid "' | sed 's/.\{6\}$//'`
    

    though this will undesirably report invalid option or syntax: -pid if there are no java processes alive.

    EXPLANATION

    The other solutions posted here use the format top -p id1,id2,id3, but MacOS' top only supports the unwieldy format top -pid id1 -pid id2 -pid id3.

    So firstly, we obtain the list of process ids which have process name "java":

    pgrep java
    

    and we pipe this to awk which joins the results with delimitor " -pid "

    | awk 'ORS=" -pid "'
    

    Alas, this leaves a trailing delimitor! For example, we may so far have obtained the string "123 -pid 456 -pid 789 -pid ".

    We then just use sed to shave off the final 6 characters of the delimitor.

    | sed 's/.\{6\}$//'`
    

    We're ready to pass the results to top:

    top -pid `...`
    
    0 讨论(0)
  • 2020-12-12 10:18

    Suppose .. if we have more than 20 process running on the server with the same name ... this will not help

    top -p pgrep oracle | head -n 20 | tr "\\n" "," | sed 's/,$//'

    It will try to list and provide real time output of 20 process where we have good chance of missing other prcesses which consumes more resource ....

    I am still looking for better option on this

    0 讨论(0)
  • 2020-12-12 10:20

    The following code updates a list of processes every 5 seconds via the watch command:

    watch -n 5 -t top -b -n 1 -p$(pgrep java | head -20 | tr "\\n" "," | sed 's/,$//')

    0 讨论(0)
  • 2020-12-12 10:21

    Using the approach mentioned in the answer by Rick Byers:

    top -p `pgrep java | paste -sd "," -`
    

    but I had more than 20 processes running so following command can be helpful for someone who encounter a similar situation.

    top -p `pgrep java | head -n 20 | paste -sd "," -`
    

    pgrep gets the list of processes with given name - java in this case. head is used to get first 20 pids because top cannot handle more than 20 pids when using -p argument. Finally paste joins the list of pids with ','.

    You can control the process name you are looking for in the above command and the number of processes with that name you are interested to watch. You can ignore the head -n 20 part if the number of your processes with the given name is less than 20.

    0 讨论(0)
提交回复
热议问题