htop Command ExamplesThe htop command in Linux is an interactive system-monitoring tool for viewing and managing processes, offering a user-friendly alternative to top. It displays CPU, memory, swap usage, and detailed process information. Below are 10 examples demonstrating various uses of the htop command with detailed explanations.
htop may not be installed by default. Install it using sudo apt install htop (Debian/Ubuntu) or sudo yum install htop (CentOS/RHEL).htop
Launches htop with default settings, displaying CPU, memory, swap usage, and a list of processes with columns for PID, user, priority, CPU/memory usage, and command.
Sample Output: Interactive interface with real-time updates. Use arrow keys to navigate, F10 or q to quit.
htop -u username
Displays only processes owned by the specified user (replace username with an actual username, e.g., john).
Sample Output: htop interface shows only processes for the specified user, useful for isolating user-specific resource usage.
Interactive Tip: Press F4 in htop to filter interactively by typing a keyword.
htop --sort-key CPU
Launches htop with processes sorted by CPU usage in descending order. Other sort keys include MEM, PID, TIME, etc.
Sample Output: Processes are displayed with the highest CPU-using process at the top.
Interactive Tip: Press F6 to select a sort column interactively.
htop --tree
Shows processes in a hierarchical tree view, grouping child processes under their parents, making it easier to see process relationships.
Sample Output: htop interface with processes indented to show parent-child relationships, e.g., a web server and its worker processes.
Interactive Tip: Press F5 to toggle tree view in htop.
htop
While not a direct command-line option, you can customize displayed columns interactively. Launch htop, press F2 (Setup), select "Columns," and add/remove columns like IO_READ, IO_WRITE, or COMMAND.
Sample Output: Updated htop interface with selected columns, e.g., showing I/O rates for processes.
Note: Changes are saved to ~/.config/htop/htoprc for future sessions.
htop
Launch htop, select a process with arrow keys, press F9, choose a signal (e.g., SIGTERM or SIGKILL), and confirm to terminate the process.
Sample Output: The selected process disappears from the htop interface after termination.
Warning: Use SIGKILL cautiously, as it forcefully terminates processes without cleanup.
htop
Launch htop, select a process with arrow keys, press F7 to increase priority (lower nice value) or F8 to decrease priority (higher nice value).
Sample Output: The PRI or NI column updates to reflect the new priority value.
Note: Requires sufficient permissions (e.g., root for some changes). Negative nice values increase priority, while positive values decrease it.
htop -d 50
Sets the refresh interval to 5 seconds (50 tenths of a second). This reduces CPU usage for htop on systems where frequent updates are unnecessary.
Sample Output: htop interface updates every 5 seconds instead of the default 2 seconds.
Interactive Tip: Press + or - in htop to adjust the refresh interval dynamically.
htop -U
Shows only processes owned by the current user, excluding system processes, for a cleaner view.
Sample Output: htop interface lists only the current user’s processes, e.g., user applications and scripts.
htop
Launch htop, customize settings (e.g., columns, colors, sort order) via F2 (Setup), and save by exiting setup. Settings are stored in ~/.config/htop/htoprc.
Sample Output: No output. Future htop sessions load the customized settings.
Tip: Backup htoprc to preserve settings across systems or resets.
Additional Tips:
F3 to search for processes, and F1 for help.~/.config/htop/htoprc manually for advanced tweaks, such as color schemes.-d) to reduce htop’s overhead.sudo htop to view all processes and perform actions like killing system processes.Note: htop is highly interactive and customizable. Check the man page (man htop) or press F1 in htop for additional options and keybindings specific to your version.