Skip to content

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)
htop

Output example:

%CPU COMMAND
25.5 /usr/bin/node server.js
15.2 /usr/lib/firefox/firefox
8.3 /usr/bin/python3 app.py

Real-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
uptime

Memory 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 -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           15Gi       8.2Gi       2.1Gi       500Mi       5.0Gi       6.5Gi
Swap:         2.0Gi       512Mi       1.5Gi

Swap Usage

bash
# Show swap usage
swapon --show

# Swap summary
cat /proc/swaps

# Clear swap cache (requires root)
sudo swapoff -a && sudo swapon -a

Disk 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 2

Disk Performance

bash
# I/O statistics
iotop

# Disk usage by inode
df -i

# Check filesystem
sudo fsck /dev/sda1

Process 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 -ef

Process 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 process

Network 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 -ant

Network 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
nethogs

Ping 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.com

System 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
who

System Information

bash
# System information
uname -a

# OS release information
cat /etc/os-release

# Kernel version
uname -r

# Hostname
hostname

# System uptime
uptime

Log 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 -f

Application 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/syslog

Performance 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
glances

Comprehensive Monitoring

bash
# Install glances
sudo apt install glances  # Debian/Ubuntu
sudo yum install glances  # RHEL/CentOS

# Run glances
glances

# Web interface
glances -w

Quick 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 -5

Monitoring 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
done

Memory 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}%"
fi

Performance 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
iftop

System Bottlenecks

  1. CPU bottleneck: top shows high CPU usage
  2. Memory bottleneck: High swap usage in free -h
  3. Disk bottleneck: High %util in iostat -x
  4. Network bottleneck: High bandwidth in iftop

See Also

External Resources

Released under the MIT License.