Javatpoint Logo
Javatpoint Logo

Top Command in Linux/Unix with Examples

The top command displays all the running process within the environment of your system. It helps in monitoring system usage and performances. It is mainly used to detect load on the server by system administrators.

The top command stands for table of processes. It is a task manager program, detected in several Unix-like operating systems, that shows information about memory and CPU utilization.

Overview of Top Command

The program generates an ordered list of active processes chosen by user-specified format and periodically updates it. Default ordering can be done by CPU usage, and the top CPU consumers are only shown. The top command displays how much memory and processing power is being utilized, as well as other details of the active processes.

A few top versions permit extensive customization of display, like sorting methods or choice of columns. The command is helpful for system administrators because it displays which processes and users are utilizing the most system resources at a time.

Implementations of Top Command

There are various different top versions available. The classical Unix version was specified by William LeFebvre and copyrighted in 1984 originally. It's hosted on SourceForge, and the 3.7 version was revealed in 2008. The Linux release of top is an element of the procps-ng tool group. Originally, it was specified by Roger Binns but after that taken over by others shortly. The roughly equivalent function is prstat on Solaris.

Microsoft Windows contains the graphical Task Manager utility and tasklist command. IBM AIX contains an updating active processes list as a component of the topas_nmon and topas commands.

In Linux, the load average numbers are known as the sum of the total processes waiting inside the run-queue plus the total count executing currently. The number is not relative but absolute. Hence, unlike utilization, it can be unbounded. The instant variations of the total processes are damped using an exponential decay formula calculated with fixed point math.

A program, i.e., ps, is the same as the top command but rather generates a process snapshot taken during invocation. The n (total iterations) option of the top command can generate a similar result, making the program execute the specified iterations and exit after showing its result.

Options of Top Command

Options Description
-a This option is used to solve the processes according to the allocated memory.
-b It begins the top command in batch mode, which can be helpful in sending results from the top command to other files and programs.
-c It begins the top command with the last remembered state reversed 'c'.
-d It shows the delay between the screen updates and then overrides the associated value in the startup default or personal configuration file of one.
-h It shows the usage prompt and library version, then quit.
-H It begins a top command with the last remembered state reversed 'H'.
-i It begins a top command with the last remembered state reversed i.
-m It reports USED (rss process sum and swap count) rather than VIRT.
-M It shows memory units and displays floating point values within the memory summary.
-n It describes the maximum number of frames or iterations; the top command should generate before completion.
-p It only monitors processes with process IDs.
-s It is far better managed from the system configuration file.
-S All processes are listed using the CPU time that it and its dead children have utilized when "Cumulative mode" is active.
-U It only monitors processes with an effective username or UID matching that provided one. It matches saved, effective, real file system UIDs.
-u It only monitors processes with an effective username or UID matching that provided one.
-v It shows the usage prompt and library version, then quit.

Columns and Fields of Top Command

Some of the important fields or columns available in the top command are explained below:

  • PID: It stands for Process Id or unique process Id of the task, which wraps periodically, never rebooting at zero.
  • RUSER: It stands for the Real User Name of the task's owner.
  • PPID: It stands for Parent Process Pid. It is the process ID of the parent of a task.
  • UID: It is the effective user Id of the owner of the task.
  • USER: It is the effective user name of the owner of the task.
  • GROUP: It is the effective group name of the owner of the task.
  • TTY: It is the controlling terminal name.
  • PR: It shows the task's priority.
  • NI: It is the task's nice value. A negative NI defines higher priority, and a positive NI defines lower priority.
  • P: A number indicating the last utilized processor.
  • TIME: It shows the CPU time that the task has utilized since it began.

Syntax:


Linux top1

Look at the above snapshot, its output is explained here,

Line1

  • Time
  • how long system is running
  • how many users are logged in
  • and load average

Line2

  • Total number of tasks
  • number of running tasks
  • number of sleeping tasks
  • number of stopped tasks
  • and number of zombie tasks

Line3

It shows CPU usage in percentage for

  • users
  • system
  • low priority processes
  • idle processes
  • io wait
  • hardware interrupts
  • software interrupts
  • steal time

Line4

It shows memory usage in kilobytes for

  • total memory
  • used memory
  • free memory
  • buffered memory

Line5

It shows swap memory usage in kilobytes for

  • total memory
  • used memory
  • free memory
  • cached memory

Table explanation

  • proces ID
  • user
  • priority
  • nice user
  • virtual memory
  • resident memory
  • shareable memory
  • CPU used percentage
  • memory used percentage
  • time a process has run
  • command

If you want you can hide/show these header lines by pressing some keys.

For example,

press l - to show/hide Line1. Top line

press t - to show/hide Line3. CPU information

press m - to show/hide Line4 and 5. Memory information

Keeping top command running in background

You can keep top command running in the background continuously without typing top in terminal every time.

Use ctrl+z keys to get back your terminal.

Linux top2

Look at the above snapshot, after pressing ctrl+z keys top command has stopped and we got our terminal back.

To bring back top command in terminal type fg in terminal.

Sorting top output

By default, top command always display output in the order of CPU usage.

Press M - To display in order of memory usage.

Linux top3

Press O - To display all possible columns that you can sort.

Linux top4

Look at the above snapshot, all the columns are assigned an alphabetic letter. To sort by column type the respective alphabet and output will be sorted according to that column.

In the first line, current sort field is shown that is N which means currently it is sorted according to column N.

Press R - To display in reverse order.

Linux top5

Killing a task without exiting from top

A task can be stopped without exiting from top command by pressing k key.

It will ask for task's PID number, if you'll have authority to kill that task, then task will be removed. Otherwise, your command will fail.

Linux top6

Look at the above snapshot, after pressing k, we got a message asking for PID of task to be killed.

Renice a task

Renice is done to change the scheduling order. By pressing r, you can change the priority of a process without killing it. It will also ask for PID of the process.

Linux top7

Look at the above snapshot, after pressing r, we got a message asking for PID of task to be reniced.

Display processes for selected user

In top command output you can display all the processes for a particular user only by two options. One through command line and other without existing top.

In command line, use the following command

Syntax:

Example:

top -u sssit

Linux top8

Look at the above snapshot, it displays all the processes only for user sssit.

When top command is running, press u, it will ask for username. Type the username and press enter.

Linux top9

Look at the above snapshot, after pressing u, it is asking for username.

Updating top output

By default, top output is updated after every 3 seconds. When you want to update it in between 3 seconds press space bar.

You can also change updating frequency by pressing d key while running top command.

Linux top10

Look at the above snapshot, after pressing d key, it is asking for time for which it will be frequently updated.

Changing colors

Colors can be changed by pressing z key and text can be made bold by pressing b key.

Linux top11

Look at the above snapshot, by pressing b all running processes are highlighted in white.

To change color press z (small z) key.

Linux top12

Look at the above snapshot, our output is colored after pressing z.

Now, if you want to change the colors for different areas, press Z (capital Z). it will take you to the menu where you can select different colors for different target.

Linux top13

Suppose we want to apply blue color in column heading and magenta color in the task information.

Then we'll press 4 with H for heading and 5 with T for task information.

Linux top14

Look at the above snapshot, colors have been changed for their respective target.

Quitting after certain iterations

The top command continuously displays output until you'll quit by pressing q.

But you can define certain number of iterations after which top command will automatically quit from the terminal.

Syntax:

Example:

top -n 2

With above example, it will show 2 iterations and exit automatical


Next TopicLinux last





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA