WebMasterCampus
WEB DEVELOPER Resources

Linux top Command

Learn Linux top Command with examples


Linux top Command

In Linux, we can use “top” command shows the active processes and other system information.

top Command Syntax

>> top -hv | -bcEeHiOSs1 -d secs -n max -u | U user -p pid(s) -o field -w [cols] 
Option Description
-h Shows the command help file.
-v Shows the library version and usage prompt and then quits the program.
-b Starts top in batch mode; allowing to send the command's output to a file or other programs. top doesn't accept input in batch mode and runs until killed or until it reaches the specified iteration limit.
-c Toggle command line/program name Reverses the last remembered c state. If top displayed command lines; that field now shows program names and vice versa.
-d [secs.tenths] Delay time interval Prompts you to specify the delay before updating the screen; overriding the default value. The value is in seconds. The command accepts fractional seconds but doesn't accept negative numbers.
-e [k | m | g | t | p] Toggle task memory scaling Allows you to change top task area memory scaling. The available scaling options are k (kibibytes); m (mebibytes); g (gibibytes); t (tebibytes); p (pebibytes).
-E [k | m | g | t | p | e] Toggle summary memory scaling allows you to change top summary area memory scaling. The available scaling options are k (kibibytes); m (mebibytes); g (gibibytes); t (tebibytes); p (pebibytes); e (exbibytes).
-H Threads mode operation Specifying the -H option instructs top to display individual threads instead of a sum of all threads in each process.
-i Idle process toggle Reverses the last remembered i state. When the toggle is OFF; top doesn't display the tasks that haven't used CPU resources since the last update.
-n Limit iteration number Specify the maximum number of iterations you want top to produce before ending.
-o [field name] Overwrite sort field specify the field name top uses to sort tasks. Prepending a + to the field name forces top to sort high to low while prepending it with - orders tasks low to high. The -o option primarily supports automated batch mode operation.
-O Output field names. The -O option complements the -o option; causing top to print each of the available field names separately and then quit.
-p [N1 N2...] Monitor process IDs mode. Instructs top to monitor only the processes with the specified process IDs. When combined with the threads mode (-H); top shows all the processes in each monitored PID thread group. The maximum number of PIDs is 20. Specifying the PID value 0 is considered as the top program process ID. To return to normal operation; issue any of the following commands: =; u; or U; as they are mutually exclusive.
-s Secure mode operation Starts top in secure mode; even for root.
-S Cumulative time toggle Reverses the last remembered S state. When the cumulative time toggle is ON; each process is listed with the CPU time it has used.
-u | -U [ID or name] User filter mode Displays only the processes matching the specified user ID or user name. The -u option matches only the effective user; while the -U option matches any user - real; effective; saved; or filesystem. Prepending the user ID or user name with an exclamation point (!) causes top to display only the processes that don't match the one provided.
-w [number] Output width override When used without an argument; -w instructs top to format the output using the COLUMNS= and LINES= environment variables; if applicable. Otherwise; the width is fixed at 512 columns maximum. Appending an argument to -w increases (up to 512) or decreases the output width while the row number is unlimited. The program never exceeds the actual terminal dimensions at which top was invoked.
-1 Single/separate CPU states toggle Reverses the last remembered CPU state portion. Depending on the NUMA Node command toggle (2) state; either displays all CPU information in a single line or separately.

top command Example

To list all running Linux processes on your system, open a terminal and enter:

>> top

If the process list is long, scroll through it using the Up and Down arrows and Page Up and Page Down keys. To quit top, press q.

top -n to Exit After X Repetitions

The top command keeps refreshing the statistics until quitting the program by pressing q. Instruct top to quit automatically after refreshing the stats for a specified number of times.

>> top -n [X]

Sort Processes By default, top sorts the process list using the %CPU column. To sort processes using a different column, press one of the following keys:

M. Sort by the %MEM column. N. Sort by PID column. T. Sort by the TIME+ column. P. Sort by the %CPU column. In the following example, the process list is sorted by the %MEM column:

top -u to Filter Processes by Specific User

The -u option allows you to display all user-specific processes. Press the u key while top is running or use the following syntax:

>> top -u [user name]

Pressing the u key in interactive mode prompts for the user name or user ID.

Change Display Units

Use the E key to choose the units you want top to use for displaying the memory values.

Press lowercase e to choose the units for displaying values in the process list. The available options are.

  • kibibytes (KiB)
  • mebibytes (MiB)
  • gibibytes (GiB)
  • tebibytes (TiB)
  • pebibytes (PiB)
  • exbibytes (EiB - applies only for memory values)

Change Output Contents

The default top output contains a lot of information and may appear confusing.

Tune the contents with a few keypresses, find the information you need, or delete certain sections from the summary.

Individual CPU Core Statistics

Pressing 1 on your keyboard allows you to see individual statistics for each CPU core. Press 1 repeatedly to toggle core statistics.

CPU Usage Graph

If you want a graphical representation of CPU usage, top supports elementary ASCII graphs, denoting each CPU core usage. Toggle the graphs on or off by pressing t.

To change the graph display to solid block characters, press t again.

Memory Usage Graph

The command also supports graphical display options of memory and swap memory lines. To get a visual representation of memory usage, press m. Remove the lines by pressing m again.

A graphical representation of memory usage in Linux.

Emphasize the information in the output by adding color to the output. Press the z key to add color to the display.

Show Process Command Line

To see the full command line instead of the process name, press the c key. To toggle back to displaying the process name, press c again.

Showing the process command line instead of the process name.

See Process Hierarchy

To see which processes were launched or spawned by other processes, press the V key.

The tree view is useful when you want to see the child-parent process hierarchy on your system.

Show Active Tasks

To filter the output and show only active tasks, press the I key.

Filtering the top command output to show only active tasks.

The output hides the tasks that haven’t used any CPU resources since the last update.

Limit Process Number

Press the n key to limit the process number in the output. Regardless of the number of active processes. top prompts you to enter the number of processes you want to see.

Change Process Priority

The top command allows changing the process priority (nice value).

Follow these steps:

  1. Run top and press the r key.

  2. When prompted, enter the process ID and press Enter.

  3. After you enter the PID, the program prompts for a new nice value. Enter a new value and press Enter.

Note: Negative nice values indicate a higher process priority.

Filter Processes

The top filter feature allows using a filter expression to limit which processes to see in the list. Activate the filter option by pressing the o key. The program prompts you to enter a filter expression.

>> %MEM>5.0

Filtering processes in the top command using filter expressions. The top command now shows only the processes that used more than 5% memory. Clear the filters by pressing =.

Created with love and passion.