Jump to content

Top Command in Linux


Linux Hint

Recommended Posts

In UNIX-like systems, the top command reports valuable system information like running processes and resource usage. It shows processor activity and kernel-managed tasks in real-time. It’s one of the staple tools for system administrators.

In this guide, check out how to use the top command in Linux.

Top command usage

The top command will come pre-installed on any UNIX/Linux system. There’s no need to install any additional package to use it.

Check top version

Assuming your distro is up-to-date, your system should have the latest version of the top. Check the version of the top.

$ top -v

word-image-375.png

Default window

Run the top command without any parameter. This will load the default screen of the top.

$ top

word-image-376.png

It’ll output a real-time report of various information. Let’s have a quick breakdown of it.

The first heading portion reports hardware resource usage. The first line consists of the time, the amount of time the system is running, the number of logged-in users, and the load average. The second line reports the number of tasks along with their states.

Here’s a quick list of all the states. The value of each state describes how much time the CPU spends executing processes of that state.

  • us: Executing processes running under the userspace.
  • sy: Executing system kernel processes.
  • ni: Executing processes with a manually configured nice value.
  • id: The amount of time CPU remains idle.
  • wa: Waiting time for I/O to complete.
  • hi: Servicing hardware interrupts.
  • si: Servicing software interrupts.
  • st: Time lost for running virtual machines, also known as “steal time”.

The fourth line describes the system memory usage, for example, total physical memory amount and how much of it is used, free, buffered, or cached.

The second part of the output is a table listing all the running processes. Here’s a quick explanation of all the columns constituting the table. All these explain various attributes of the related process.

  • PID: The process ID of the process.
  • USER: The user the process is running under.
  • PR: Processor priority.
  • NI: Process nice value.
  • VIRT: Virtual memory used.
  • RES: Resident memory used.
  • SHR: Shared memory used.
  • S: Process status. It can of five types described below
    • D: Uninterruptible sleep
    • R: Running
    • S: Sleeping
    • T: Traced
    • Z: Zombie
  • %CPU: CPU time consumed by the process.
  • %MEM: Percentage of physical memory used.
  • TIME+: Total CPU time used (in millisecond).
  • COMMAND: The command that’s the process is running with.

I guess that’s a lot of information to digest. It’s alright to take time and learn them slowly.

Scrolling

Top reports the entire list of running processes. As it’s a command-line tool, the navigation is a bit different than you’d expect in a GUI tool.

To navigate, use the Up and Down arrow keys. Additional navigation keys include Home, Page Up, Page Down, and End. To move the process list sideways, use the Left/Right arrow keys.

word-image-377.png

Change numeric unit

By default, the memory values are displayed in kibibytes. However, for practical purposes, it’s not a pleasant format. To switch the unit to other units, press “E”. The available unit formats are kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, and exbibytes. Pressing “e” will do the same thing.

Change summary content

This impacts the first section of the top output. Press “l” to toggle the first line on/off.

word-image-378.png

If your system is running a multi-core CPU, then the top can report information for individual cores. Press “1” to toggle information on a per-core basis.

word-image-379.png

Want to change the CPU displays? Press “t”. On the first press, it’ll change the graphs to ASCII.

word-image-380.png

On the second press, it’ll change the graphs to solid block characters.

word-image-381.png

Press “t” once more and the CPU display and task summary will completely disappear.

word-image-382.png

To do the same with the memory and swap memory, press “m”.

word-image-383.png

word-image-384.png

word-image-385.png

Highlighting

The default screen of the top is just black and white. How about spicing things up? Press “z” to colorize the display.

word-image-388.png

To highlight the running tasks, press “y”. If you press “x”, the top will highlight the columns that it uses to sort the process list. The bold and reversed text highlight can be toggled by pressing “b”.

word-image-391.png

Full command line

In the case of running processes, we can toggle between just the process name and process name along with the full command. Press “c” to toggle.

word-image-394.png

To see a tree-style view where processes were launched/spawned by other processes, press “V”.

word-image-395.png

Filter output by the user

By default, the top will show the processes from all the users. To see the processes running under a specific process, press “u”. The top will ask for the user name or UID.

word-image-396.png

Active tasks

To see only the active tasks, press “I”.

word-image-397.png

To revert the changes, press “I” again.

word-image-398.png

Number of processes to display

By default, the top will report all the processes in a long table. However, we can manually set the number of processes top will display irrespective of the number of processes running. I think it’s a useful trick in very specific scenarios.

To limit the displays, press “n”. The top will ask for the number of processes to display. By default, the value is 0 (unlimited).

word-image-399.png

Change process priority

Every single running process will have a nice value to them. This nice value determines the priority of the process among all the running processes. The top allows manually defining the nice value.

To manually determine the nice value of a process, press “r”. The top will ask for the PID.

word-image-400.png

After entering the PID, the top will ask for the new nice value.

word-image-401.png

Kill process

In case you want to manually terminate a process, the top allows you to send a termination signal. To send a termination signal, we’ll be needing the PID and the signal name.

Press “k”. The top will ask for the PID.

word-image-402.png

word-image-403.png

Once the PID is entered, the top will then ask for the signal to send. By default, it’ll be the SIGTERM (kill) signal. Most of the time, it’s the signal you want to send. For a complete list of all the available signals, check out the signal man page.

$ man 7 signal

word-image-404.png

Miscellaneous shortcuts

Whatever changes you’ve made to how top behaves, it won’t be loaded unless you save it. To save the current preset, press “W”.

By default, the top will refresh the information at the default value. To define your custom value, press “d”.

word-image-405.png

To perform an instant quick refresh, press Spacebar.

Final thoughts

This is just the tip of the iceberg. The top is a complicated yet powerful tool for monitoring your system. There are plenty of tutorials available online. Check them out if you want to master the top command.

Interested in monitoring the system I/O? Then iotop is what you’re looking for. Check out how to monitor disk I/O using iotop.

Happy computing!

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...