Monitoring System Resources: CPU, Memory, Disk, Ports, and Processes in Linux
CPU Utilization Monitoring
The top command provides a dynamic, real-time view of system processes and resource usage. Press q or Ctrl+C to exit.
The initial five lines present overall system statistics.
Line 1: System Summary
This line mirrors the output of uptime.
- Current time (e.g., 10:59:33).
- Uptime: System runtime in days, hours:minutes (e.g., up 88 days, 14:39).
- Logged-in users: Number of users currently logged in.
- Load averages: Average system load over the last 1, 5, and 15 minutes.
Lines 2 & 3: Process and CPU Statistics
- Tasks: Total number of processes.
- Running: Processes currently executing.
- Sleeping: Processes in a sleep state.
- Stopped: Processes that have been halted.
- Zombie: Defunct (zombie) processes.
CPU usage percentages are broken down as follows:
- us: Percentage of CPU time spent in user space.
- sy: Percentage of CPU time spent in kernel space.
- ni: Percentage of CPU time used by processes with altered priority (nice value).
- id: Percentage of idle CPU time.
- wa: Percentage of CPU time spent waiting for I/O operations.
- hi: Percentage of CPU time spent handling hardware interrupts.
- si: Percentage of CPU time spent handling software interrupts.
- st: Steal time percentage, indicating CPU time a virtual machine waits for the hypervisor. High values may suggest resource contention on virtualized systems.
Lines 4 & 5: Memory Information
- KiB Mem: Physical memory statistics.
total: Total installed memory.free: Unused memory.used: Memory currently in use.buff/cache: Memory used by kernel buffers and page cache.
- KiB Swap: Swap space statistics.
total: Total swap space.free: Unused swap space.used: Swap space currently utilized.avail Mem: An estimate of memory available for starting new applications without swapping.
Process List Details The lower section lists individual processes with columns including:
- PID: Process identifier.
- USER: Process owner.
- PR/NI: Priority and nice value.
- VIRT: Total virtual memory used (VIRT = SWAP + RES).
- RES: Resident (non-swapped) physical memory used.
- SHR: Shared memory size.
- S: Process state (e.g., R=Running, S=Sleeping, Z=Zombie).
- %CPU/%MEM: CPU and physical memory usage percentages.
- TIME+: Total CPU time consumed.
- COMMAND: Command name or line.
Checking Memory Usage
The free command displays system memory usage.
free
Key output fields:
- total: Total physical RAM.
- used: Memory currently in use.
- free: Unallocated memory.
- shared: Memory used by tmpfs (shared memory).
- buff/cache: Sum of memory used by kernel buffers and the page cache.
- available: Estimate of memory available for new applications.
Use flags for human-readable output:
free -h # Auto-scales units (KB, MB, GB)
free -g # Shows values in gigabytes
free -m # Shows values in megabytes
Monitoring Disk Space
The df command reports filesystem disk space usage.
df -h
Output columns:
- Filesystem: The disk partition or filesystem.
- Size: Total capacity of the partition.
- Used: Amount of space consumed.
- Avail: Free space available.
- Use%: Percentage of used space.
- Mounted on: The mount point directory.
Common df and du (disk usage) command variations:
df -hl # Lists free disk space
du -sh /path/to/dir # Shows total size of a specific directory
du -sm /path/to/dir # Shows directory size in megabytes
du -h /path/to/dir # Shows sizes of all items within a directory recursively
Port and Network Socket Inspection
To check if a specific port is in use:
netstat -anp | grep :3306
To list all listening TCP and UDP ports:
netstat -nultp
If a port is occupied, you can terminate the associated process using its PID.
Viewing System Processes
The ps command provides a snapshot of current processes.
To see all running processes:
ps aux
Common ps options for process discovery:
ps a: Shows processes from all users on the current terminal.ps -Aorps -e: Displays all process on the system.ps -forps -H: Displays a forest (tree) view showing parent-child process relationships.ps u: Outputs in a user-oriented format.ps x: Includes processes not attached to a terminal (daemons).ps -p <PID>: Shows details for a specific process ID.