7

The following command is useful to capture the current processes that are taking up the most CPU and store to a file:

top -c -b -n 1 > top.log

The -c flag is particularly useful because it gives you the command line arguments of each process rather than just the process name.

Without -c:

2497 root      18   0 11264 5888 1524 S  0.0  0.1   0:03.31 miniserv.pl

With -c (additional useful command info):

2497 root      18   0 11264 5888 1524 S  0.0  0.1   0:03.31 /usr/bin/perl /usr/libexec/webmin/miniserv.pl /etc/webmin/miniserv.conf

The problem is that each line of output is truncated to fit on the current terminal window. This is ok if you can have a wide terminal because you have a lot of the output but if your terminal is only 165 characters wide, you only get 165 characters of information per process and it is often not enough characters to show the full process command. This is a particular problem when the command is executed without a terminal, for example if you do it via a cron job.

Does anyone know how to stop top truncating data or force top to display a certain number of characters per line?

This is not urgent because there is an alternative method of getting the top 10 processes ordered by CPU usage:

ps -eo pcpu,pmem,user,args | sort -r -k1 | head -n 10

2 Answers 2

6

You can set COLUMNS environment variable to a large value. For example, to set it just for top invocation:

$ COLUMNS=1000 top -c -b -n 1 > top.log

At least it works in bash 4.0.33 on Ubuntu 9.10.

1
  • Not sure if it worked in earlier versions. But as per man top 512 columns is the max. Oct 24, 2023 at 10:09
2

Yet a better solution, tell top to use a width, regarless of terminal.

top -w 512 -cbn 1 where -w is the wdith that can be used.

From man top:

-w :Output-width-override as: -w [ number ]
In Batch mode, when used without an argument top will format output using the COLUMNS= and LINES= environment variables, if set. Otherwise, width will be fixed at the maximum 512 columns. With an argument, output width can be decreased or increased (up to 512) but the number of rows is considered unlimited.

You must log in to answer this question.

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