Wednesday, December 29, 2010

How to Use the Unix Top Command


How to Use the Unix Top Command
Top is a small, but powerful program available on both UNIX and Linux systems whose purpose is to allow you to monitor processes on your system.

It has two main sections. The first displays general information such as the load averages, number of running and sleeping tasks, and overall CPU and memory usage. The second main section displays a sorted list of processes (usually by CPU usage) and showing their PIDs (Process ID number), user who owns the process, running time, and CPU and memory used by that process.

As such top is useful even without any further configuration. Just starting it up provides a plethora of information about your system's resource usage. If you just need to know what may be slowing your system down, for example, it might be enough to run top and see if there's any process running at close to 100% of CPU time or using a little too much memory.

It is, however, possible to configure it in a variety of ways that change how it displays information. We'll cover some top options on two of the most popular UNIX variants, Mac OS X and Linux (which is technically an UNIX clone).

Mac OS X Top

Top in Mac OS X is somewhat more limited than its Linux (and traditional FreeBSD) counterpart, but can still be a fairly useful tool.

To get started, you can run top -h to see all of the available commands, and man top to see an explanation of each. We'll cover a few examples to help get you started.

Perhaps the first thing you might want it to do is display processes sorted by what you want to focus on monitoring. For instance, you might want to primarily monitor memory usage so the default sorting by CPU usage might not quite fit the bill. To sort by memory usage you would use the top -o mreg command.

The top -o command accepts a number of other key's like mreg including cpu for CPU usage, time for execution time, threads for number of threads running, user for sorting by process's username, and so on. The full list is available in the man page (the man top command).

You can also control how often the information shown is updated (the default is every second). If you want to set it to update every 10 seconds just run top -s 10.

To limit how many processes in total are shown use the top -n 10 if you want to show just the top 10 processes.

Top on Mac has four modes of counting events the default one being non-event mode (top -c n). The accumulative mode (top -a) counts events cumulatively since the launch of top. The delta mode (top -d) counts events relative to the previous value. Absolute mode (top -e) counts using absolute counters.

Multiple top commands can be combined to control what exactly you want to see. For example, if you want top to show you processes sorted by memory usage, in delta mode, updating every 5 seconds and displaying only top 5 results you simply combine all of the appropriate commands (mentioned above) and run "top -o mreg -d -s 10 -n 5".

Linux Top

Top can be found on pretty much any Linux distribution out there including popular ones such as Ubuntu, Fedora and OpenSUSE which are among the easiest to install and try out.

The commands to get help with top on Linux are the same as on Mac OS X, top -h for a condensed list of commands, and man top for a much more verbose explanation of all available options.

It has a powerful interactive mode which is active by default when you start top, and is probably going to be the primary way in which you operate top since it provides the most flexibility and ease of use. You can use a variety of commands in form of just letter key presses to manipulate what you are seeing. Here are a few examples.

To sort processes by memory usage simply press M (shift-m on the keyboard). To sort by CPU usage press P (shift-p). You can see a full list of fields by pressing f which will show the fields with their appropriate letter (which you can use to sort by that field).

With the f key you can also opt to add a new field to display by pressing f and then the letter representing the key. For example if you want to add a code size field (CODE) press f and then r (as shown in the field list).

You can set the delay time for updates by just pressing s, putting in a desired number of seconds, and pressing enter. It will then update in that number of seconds, but you can update any time you want yourself by pressing the space bar.

To change the number of processes shown just press n, put in the desired number and press enter.

You can even kill processes from within top if you have a rogue process or a program of some kind wont quit or some process is using too many resources. To do so press k, enter the PID (Process ID, which you can read in top) of that process and press enter.

Linux top also supports the cumulative mode which can be simply toggled by pressing S (shift-s). A full list of all available interactive commands is available in the top manual page (man top).

Of course, it is still possible to start top with some options already set. For example, top -d 5 will cause top to refresh its data every five seconds (equivalent to s and 5 in the interactive mode). The top -q will display information without any delay, and run it with highest priority if top is run as a superuser (or root).

Top can also be started to show only a few updates and then quit automatically. Just run top -n 10, for instance, to have top show 10 iterations of information before quitting.

Another interesting command line option is top -b which runs top in a batch mode suitable for recording into a text file. Instead of just refreshing information on screen it prints it out in batches every specified number of seconds until all iterations run out (set by -n as mentioned above) or until it is quit. To write it out into a text file called top.txt stored in a local directory just run top -b > top.txt.

Just as in Mac OS X, it is possible to combine multiple commands to configure top at its start. For example, run top -d 5 -n 10 to start top with a refresh rate of five seconds and have it update only 10 times before quitting.

To quit top manually, in both OSX and Linux, just press the q key.

No comments:

Post a Comment