Linux System Monitoring
Essential commands for monitoring Linux system performance, processes, and resource usage.
CPU Monitoring
Top 10 Processes by CPU Usage
bash
# Using ps command
ps -A --format=pcpu,args --sort=-pcpu | head -n 11 | cut -c 1-100
# Alternative: using ps with custom columns
ps aux --sort=-pcpu | head -n 11
# Using top (interactive)
top
# Using htop (if installed, more user-friendly)
htopOutput example:
%CPU COMMAND
25.5 /usr/bin/node server.js
15.2 /usr/lib/firefox/firefox
8.3 /usr/bin/python3 app.pyReal-time CPU Monitoring
bash
# Interactive process viewer
top
# Better interactive viewer (requires installation)
htop
# Monitor specific process
top -p <PID>
# Show CPU usage per core
mpstat -P ALL 1
# Watch CPU usage (updates every 2 seconds)
watch -n 2 'ps -A --sort=-pcpu | head -10'CPU Information
bash
# CPU details
lscpu
# Number of CPU cores
nproc
# CPU model and specs
cat /proc/cpuinfo
# CPU usage summary
uptimeMemory Monitoring
Memory Usage
bash
# Memory and swap usage (human-readable)
free -h
# Detailed memory information
cat /proc/meminfo
# Show memory usage of processes
ps aux --sort=-rss | head -n 11
# Real-time memory monitoring
watch -n 1 free -hExample output:
total used free shared buff/cache available
Mem: 15Gi 8.2Gi 2.1Gi 500Mi 5.0Gi 6.5Gi
Swap: 2.0Gi 512Mi 1.5GiSwap Usage
bash
# Show swap usage
swapon --show
# Swap summary
cat /proc/swaps
# Clear swap cache (requires root)
sudo swapoff -a && sudo swapon -aDisk Monitoring
Disk Usage
bash
# Disk space usage (human-readable)
df -h
# Disk usage by directory
du -sh /*
# Disk usage of current directory
du -sh .
# Top 10 largest directories
du -h / 2>/dev/null | sort -rh | head -n 10
# Disk I/O statistics
iostat
# Monitor disk I/O in real-time
iostat -x 2Disk Performance
bash
# I/O statistics
iotop
# Disk usage by inode
df -i
# Check filesystem
sudo fsck /dev/sda1Process Monitoring
List Processes
bash
# All processes
ps aux
# All processes in tree format
ps auxf
# Processes by specific user
ps -u username
# Processes by command name
ps aux | grep nginx
# Process tree
pstree
# Detailed process information
ps -efProcess Details
bash
# Information about specific process
ps -p <PID> -o pid,ppid,cmd,%mem,%cpu
# Process file descriptors
lsof -p <PID>
# Process threads
ps -eLf | grep <PID>
# Process environment variables
cat /proc/<PID>/environ | tr '\0' '\n'
# Process current directory
pwdx <PID>Kill Processes
bash
# Kill process by PID
kill <PID>
# Force kill
kill -9 <PID>
# Kill by name
pkill nginx
# Kill all processes by name
killall nginx
# Interactive process killer
htop # Press F9 to kill selected processNetwork Monitoring
Network Connections
bash
# Active network connections
netstat -tulpn
# Alternative using ss (faster)
ss -tulpn
# Established connections
ss -tun state established
# Listen ports
ss -tlnp
# All TCP connections
netstat -antNetwork Traffic
bash
# Real-time network usage
iftop
# Network statistics
netstat -s
# Interface statistics
ip -s link
# Monitor specific interface
iftop -i eth0
# Bandwidth monitoring
nload
# Network usage by process
nethogsPing and Connectivity
bash
# Ping host
ping google.com
# Ping with count
ping -c 4 google.com
# Trace route
traceroute google.com
# DNS lookup
nslookup google.com
dig google.comSystem Load
Load Average
bash
# System load
uptime
# Load average (1, 5, 15 minutes)
cat /proc/loadavg
# System load and running processes
w
# Who is logged in
whoSystem Information
bash
# System information
uname -a
# OS release information
cat /etc/os-release
# Kernel version
uname -r
# Hostname
hostname
# System uptime
uptimeLog Monitoring
System Logs
bash
# View system log
sudo tail -f /var/log/syslog
# View authentication log
sudo tail -f /var/log/auth.log
# View kernel messages
dmesg
# Follow kernel messages
dmesg -w
# System journal (systemd)
journalctl -f
# Journal for specific service
journalctl -u nginx.service -fApplication Logs
bash
# Apache logs
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
# Nginx logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
# Last 100 lines
tail -n 100 /var/log/syslogPerformance Tools
System-wide Monitoring
bash
# Interactive process viewer
top
# Enhanced process viewer
htop
# System activity report
sar
# Virtual memory statistics
vmstat 1
# I/O statistics
iostat -x 1
# All-in-one monitoring
glancesComprehensive Monitoring
bash
# Install glances
sudo apt install glances # Debian/Ubuntu
sudo yum install glances # RHEL/CentOS
# Run glances
glances
# Web interface
glances -wQuick Checks
System Health Check
bash
# One-liner system health check
echo "Load: $(uptime | awk '{print $(NF-2),$(NF-1),$NF}')" && \
echo "Memory: $(free -h | grep Mem | awk '{print $3 "/" $2}')" && \
echo "Disk: $(df -h / | tail -1 | awk '{print $3 "/" $2 " (" $5 ")"}')"Top Resource Consumers
bash
# Top 5 CPU processes
ps aux --sort=-pcpu | head -6
# Top 5 memory processes
ps aux --sort=-rss | head -6
# Top 5 disk usage directories
du -h /home 2>/dev/null | sort -rh | head -5Monitoring Scripts
CPU Alert Script
bash
#!/bin/bash
# cpu_alert.sh - Alert when CPU usage is high
THRESHOLD=80
while true; do
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
CPU_INT=${CPU_USAGE%.*}
if [ $CPU_INT -gt $THRESHOLD ]; then
echo "WARNING: CPU usage is ${CPU_USAGE}%"
fi
sleep 60
doneMemory Alert Script
bash
#!/bin/bash
# memory_alert.sh - Alert when memory usage is high
THRESHOLD=80
MEMORY_USAGE=$(free | grep Mem | awk '{print ($3/$2) * 100}')
MEMORY_INT=${MEMORY_USAGE%.*}
if [ $MEMORY_INT -gt $THRESHOLD ]; then
echo "WARNING: Memory usage is ${MEMORY_USAGE}%"
fiPerformance Analysis
Identify Performance Issues
bash
# Check for high load
uptime
# Find process causing high CPU
top -o %CPU
# Find process causing high memory
top -o %MEM
# Check disk I/O
iotop
# Check network usage
iftopSystem Bottlenecks
- CPU bottleneck:
topshows high CPU usage - Memory bottleneck: High swap usage in
free -h - Disk bottleneck: High %util in
iostat -x - Network bottleneck: High bandwidth in
iftop
See Also
- Linux Service Management - Managing system services
- Linux File Operations - File and directory commands
- Cross-Platform Commands - Commands across OSes