Using top and htop - System Monitoring in Linux
Introduction
System monitoring is a critical aspect of managing and maintaining a stable and efficient Linux environment. Two powerful tools for this purpose are top and htop. These tools provide real-time insights into system performance, resource usage, and running processes.
Using top
The top command is a built-in utility in Linux systems that provides a dynamic, real-time view of system processes. It displays system summary information as well as a list of tasks currently being managed by the kernel.
Basic Usage
To start top, simply open your terminal and type:
Understanding the Output
The output of top can be divided into two main sections:
- System Summary: Provides information about system uptime, load average, tasks, CPU, memory, and swap usage.
- Task List: Displays a list of running processes, including details such as PID, user, priority, nice value, virtual memory, resident memory, shared memory, state, CPU usage, memory usage, and command.
Common Interactions
While top is running, you can interact with it using various commands:
- k: Kill a process by entering its PID.
- r: Renice a process by entering its PID and new nice value.
- q: Quit top.
- h: Display the help screen.
- 1: Toggle the display of individual CPU cores.
Using htop
htop is an interactive process viewer for Unix systems. It is a more user-friendly and visually appealing alternative to top. To use htop, it needs to be installed on your system.
Installing htop
To install htop, use the package manager for your distribution:
sudo yum install htop (for CentOS/RHEL)
sudo dnf install htop (for Fedora)
Launching htop
To start htop, open your terminal and type:
Understanding the Interface
The htop interface is divided into three sections:
- Header: Displays system metrics such as CPU, memory, and swap usage along with load average and uptime.
- Process List: Shows a list of running processes with details such as PID, user, CPU usage, memory usage, time, and command.
- Footer: Provides function key shortcuts for various actions like killing a process, renicing, searching, filtering, and more.
Common Interactions
While htop is running, you can interact with it using function keys:
- F1: Help
- F2: Setup
- F3: Search for a process
- F4: Filter processes
- F5: Tree view
- F6: Sort by column
- F9: Kill a process
- F10: Quit htop
Conclusion
Both top and htop are powerful tools for monitoring system performance and managing processes in a Linux environment. While top is available by default on most distributions and provides essential monitoring capabilities, htop offers a more user-friendly and interactive experience. By leveraging these tools, you can gain valuable insights into your system's behavior and ensure its optimal performance.