April 13, 2014, 2:12 PM — The top command provides a quick glimpse into how a Unix system is performing. It highlights the processes that are using most of your CPU cycles, givs you an idea how much memory is in use, and even provides some data that can tell you whether performance is getting better or worse. Still, there are a number of options that you may have never tried that can help you find the answers you are looking for more quickly.
One option is to use the top command to display tasks for just a single user. To do this, just follow the top command with the -u option and the username of the particular user. This will let you focus on what that user is doing on the system.
$ top -u mjones top - 12:35:45 up 86 days, 1:30, 1 user, load average: 3.06, 3.03, 3.01 Tasks: 192 total, 5 running, 187 sleeping, 0 stopped, 0 zombie Cpu(s): 36.3%us, 38.8%sy, 0.0%ni, 24.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2074932k total, 2024796k used, 50136k free, 391756k buffers Swap: 4192956k total, 1426488k used, 2766468k free, 605736k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7996 mjones 25 0 2052m 697m 1084 R 63.0 34.4 653:47 bash 8564 mjones 16 0 4784 392 384 S 0.0 0.0 0:00.00 bash 8566 mjones 19 0 2444 988 760 S 0.0 0.0 215:26.19 top
You will see only the processes (and likely all of the processes) being run by that user.
You can also use top to look at a single process and nothing else.
$ top -p 22526 top - 13:00:56 up 86 days, 1:55, 1 user, load average: 3.00, 3.00, 3.00 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Cpu(s): 37.3%us, 37.7%sy, 0.0%ni, 25.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2074932k total, 2025044k used, 49888k free, 392164k buffers Swap: 4192956k total, 1426488k used, 2766468k free, 605736k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22526 shs 15 0 4784 1476 1204 S 0.0 0.1 0:00.05 bash
While top's output is normally sorted on the %CPU usage column, you can instead sort it on some other column. To sort based on memory usage, for example, start top and then type M (a capital M). Typing a lowercase m will turn off or back on the display of memory statistics that appear at the top of your top output.
top - 12:34:56 up 86 days, 1:29, 1 user, load average: 3.14, 3.04, 3.01 Tasks: 192 total, 5 running, 187 sleeping, 0 stopped, 0 zombie Cpu(s): 36.3%us, 38.8%sy, 0.0%ni, 24.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2074932k total, 2024672k used, 50260k free, 391736k buffers Swap: 4192956k total, 1426488k used, 2766468k free, 605736k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7996 mjones 25 0 2052m 697m 1084 R 63.0 34.4 46852:58 bash 1927 root 10 -10 22524 21m 1740 S 0.0 1.1 0:00.02 iscmd12 1233 root 18 0 27052 12m 7440 S 0.0 0.6 0:00.43 httpd 18857 apache 17 0 27076 7184 2140 S 0.0 0.3 0:00.00 httpd
You can also select the column you would like to sort your top output on by selecting it from a list of options. To do this, once you've started top, press a capital O and you will see a list of options like that shown below.
Current Sort Field: K for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id the TTY & WCHAN fields will violate b: PPID = Parent Process Pid strict ASCII collating sequence. c: RUSER = Real user name (shame on you if WCHAN is chosen) d: UID = User Id e: USER = User Name f: GROUP = Group Name g: TTY = Controlling Tty h: PR = Priority i: NI = Nice value j: P = Last used cpu (SMP) * K: %CPU = CPU usage l: TIME = CPU Time m: TIME+ = CPU Time, hundredths n: %MEM = Memory usage (RES) o: VIRT = Virtual Image (kb) p: SWAP = Non-resident size (kb) q: RES = Resident size (kb) r: CODE = Code size (kb) s: DATA = Data+Stack size (kb) t: SHR = Shared Mem size (kb) u: nFLT = Page Fault count v: nDRT = Dirty Pages count w: S = Process Status x: COMMAND = Command name/line y: WCHAN = Sleeping in Function z: Flags = Task Flags
Notice the * to the left of K: %CPU. This indicates which of the columns the information is being sorted on currently. Press another letter from the list and you will see the * move to a different line in your display. Then press return to see the data sorted on that column.
If you are sufficiently empowered, you can also kill processes from top without exiting top. Just press a lower case k and you will be prompted first for the process you want to kill and then for the signal you want to use to kill it (the default is 15). You will see an "Operation not permitted" error if you don't have sufficient rights to kill the process that you've selected.
Similarly, you can renice (i.e., change the nice setting) for a process by typing a lowercase r. You will then be prompted for the process ID of the process you want to renice and then the nice setting that you want to use instead.
PID to renice: 22720
and then ...
Renice PID 22720 to value: 10
If the system you are working on has more than one CPU, your top default display will combine the information on all CPUs into one line. To break this down by CPU instead, press a 1 while in top and your display will change to something like this:
top - 13:12:18 up 86 days, 2:07, 1 user, load average: 3.06, 3.09, 3.05 Tasks: 192 total, 5 running, 187 sleeping, 0 stopped, 0 zombie Cpu0 : 37.3%us, 62.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu1 : 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 13.3%us, 86.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2074932k total, 2025292k used, 49640k free, 392424k buffers Swap: 4192956k total, 1426488k used, 2766468k free, 605740k cached
Typing c while running top will display the full path for the currently running process.
7072 root 25 0 4216 984 812 R 100.0 0.0 9813:10 /usr/bin/whois 220.127.116.11
The top command will normally run continuously, updating its display every few seconds. If you would prefer that it update less frequently, you can type a lowercase d and then, when being prompted, tell top how often (in seconds) you want to see the updates.
Change delay from 3.0 to: 10
If you want top to run through a limited set of iterations, you can provide this number when you start top. For example, if you want to see only two iterations, type top -n 2.
% top -n 2
You can also type a lowercase h to get a little help while running top and, of course, q to quit
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.
flickr / Richard Leeming