94

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.

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

6 Answers 6

108

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

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

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.

2
  • 11
    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, 2018 at 14:35
  • 6
    Actually, pressing x will highlight the current sort column even in non-color mode. Pressing b will toggle bold to highlight it even more. Dec 19, 2018 at 12:09
28

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"
13

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.

11

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.

1
  • 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, 2016 at 11:42
4

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

You must log in to answer this question.