The previous two episodes have covered the Summary Area and Task Area of top repesctively. This episode will detail how to control the output of top via shortcut keys and command line switches.
There are a few modes in top that control how information is displayed. These modes can be set with commandline switches and/or hot keys:
- Cumulative Mode – Cumulative mode shows the CPU time used by the process since it started and includes the CPU time consumed by the proceses dead children. The default is for Cumulative mode to be turned off and only show the CPU time that the process has consumed since it started and not including the dead children. This value can be toggled on and off while top is running by pressing “S” (shift+s). Starting top with the “-S” flag will set Cumulative mode to the reverse of the last known Cumulative mode state. More than likey this will set Cumulative mode on. If you have a .toprc file that sets Cumulative mode on then the “-S” would toggle Cumulative mode off.
- Irix Mode – Irix mode controls how CPU percentages are displayed. In a SMP system the CPU percentage is a sum of the total number of CPU’s * 100. So in a quad core system that value would be 4*100=400 or 400%. CPU percentages would be shown against this value so 15% would be 15% of 400%. The hot key to toggle Irix mode off and turn on Solaris mode is “I” (shift+i). In Solaris mode total CPU percentage is 100% regardless of the number of CPU’s. Therefore, a process consuming 5% CPU would be 5% of 100%. Irix mode does not have a toggle flag to start top with.
- Secure Mode – Secure mode is disabled by default but can be turned on via the command line with the “-s” switch. There is not hot key toggle for Secure mode. Secure mode limits some of the interactive commands of top even if top is run at the root user. Those include the ability to renice or kill a process. If you are going to keep top running it may be in your best interest to set secure mode on by default in your the top configuration file. You can see whether top is running in Secure mode by viewing the help window.
- Threads Mode – Threads mode toggles whether or not processes are shown as tasks or threads. The hotkey for this is “H” (shift+h) and the command line switch is “-H”. You can tell if you are in Threads mode or not by looking at the second line in the summary window. If Threads mode is turned on the first word will say “Threads:” instead of “Tasks:”. Threads mode will display running threads instad of running tasks in the Task Summary window.
Instead of monitoring all processes you can pass a list of process to top that you want to monitor with the “-p” switch. The list can be a separate “-pN1 -pN2…” or a commas separated list: “-pN1,N2,N3”. You can pass up to 20 different pids to this mode. Once in the “Monitor PIDs” mode you can exit back to normal mode without quitting top by pressing any of the following keys: “=”, “u”, or “U”. Once you leave, though, you cannot return unless you quit top and rexecute the command with the previous options.
You can specify the processes of what users you want to monitor with the “-u” or “-U” switches. Use “-u” to match on effective user. Recall from the previous entry that the effective user is the user the process is currently running under. It may not be the user that started the process, as the UID could have been altered by a command like suid. The “-U” option will match on effective, user, saved, or filesystem. You can only specify one user name with this switch, but you can alter the user to monitor by pressing the “u” key while top is running. While top is running if you press the “u” key it will ask you which user’s tasks to monitor or you can leave blank to monitor them all.
You can alter the output width of top using the “-w” option with a number. The upper limit of columns is 512.
The default is for top to run continuously until one quits. You can specify a number of iterations to run with the “-n” switch and a number. So -n 4 would run top for 4 iterations including the start-up display. By default top refreshes itself every 3 seconds, this can be changed with the “-d” switch with a number specified in seconds and tenth of a second:
top -n 5 -d 5.50
This would run top for 5 iterations with 5.50 seconds between iterations.
Top has a batch mode which is used primarly to send the output of top to other programs or to a file. Batch mode is specified with a “-b” and once in batch mode top will not accept any other input. Therefore you should run it with the “-n” option to specify a number of iterations otherwise you will have to kill top using “ctrl+c” instead of “q” to quit.
Forest View Mode – Forest View mode displays processes or threads in a hierarchical tree ordered by their parent. This mode is toggled using the “V” (shift+v) but does not have a command line switch.
While running top can accept the following hot keys to control the display:
- *<enter> or <space> will refresh the display, toggle an immediate interval.
- “=” removes any limits or restrictions on which tasks are shown. For instance, if you specified only to show tasks by the root user and then pressed “=” top would show the processes from all users.
- “B” <shift + b> – will toggle bold.
- “d” or “s” will allow you to adjust the refresh interval
- “H” toggle threads mode
- “I” toggle Irix mode
- “q” quit top
- “i” toggle idle tasks off/on.
- “x” will highlight the column top is sorting on.
You can limit the number of tasks to display in the task area with the “n” or “#” key. Pressing “n” or “#” will prompt you for a number or rows to limit the task area to. If you enter “0” then unlimited tasks will be displayed.
Top will allow you to kill processes that you have permission to kill with the “k” key. Pressing the “k” key to ewill prompt you for the PID and then the kill signal to pass. More than likely you will want to pass a 1 or a 9 as the value. If you are running top in secure mode then you cannot use top to kill a process.
You can renice processes with top if you have the permission to do so on that process. Press the “r” key and you will be prompted for whic PID to renice and then what value to enter. The value can be from 19 to -19. The lower the nice value the higher priority the process is given. As a standard user you will only be able to renice processes you control with values between 19 and -10. If you try to renice a process with a value of -11 to -19 you will be denied permission. If you try to renice a process with a value greater than 19 it will just set the value to 19. You cannot renice processes when safe mode is on.
You can sort the task window using the following controls:
- “M” <shift+m> – This will sort the tasks by memory usage
- “N” <shift+n> – This will sort the tasks by PID
- “P” <shift+p> – This will sort the tasks by CPU usage
- “T” <shift+t> – This will sort the tasks by TIME+ or how long a process has been running.
- “R” <shift+r> – reverse the current sort on field (from highest to lowest usage and vice versa).
You can also adjust which column you want to sort on by using the “<” and “>” keys to move the sort column. Pressing the “x” key to highlight the sort column can be very helpful in identifying you are sorting on the column you want.
You can scroll the task using the up and down arrow to move up and down the task list. You can use page down and page up to move by pages. Home will bring you to the top of the list and end will take you to the bottom of the list. The left and right arrow keys will allow you to scroll the columns accordingly.
You can search the task area by pressing the “L” <shift+l> key. The search is case sensitive and will return matches on all columns. The “&” key will cycle through the established match to locate the next match.
This entry of Linux in the Shell covered the basic controls of top, both command switches and top hot keys. The final installment will talk about alternate window displays and color highlighting controls and complete the series on top.
- man top
- info top
If the video is not clear enough view it off the YouTube website and select size 2 or full screen. Or download the video in Ogg Theora format:
- Episode 015 – Control Top Ogg Theora Video – Archive.org
Thank you very much!
- Ogg file hosted by Hacker Public Radio
- Speex file hosted by Hacker Public Radio
- Mp3 file hosted by Hacker Public Radio