3

I am running top command and the result is as below.

PID   USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
12446 root      20   0 9869844 6.538g 381348 S 181.2 56.1   2136:08 myps
5019 root      39  19       0      0      0 S   1.3  0.0 426:58.00 kipmi0

I would like to see only myps ,so i used grep as below

top | grep myps

The result looks below

12446 root      20   0 9869844 6.538g 381348 S 181.2 56.1   2136:08 myps

I need header(title ) also need to be displayed. What is the option to do it?

Update: Following answer from the source question worked for me

top -p "$(pgrep -d ',' myps)"
Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230

2 Answers2

5

You can use top’s filtering to focus on a few processes.

$ top

Then type 'o/O', a prompt will appear inside top interface. Here, you can write filter expressions for example :

COMMAND=myps        //to get processes containing 'myps' in COMMAND attribute
!COMMAND=myps       //to get processes which do not contain 'myps' in COMMAND attribute
Tarun Kumar
  • 201
  • 1
  • 4
  • 1
    For resetting the filter, press `=` and then type your new filter with `o` again; Otherwise every time you press `o` the current filter would apply to the results of the previous filter (leads to nested filters) – DummyBeginner Apr 17 '21 at 06:27
0

You can use extended grep to get the header line as well something like:

top | grep -E '(PID|myps)'

You could also bypass top and use watch like:

watch -n 1 bash -c "ps aux | grep -E '([P]ID|[m]yps)'"
tonyb
  • 61
  • 2