Fahd Aomari Last Edit :

Top command

Top

The top Command in Linux

Overview:top is a powerful command-line utility in Linux that provides a dynamic real-time view of a running system. It displays a detailed overview of system processes, including information about CPU and memory utilization.

Key Features of top:

  1. Dynamic Update: It refreshes the display at regular intervals to show the current state of the system.
  2. Process Information: Shows details about processes running on the system, including process ID, user, priority, CPU and memory usage.
  3. Resource Utilization: Displays CPU, memory, and swap usage.
  4. Task Summary: Provides a quick summary of the number of tasks and their states (running, sleeping, stopped, zombie).

Common Arguments:

  • d: Specifies the delay between screen updates. For example, top -d 5 updates every 5 seconds.
  • u: Shows processes by a specific user. For example, top -u username shows processes for username.
  • p: Monitors specific processes. For example, top -p PID where PID is the Process ID.
  • n: Limits the number of iterations. For example, top -n 10 will update 10 times and then exit.
  • b: Runs top in batch mode, useful for sending output to a file or another process.

Interactive Commands Inside top:

  • h or ?: Displays help.
  • k: Kills a process. You need to enter the PID of the process.
  • u: Filters processes by a specific user.
  • P: Sorts by CPU usage.
  • M: Sorts by memory usage.
  • q: Quits top.

Example Usage and Output:

Command:

top

Sample Output:

top - 15:30:05 up 10 min,  2 users,  load average: 0.42, 0.44, 0.27
Tasks: 162 total,   1 running, 160 sleeping,   0 stopped,   1 zombie
%Cpu(s):  5.1 us,  2.1 sy,  0.0 ni, 91.7 id,  0.8 wa,  0.0 hi,  0.3 si,  0.0 st
MiB Mem :   7822.0 total,   5068.3 free,   1703.3 used,   1050.4 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   5799.7 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1023 user      20   0  457904  64740  32020 S   6.2   0.8   0:03.89 gnome-shell
    1 root      20   0  170392  11344   8244 S   0.0   0.1   0:01.23 systemd
    [other processes...]

Explanation of Output:

  • Header: Shows the current time, system uptime, number of users, and load average.
  • Task Summary: Total number of tasks, their states (running, sleeping, etc.).
  • CPU Usage: Percentage of CPU used for user processes (us), system processes (sy), and idle (id).
  • Memory Info: Total, free, used, and buffer/cache memory.
  • Process List: Details of each process including PID, user, CPU, and memory usage.

Conclusion:

top is an invaluable tool for system administrators and power users. It gives a quick and detailed view of the system’s state, helping in diagnosing performance issues, monitoring resource usage, and managing system processes.