2

How do you use top with just showing the CMD name?

I have used top with just showing the running process that I want; for example:

$ top -p 19745

And if I want more than one PID, I would use:

$ top -p 19745 -p 19746 -p 19747

I have Googled it, but they don't say how you can do it, even when I try looking at the help in top it still doesn't show you.

Is there a way you can filter by the CMD name only?

There are certain files that I am running through Apache2, and I want to monitor them only.

afile1.abc
afile2.abc
afile3.abc
afile4.abc

Update

I see this in the man top page:

x: Command  --  Command line or Program name
      Display the command line used to start a task or the name of the associated
      program.   You toggle between command line and name with 'c', which is both
      a command-line option and an interactive command.

      When you've chosen to display command lines, processes  without  a  command
      line  (like  kernel  threads)  will  be shown with only the program name in
      parentheses, as in this example:
            ( mdrecoveryd )

      Either form of display is subject to potential truncation if it's too  long
      to fit in this field's current width.  That width depends upon other fields
      selected, their order and the current screen width.

      Note: The 'Command' field/column is unique, in that it is not  fixed-width.
      When  displayed,  this  column will be allocated all remaining screen width
      (up to the maximum 512 characters) to provide for the potential  growth  of
      program names into command lines.

Will that do anything for me?

Kevdog777
  • 3,224

2 Answers2

3

I believe you could do something like below as suggested here.

top -p `pgrep -d ',' "apache2"`

I believe the comma delimiter is not needed in this case as we have only one process.

top -p `pgrep "apache2"`
Ramesh
  • 39,297
  • Sorry, I edited my post, saying: "There is a certain file that I am running through Apache2, and I want to monitor it only." - I can sort them alphabetically by pressing c, but that doesn't really help... Well it does for me, as the filename starts with "A" – Kevdog777 Jul 30 '14 at 14:41
  • @Kevdog777, please see the updates. – Ramesh Jul 30 '14 at 14:42
  • Oh wait, it disappears when it refreshes itself. – Kevdog777 Jul 30 '14 at 14:45
  • @Kevdog777, I believe the comma delimiter is not needed in pgrep. Can you try the updated command? – Ramesh Jul 30 '14 at 14:56
  • I am getting this: top: unknown argument '1' – Kevdog777 Jul 30 '14 at 14:58
  • See I can do this: top -ppgrep -d ',' "afile*"`` but then shows the first 3, then disappears... When I try the other command, you gave, I get this: top: unknown argument '7' usage: top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...] – Kevdog777 Jul 30 '14 at 15:08
  • I am using Ubuntu 12.04.3 LTS, if that makes any difference? – Kevdog777 Jul 30 '14 at 15:10
  • It sounds like these processes come and go every few seconds and the OP would like to see them even when they are created after top has already started. @Kevdog777, is that correct? – Mark Plotnick Jul 30 '14 at 15:11
  • Well they are running all the time, but they might drop off every so often - but, yes that is correct. I really want to see them, even if they are sleeping. – Kevdog777 Jul 30 '14 at 15:12
  • It looks like the PID's are changing, so maybe it is what you say then @MarkPlotnick, cuz when the PID changes, it means it has died and started again, gaining a new PID. – Kevdog777 Jul 30 '14 at 15:19
3

If your top supports filtering, start top then type the following interactive command:

oCOMMAND=afile

The field name, COMMAND, must be all uppercase.

From the man page:

5e. FILTERING in a Window
   You can use the 'Other Filter' feature to establish selection cri‐
   teria which will then determine which tasks are shown in the ´cur‐
   rent´ window.

   Establishing a filter requires: 1) a field name; 2)  an  operator;
   and  3) a selection value, as a minimum.

   Filter Basics
      .  field names are case sensitive and spelled as in the header
      .  selection values need not comprise the full displayed field
      .  separate unique filters are maintained for each task window

Keyboard Summary
     o  :Other-Filter (lower case)
         You will be prompted to establish a filter that ignores case
         when matching.

     O  :Other-Filter (upper case)
         You will be prompted to establish a case sensitive filter.
Mark Plotnick
  • 25,413
  • 3
  • 64
  • 82
  • Should I use this command line: top oCOMMAND=afile? I tried that and got: top: unknown argument 'o' – Kevdog777 Jul 30 '14 at 15:36
  • 1
    The o command is an interactive command only, as far as I can tell. Start top first, then type it. – Mark Plotnick Jul 30 '14 at 15:37
  • Oops, I moved the X: COMMAND to the top of the filters list. It is now in the front of the list. But I can't filter it by only that one filename: afile – Kevdog777 Jul 30 '14 at 15:42
  • I have used top -i and have all the running processes, which is sufficient for me, as I have this machine on VirtualBox. – Kevdog777 Jul 30 '14 at 15:56