SyntaxStudy
Sign Up
Linux / Bash Viewing and Managing Processes with ps and top
Linux / Bash Beginner 1 min read

Viewing and Managing Processes with ps and top

A process is a running instance of a program. Linux assigns each process a unique process ID (PID) and tracks its state, resource consumption, parent-child relationships, and associated user. Understanding how to inspect and manage processes is essential for system administration and performance troubleshooting. The `ps` command takes a snapshot of current processes. The most common invocation, `ps aux`, shows all processes from all users in a detailed format. The columns include user, PID, CPU%, memory%, virtual memory size, resident set size, terminal, state, start time, CPU time, and the command. The BSD-style flags (`aux`) do not require a leading dash. The `top` command provides a real-time, continuously updating view of system resource usage. The header shows uptime, user count, load averages, CPU breakdown, and memory statistics. The process list is sorted by CPU usage by default. Press `M` to sort by memory, `k` to kill a process, `r` to renice, and `q` to quit. `htop` is a modern alternative with colour output and mouse support.
Example
# List all running processes (BSD style)
ps aux

# List processes in forest/tree view
ps auxf

# List processes for current user
ps -u $USER

# Find a specific process
ps aux | grep nginx
pgrep nginx           # Returns just the PID(s)
pgrep -l nginx        # Returns PID and name

# Show process tree
pstree
pstree -p             # Include PIDs
pstree -u             # Include usernames

# ---- top ----
top
# Keyboard shortcuts inside top:
# q       quit
# M       sort by memory
# P       sort by CPU (default)
# k       kill process (enter PID)
# r       renice process
# 1       toggle per-CPU display
# H       show threads
# u       filter by username

# ---- htop (install: sudo apt install htop) ----
htop
# F5 = tree view | F6 = sort | F9 = kill | F10 = quit

# ---- Process states ----
# R  Running
# S  Sleeping (interruptible)
# D  Sleeping (uninterruptible, usually I/O wait)
# Z  Zombie (finished but parent hasn't collected exit status)
# T  Stopped / traced

# Watch a command's resource use in real time
watch -n 1 'ps aux --sort=-%cpu | head -15'