Chapter 4.2 - Process Visualization
Learning Objectives
By the end of this chapter, you will be able to:
- Use ps effectively
- Monitor with top and htop
- Understand the displayed information
- Filter and sort processes
ps - Process Listing
Basic Syntax
# All processes
ps aux
# Current user's processes
ps
# BSD format
ps aux
# Standard format
ps -ef
Useful Options
# Custom format
ps -eo pid,ppid,user,cmd,%mem,%cpu,etime
# By user
ps -u user1
ps aux | grep user1
# By command
ps aux | grep apache
# Process tree
ps auxf
ps -ejH
ps --forest
# Processes from a terminal
ps -t tty1
Important Columns
- PID: Process ID
- PPID: Parent Process ID
- USER: Owner
- %CPU: CPU usage
- %MEM: Memory usage
- STAT: State (R, S, T, Z, D)
- START: Start time
- TIME: CPU time used
- COMMAND: Full command
top - Real-Time Monitoring
Interactive Interface
top
Header displays:
- System time and uptime
- Number of users
- Load average
- Process states
- CPU and memory usage
Body displays:
- List of processes
- Sorted by CPU (default)
Commands in top
# Navigation
h # Help
q # Quit
Space # Refresh immediately
# Sorting
P # By CPU (default)
M # By memory
T # By CPU time
N # By PID
k # Kill a process (asks for PID)
r # Renice (change priority)
# Filters
u # Filter by user
o # Filter by criteria
= # Reset filters
# Display
1 # Show all CPUs
f # Choose columns
W # Save configuration
Command Line Options
# Custom update
top -d 5 # Every 5 seconds
# By user
top -u user1
# Specific process
top -p 1234,5678
# Batch mode (for scripts)
top -b -n 1 > processes.txt
htop - Enhanced Version
Installation
# Ubuntu/Debian
sudo apt install htop
# Red Hat/CentOS
sudo yum install htop
# or
sudo dnf install htop
Advantages
- Colorful and intuitive interface
- Arrow key navigation
- Easy filtering
- Visual process tree
- Progress bars
Usage
htop
Shortcuts:
F2: SetupF3: SearchF4: FilterF5: Tree viewF6: SortF9: Kill processF10: QuitSpace: Mark process
Other Tools
pstree - Process Tree
# Simple tree
pstree
# With PIDs
pstree -p
# With users
pstree -u
# By user
pstree -u user1
pgrep - Search by Name
# Find PID by name
pgrep apache
pgrep -u user1 bash
# Show name too
pgrep -l apache
# Count
pgrep -c apache
pidof - Program PID
# Program PID
pidof apache2
pidof -s apache2 # Single PID
Detailed Information
/proc/[PID]/
Directory with information about each process:
# View process info
cat /proc/1234/status
cat /proc/1234/cmdline
cat /proc/1234/environ
cat /proc/1234/io # I/O
cat /proc/1234/fd/ # Open files
stat and status
# Detailed status
cat /proc/1234/status
# Raw information
cat /proc/1234/stat
Summary
In this chapter, you learned:
ps: Process listing, numerous options
top: Real-time monitoring, interactive interface
htop: Enhanced version, more user-friendly
pstree: Process tree
pgrep/pidof: Search processes by name
/proc/: Detailed process information
Next Steps
Chapter 4.3: Process Control
Chapter 4.4: Priorities and Nice Values
Chapter created on: December 2024