52

A former coworker did something to top that whenever it runs as root the data is sorted by MEM usage instead of the default CPU usage. According to multiple searches, the man page and even the options within the top console itself (O), just pressing k it should be sorted by CPU, but instead when I hit k it asks me for a pid to kill.

So how can I get back default sorting to CPU?

  • 1
    @programmer5000 on MacOS, yes. It's not portable though. I know Red Hat rejects it. – M. Davis Jan 9 '18 at 14:18
38

You can change the sort field in the interactive top window with the < and > keys. I'm not sure what operating system you're running but at least on my GNU top, k is supposed to kill, not reset.

Presumably, your friend changed the sort field and hit Shift+W to save to ~/.toprc. Just use the keys I mentioned to choose the sort field you want and then, when it's set up as you like it, hit Shift+W again and it should save that state and open that way next time.

  • 4
    Note that you can press z to turn on color mode and then x to highlight the current sort column. Much easier to see what's going on as you use < and > to change what you're sorting by. – Mark Reed May 10 '18 at 14:35
  • 2
    Actually, pressing x will highlight the current sort column even in non-color mode. Pressing b will toggle bold to highlight it even more. – Stephan Henningsen Dec 19 '18 at 12:09
60

To add to the answers already P (upper case P) makes top order by CPU, Then hit W (again upper case W) to save.

M goes back to memory

  • Oddly enough this option does not appear to be featured in the man top available on macOS Sierra 10.12.2. – Konrad Jan 17 '17 at 7:27
18

On Mac OS X, it appears that there is no command to save the options, and man top says nothing about a .toprc file. So it looks like the best you can do there is

alias top="top -o cpu"
10

You may need to hit Shift+f or Shift+o to get into the sort field sub-menu, then hit k for %CPU (followed by Enter to exit the menu). k means kill when you are in the default "global" command mode.

  • You'll need to press Shift+W to save the current configuration to ~/.toprc. Otherwise, this answer is easier to visualize then blindly pressing the < and > keys. – Ray Foss May 16 '16 at 11:42
7

I know it's not a direct answer to your question, but there's a wonderful tool called htop which I'd like to recommend. It's like an advanced version of the original top tool which allows you to sort the output in a much easier way and appearance. For example, if you want to sort by CPU, you simply hit F6 and choose your sorting.

Here's how htop looks like: htop screenshot

Here's some of the sorting options available: some htop sorting options

In order to install it on CentOS/RHEL machines you will have to add the EPEL repo and then simply run:

yum install -y htop

or on Debian machines simply run:

sudo apt-get install htop without adding any repo's.

I've written in the past a nice article about htop, feel free to check it.

2

If you run this in batch mode, you should try

top -b -o -%CPU

-b is for batch mode (usually used with -n to set a number of times to run) -o is to override the sorting order -%CPU is the %CPU field/column, you can use +/- to sort ascending or descending

I was using this with the -S option too, although I don't think that changes much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.