CIS120 Linux Fundamentals by Scott Shaper

ps and top Commands

Think of the ps and top commands like two different ways to check what's happening in a busy kitchen. The ps command is like taking a quick snapshot of the kitchen at one moment - you can see all the dishes being prepared, who's cooking what, and how much counter space each chef is using. The top command is more like watching a live security camera feed of the kitchen - you see everything happening in real-time, with constant updates about which dishes are taking the most resources. These commands help you understand what's running on your system and how resources are being used.

Quick Reference

Command What It Does Common Use
ps Shows a snapshot of current processes Getting a quick list of running programs
ps aux Shows detailed info about all processes Troubleshooting or finding resource-heavy programs
top Shows real-time, updating process information Monitoring system performance over time
top -u username Shows processes for a specific user Checking what a particular user is running

When to Use These Commands

The ps Command

Think of ps as a camera that takes a quick snapshot of all the programs (processes) running on your computer at that exact moment. Unlike top, which keeps updating, ps just gives you a single report and then exits. This makes it perfect for quickly checking what's running or for use in scripts.

The name ps stands for "process status," and it's one of the most commonly used commands for system monitoring. By default, the basic ps command only shows processes running in your current terminal, but with options, you can see everything running on your system.

Option What It Does When to Use
-e Shows all processes When you need to see everything running on the system
-f Full format listing with more details When you need to see parent-child relationships between processes
-u username Shows processes for specific user When you only care about what a particular user is running
-l Long format with extra details When you need priority and state information
aux Shows all processes with detailed info When you need comprehensive process information (most common)
--sort=field Sorts output by a specific field When you want to find the highest CPU or memory users

Practical Examples

# Basic process view (only shows processes in your current terminal)
ps
# Output shows simple info like PID, TTY, TIME, and CMD

# See all processes on the system
ps -e  
# Lists every process running on the system

# See detailed information about all processes (most common usage)
ps aux
# Shows USER, PID, %CPU, %MEM, and more for every process

# Find all processes owned by a specific user
ps -u your_username
# Shows only processes belonging to that user

# Sort processes by CPU usage (highest first)
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu
# Great for finding what's consuming most CPU

# Sort processes by memory usage (highest first)
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem
# Helps identify memory hogs

# See process tree (parent-child relationships)
ps -ef --forest
# Shows processes in a tree-like format

Understanding ps Output

When you run ps aux, you'll see a table with many columns. These are the most important ones to understand:

Column What It Shows Why It's Important
USER Who owns the process Helps identify whose programs are running
PID Process ID (unique number) Required when you need to stop or manage a process
%CPU Percentage of CPU being used Shows how computationally intensive the process is
%MEM Percentage of memory being used Shows how much RAM the process is consuming
VSZ Virtual memory size (KB) Shows total memory allocated to the process
RSS Resident Set Size (actual memory used) Shows actual physical memory the process is using
STAT Process state (running, sleeping, etc.) Tells you if the process is active, waiting, or stopped
START When the process started Helps identify long-running or recently started processes
TIME CPU time used by the process Shows total processing time consumed
COMMAND Command that started the process Shows what program the process belongs to

Common Process States

The STAT column in ps output uses codes to show the state of each process. Here are the most important ones to know:

Code What It Means Real-World Comparison
R Running or runnable A chef actively cooking a dish
S Sleeping (waiting for something) A chef waiting for water to boil
D Uninterruptible sleep (usually I/O) A chef focused on a delicate task who can't be disturbed
Z Zombie (completed but not cleaned up) A finished dish that no one has cleared away
T Stopped or being traced A chef who's been told to pause what they're doing

The top Command

Think of top as a live, constantly updating dashboard for your computer. Unlike ps, which gives you a one-time snapshot, top refreshes every few seconds to show you what's happening in real-time. It's like having a monitoring screen that shows which programs are currently using the most resources.

The top command is interactive, meaning you can use keyboard commands while it's running to change how information is displayed, sort by different columns, or even manage processes directly. This makes it a powerful tool for system monitoring and troubleshooting.

Option What It Does When to Use
-d seconds Changes update frequency When you want slower or faster updates than the default
-u username Shows only a specific user's processes When monitoring what a particular user is running
-p PID Monitors specific process IDs When you only care about watching certain programs
-n number Exit after a certain number of updates When you want top to run for a limited time
-b Run in batch mode (for logging) When saving output to a file for later analysis
-o field Sort by a specific field When you want to focus on CPU, memory, or other metrics

Practical Examples

# Run top with default settings
top
# Shows real-time process information, refreshing every 3 seconds

# Update every 10 seconds instead of the default
top -d 10
# Slows down the refresh rate

# Show only processes from a specific user
top -u your_username
# Filters to only show your processes

# Monitor specific process IDs
top -p 1234,5678
# Only shows processes with those PIDs

# Run top once and save to a file
top -b -n 1 > system_snapshot.txt
# Useful for logging system state

# Sort processes by memory usage instead of CPU
top -o %MEM
# Shows memory-hungry processes at the top

# While top is running, press these keys for different actions:
# 'q' - Quit top
# 'k' - Kill a process (will prompt for PID)
# 'r' - Renice a process (change its priority)
# 'f' - Add/remove fields from display
# 'u' - Filter by user
# 'M' - Sort by memory usage
# 'P' - Sort by CPU usage (default)

Understanding top Display

The top display is divided into two main parts: the summary area at the top and the process list below it. Let's break down what you're seeing:

Summary Area

Line What It Shows Why It's Important
Line 1 Time, uptime, users, load average Quick overview of system status and how busy it is
Line 2 Task/process summary Shows how many processes are in each state
Line 3 CPU usage breakdown Shows how CPU time is being used (user, system, idle)
Line 4 Memory usage Shows physical memory allocation
Line 5 Swap usage Shows virtual memory usage

The load average numbers (like 0.15, 0.21, 0.18) tell you how busy your system is. Think of them as the number of processes waiting in line for the CPU. Numbers below 1.0 generally mean your system has capacity to spare, while higher numbers might indicate that your system is under stress.

Process List

The process list in top shows similar information to ps, but it's constantly updating and allows interactive control. The most important columns are %CPU and %MEM, which show you which processes are using the most resources.

Tips for Success

Common Mistakes to Avoid

Best Practices

Practical Monitoring Scenarios

Troubleshooting a Slow System

# First, check overall system load
top

# Look at the load average in the first line
# If it's consistently above 1.0 per CPU core, the system is under stress

# Sort by CPU usage to find resource hogs
# Press 'P' while in top, or use:
ps aux --sort=-%cpu | head -10
# Shows the top 10 CPU-consuming processes

# Check memory usage
# Press 'M' while in top, or use:
ps aux --sort=-%mem | head -10
# Shows the top 10 memory-consuming processes

# If you find a suspicious process, get more info
ps -p PROCESS_ID -f
# Shows detailed info about that specific process

# Check if the system is swapping heavily
free -m
# If swap 'used' is high and changing, this could explain slowness

Monitoring Multiple Systems

# Create a simple monitoring script
echo '#!/bin/bash
echo "System: $(hostname) - $(date)"
echo "Load Average: $(uptime | cut -d "," -f 3-5)"
echo "Top 5 CPU-consuming processes:"
ps aux --sort=-%cpu | head -6
echo "Memory Usage:"
free -m
echo "------------------------------"
' > monitor.sh

# Make it executable
chmod +x monitor.sh

# Run it on multiple systems or schedule with cron
./monitor.sh > system1_status.txt
# Or send the output to yourself by email with:
./monitor.sh | mail -s "System Status Report" your@email.com