This article is part of the on-going 15 example series where 15 examples will be provided for a specific command or functionality. In this series, earlier we discussed about find command, crontab examples, grep command, history command, ping command, and wget examples.
In this article, let us review 15 examples for Linux top command that will be helpful for both newbies and experts.
1. Show Processes Sorted by any Top Output Column – Press O
By default top command displays the processes in the order of CPU usage. When the top command is running, press M (upper-case) to display processes sorted by memory usage as shown below.
Fig: Press M to sort by memory usage – Unix top command
To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.
Current Sort Field: P for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id v: nDRT = Dirty Pages count d: UID = User Id y: WCHAN = Sleeping in Function e: USER = User Name z: Flags = Task Flags ........
When the linux top command is running, Press R, which does the sort in reverse order.
2. Kill a Task Without Exiting From Top – Press k
Once you’ve located a process that needs to be killed, press ‘k’ which will ask for the process id, and signal to send. If you have the privilege to kill that particular PID, it will get killed successfully.
PID to kill: 1309 Kill PID 1309 with signal [15]: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent 5136 root 16 0 38040 14m 9836 S 0 0.2 0:00.39 nautilus
3. Renice a Unix Process Without Exiting From Top – Press r
Press r, if you want to just change the priority of the process (and not kill the process). This will ask PID for renice, enter the PID and priority.
PID to renice: 1309 Renice PID 1309 to value: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
4. Display Selected User in Top Output Using top -u
Use top -u to display a specific user processes only in the top command output.
$ top -u geek
While unix top command is running, press u which will ask for username as shown below.
Which user (blank for all): geek PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
Display Only Specific Process with Given PIDs Using top -p
Use top -p as shown below to display specific PIDs.
$ top -p 1309, 1882 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
5. Display All CPUs / Cores in the Top Output – Press 1 (one)
Top output by default shows CPU line for all the CPUs combined together as shown below.
top - 20:10:39 up 40 days, 23:02, 1 user, load average: 4.97, 2.01, 1.25 Tasks: 310 total, 1 running, 309 sleeping, 0 stopped, 0 zombie Cpu(s): 0.5%us, 0.7%sy, 0.0%ni, 92.3%id, 6.4%wa, 0.0%hi, 0.0%si, 0.0%st
Press 1 (one), when the top command is running, which will break the CPU down and show details for all the individual CPUs running on the system as shown below.
top - 20:10:07 up 40 days, 23:03, 1 user, load average: 5.32, 2.38, 1.39 Tasks: 341 total, 3 running, 337 sleeping, 0 stopped, 1 zombie Cpu0 : 7.7%us, 1.7%sy, 0.0%ni, 79.5%id, 11.1%wa, 0.0%hi, 0.0%si, 0.0%st Cpu1 : 0.3%us, 0.0%sy, 0.0%ni, 94.9%id, 4.7%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 3.3%us, 0.7%sy, 0.0%ni, 55.7%id, 40.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 5.0%us, 1.0%sy, 0.0%ni, 86.2%id, 7.4%wa, 0.0%hi, 0.3%si, 0.0%st Cpu4 : 38.5%us, 5.4%sy, 0.3%ni, 0.0%id, 54.8%wa, 0.0%hi, 1.0%si, 0.0%st Cpu5 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu6 : 0.3%us, 0.7%sy, 0.0%ni, 97.3%id, 1.7%wa, 0.0%hi, 0.0%si, 0.0%st Cpu7 : 5.4%us, 4.4%sy, 0.0%ni, 82.6%id, 7.7%wa, 0.0%hi, 0.0%si, 0.0%st Cpu8 : 1.7%us, 1.7%sy, 0.0%ni, 72.8%id, 23.8%wa, 0.0%hi, 0.0%si, 0.0%st
6. Refresh Unix Top Command Output On demand (or) Change Refresh Interval
By default, linux top command updates the output every 3.0 seconds. When you want to update the output on-demand, press space bar.
To change the output update frequency, press d in interactive mode, and enter the time in seconds as shown below.
Change delay from 3.0 to: 10 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
7. Highlight Running Processes in the Linux Top Command Output – Press z or b
Press z or b, which will highlight all running process as shown below.
Fig: Ubuntu Linux – top command highlights running process
8. Display Absolute Path of the Command and its Arguments – Press c
Press c which will show / hide command absolute path, and arguments as shown below.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 /usr/sbin/gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 /usr/sbin/gagent -l 0 -u pre
9. Quit Top Command After a Specified Number of Iterations Using top -n
Until you press q, top continuously displays the output. If you would like to view only a certain iteration and want the top to exit automatically use -n option as shown below.
The following example will show 2 iterations of unix top command output and exit automatically
$ top -n 2
10. Executing Unix Top Command in Batch Mode
If you want to execute top command in the batch mode use option -b as shown below.
$ top -b -n 1
Note: This option is very helpful when you want to capture the unix top command output to a readable text file as we discussed earlier.
11. Split Top Output into Multiple Panels – Press A
To display multiple views of top command output on the terminal, press A. You can cycle through these windows using ‘a’. This is very helpful, when you can sort the output on multiple windows using different top output columns.
12. Get Top Command Help from Command Line and Interactively
Get a quick command line option help using top -h as shown below.
$ top -h top: procps version 3.2.0 usage: top -hv | -bcisS -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
Press h while top command is running, which will display help for interactive top commands.
Help for Interactive Commands - procps version 3.2.0 Window 1:Def: Cumulative mode Off. System: Delay 3.0 secs; Secure mode Off. Z,B Global: 'Z' change color mappings; 'B' disable/enable bold l,t,m Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info 1,I Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode ..........
13. Decrease Number of Processes Displayed in Top Output – Press n
Press n in the Interactive mode, which prompts for a number and shows only that. Following example will display only 2 process as a time.
Maximum tasks = 0, change to (0 is unlimited): 2 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1309 geek 23 0 2483m 1.7g 27m S 0 21.8 45:31.32 gagent 1882 geek 25 0 2485m 1.7g 26m S 0 21.7 22:38.97 gagent
14. Toggle Top Header to Increase Number of Processes Displayed
By default top displays total number process based on the window height. If you like to see additional process you might want to eliminate some of the top header information.
Following is the default header information provided by top.
top - 23:47:32 up 179 days, 3:36, 1 user, load average: 0.01, 0.03, 0.00 Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie Cpu(s): 0.7% user, 1.2% system, 0.0% nice, 98.0% idle Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached
- Press l – to hide / show the load average. 1st header line.
- Press t – to hide / show the CPU states. 2nd and 3rd header line.
- Press m – to hide / show the memory information. 4th and 5th line.
15. Save Top Configuration Settings – Press W
If you’ve made any interactive top command configurations suggested in the above examples, you might want to save those for all future top command output. Once you’ve saved the top configuration, next time when you invoke the top command all your saved top configuration options will be used automatically.
To save the top configuration, press W, which will write the configuration files to ~/.toprc. This will display the write confirmation message as shown below.
top - 23:47:32 up 179 days, 3:36, 1 user, load average: 0.01, 0.03, 0.00 Tasks: 67 total, 1 running, 66 sleeping, 0 stopped, 0 zombie Cpu(s): 0.7% user, 1.2% system, 0.0% nice, 98.0% idle Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached Wrote configuration to '/home/ramesh/.toprc'
Comments on this entry are closed.
“htop” is somehow more intuitive and as powerful as top; try that one
very nice post….keep up the good work….cheers
only active processes top -i
use “.toprc” file to suit your needs permanently
Good Work! Very useful for people like me who have trouble memorizing so many commands. Keep it up.
Cheers,
— VB
I’d have to agree that “Htop” is definitely the better option when it comes to system monitoring. Htop’s options to kill processes are much more thorough… if you don’t want to kill a process you can even send it a more particular signal. Plus IMHO the default coloring and hotkeys are more intuitive.
Even better, use htop instead of top. Cheers!
Great article… keep up the good work!
Yes, htop is better, but the author of this article still did some fabulous work here (and it isn’t the default).
When sorting in top, the -keys can be used to switch between the columns and R will invert the sorting (ie. sort processes by least memory/cpu/priority/whatever). This, in my opnion, makes it a lot easier to switch around between different sortings
Another vote for htop as a top replacement
Thank you, this helped me so much, I might not need htop anymore. 🙂
Very cool! Tons of stuff I didn’t know here. For whatever reason, I can’t get the ‘k’ to kill command to work within the Mac OS X command line. I’ve tried a gazillion alternates, it looks like the functionality is just not there or perhaps it’s a different key to press all together.
pressing ? in top will give a nice menu summarizing the above
Good Stuff! I love htop too but it doesn’t work in Mac OS X, so this article is even more useful to mac users! Thanks!
this is very good for e learner
OS X has a slightly different version of top. The same effect can be achieved by pressing ‘S’ (Signal), choosing which signal to send (TERM by default), and then typing in the pid just like the top most Linux distributions ship with.
Correction! It seems there is a version of htop available for mac as well! Grab it from
http://www.mac-fun.com/site/2009/07/htop-mac-installer/
Top is good but htop is super hot wow
looks like top in 3000 AD
Great job–this has been extremely helpful for me. I’m going to have a job interview and I’m going to get drilled on these.
“top -b1 -n1” will give the top output in long format (showing all the processes running on the OS). It is very easy to idetify the memory/CPU usage by all process.
gr8 article…it really helped me to understand the various options…thankyou so much 🙂
Hi,
In the above command output can any one tell me what format the time is in “Time+” is it hh:mm.(ss/100) or is it mm:ss.ss?
Learnt a lot of new stuff here – cheers!
Hi,
Can you please post how to send email alerts by using sendmail.
thanks,
i think “atop” is more powerful than top
Is is true ?
Hi frnds i need ONLY cpu usage and name of the process from top command and other columns need to be filtered out, can u help me plsss.
Thnx.
nice posting sir, thanks you..
Thank you so much for sharing your knowledge.I would recommend your website to all the linux learners.Please continue your good work.
Can we create graph using this parameters
for plotting graphs from top look at this.
How run it with sorting by memory? And how run 2 tops – one with sorting by memory, second – with sorting by CPU? And how show all it in conky?
Many Thanks for the valuable info.
Thanks & Regards
Muthukumar.L
Really good work Ramesh
Many thanks
Thanks for this, can you write a post explaining the first four lines of top command.. I dont see to find it any where.
Hi RAMESH,
Nice post. I am wondering how we can monitor the CPU/Mem usage using scripts. You know, some result can only be shown interactively with human interference(eg. press 1 to toggle the usage of between per call and total CPU). Do you know we can toggle that in a script?
Thanks
Thanks; It is very Nice post ..
How can we store top command output including cpu utilization.
I want to capture below line in output
CPU states: 45.0% user, 0.1% nice, 27.9% system, 12.0% wio, 14.8% idle
I tried with below options :-
top > cpu_utl.log
nohup top & > cpu_utl.log
but when we cat output of cpu_utl.log ; it doen;t contain cpu utilization.
Please suggest.
Thanks,
Rama
top -M is something which will be useful – human readable memory available.
Awesome job Ramesh. I am a fan of your blogs, please keep new articles coming.
Batch Mode
top can also be run in batch mode. Try running the following command:
$ top -n 1 -b >/tmp/top.out The -n 1 tells top to only show one iteration, and the -b option indicates that the output should be in text suitable for writing to a file or piping to another program such as less.
Can I `somehow toggle “z” ON when starting top?
This is part of my script so I can’t manually turn it on.
to Rama:
A bit late but might help others 🙂
I think this is what you were looking for:
top -n 1 -b | grep Cpu > cpu.txt
Does the “m” on the virtual memory column on the results mean megabytes? according to man top, it would return values in kb but I see some values for the virtual memory in the form ***m. Thanks
gr8 article…it really helped me to understand the various options…thankyou so much
Hello…!!
Thanks for sharing valuable info….
Nice article.