6

I am logging the output of: top -b -n1 -c so I can keep track of which processes are using how much memory and CPU each minute. When top shows the full command line (-c), the command line text gets truncated at the edge of the screen. This is perfect when running and displaying in a terminal. However, I am sending STDOUT to a file for parsing later and want the full command name logged. Is there a way I can tell top to use a specific screen width? (I know it maxes at 512, that would be fine)

Is there a better way to achieve a similar goal: get periodic stats about which processes are running and how many resources they are consuming?

2 Answers 2

6

This seems to work:

COLUMNS=512 top -b -n1 -c

That appears to pad the lines with spaces to 512 columns, though, at least when the output is piped to another program, so you may want to filter the output like this:

COLUMNS=512 top -b -n1 -c | sed 's/  *$//'

where that asterisk is preceded by two space characters.

2

I guess top wasn't really intended to be used this way and you should use ps instead.

$ ps axv
PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
   1 ?        Ss     0:07    314    31  3908    24  0.0 init [3]  
   2 ?        S      0:00      0     0     0     0  0.0 [kthreadd]
   3 ?        S      0:06      0     0     0     0  0.0 [ksoftirqd/0]
   4 ?        S      0:12      0     0     0     0  0.0 [migration/0]
   5 ?        S      0:10      0     0     0     0  0.0 [migration/1]
   6 ?        S      0:05      0     0     0     0  0.0 [ksoftirqd/1]
....

See man 1 ps for the long list of options.

2
  • This may be a smart direction to head in the future. Thanks
    – Zach Dwiel
    Oct 14, 2010 at 18:26
  • The advantage of top is that it does show other data related to overall CPU load. I'm using this to log what else is taking up CPU time when a slow request is detected on my web server: I care about the load at that instant, and ps doesn't show that very well. Sep 5, 2012 at 2:14

You must log in to answer this question.

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