Understanding how to monitor and control processes is one of the most crucial skills for any Linux user or system administrator. Every program running on your system is a process, and knowing how to manage them effectively can mean the difference between a smoothly running system and one that's sluggish or unresponsive.
A process is simply a running instance of a program. When you open a text editor, start a web browser, or run a command in the terminal, you're creating processes. Each process has:
- Process ID (PID): A unique numerical identifier
The ps
(process status) command provides a snapshot of currently running processes.
# Show processes for current user
ps
# Show all processes with detailed information
ps aux
# Show processes in tree format
ps auxf
# Show specific user's processes
ps -u username
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 168576 11904 ? Ss 09:00 0:01 /sbin/init
john 1234 2.5 1.2 487216 98432 ? Sl 09:15 0:45 firefox
- USER: Process owner
While ps
gives you a snapshot, top
provides real-time, continuously updated information about system processes.
# Start top
top
# Sort by CPU usage (default)
# Press 'M' to sort by memory usage
# Press 'P' to sort by CPU usage
# Press 'q' to quit
- Space: Refresh display
htop
is an improved version of top
with a more user-friendly interface, colors, and mouse support.
# Install htop (if not already installed)
sudo apt install htop # Ubuntu/Debian
sudo yum install htop # CentOS/RHEL
sudo pacman -S htop # Arch Linux
# Run htop
htop
- Colorful interface: Easy to read process information
The kill
command sends signals to processes. Despite its name, it doesn't always terminate processesโit sends various types of signals.
# Graceful termination (SIGTERM)
kill PID
# Force termination (SIGKILL)
kill -9 PID
kill -KILL PID
# Hang up signal (SIGHUP) - often used to reload configs
kill -HUP PID
# List all available signals
kill -l
# Kill all processes with specific name
killall firefox
# Kill all processes matching pattern
pkill -f "python script.py"
# Interactive process killing
sudo pkill -u username
Linux allows you to run jobs in the background, freeing up your terminal for other tasks.
# List active jobs
jobs
# List jobs with PID
jobs -l
# List only running jobs
jobs -r
# List only stopped jobs
jobs -s
# Start a command in background
command &
# Move current job to background
# First stop with Ctrl+Z, then:
bg
# Move specific job to background
bg %1 # where 1 is job number
# Bring most recent background job to foreground
fg
# Bring specific job to foreground
fg %1 # where 1 is job number
# Find processes using most CPU
ps aux --sort=-%cpu | head -10
# Find processes using most memory
ps aux --sort=-%mem | head -10
# Find specific process
ps aux | grep firefox
# Get PID of process by name
pgrep firefox
# Start a long-running process in background
./backup-script.sh &
# Start process that survives terminal closure
nohup ./long-running-script.sh &
# Check if background job is still running
jobs
# Bring it to foreground to check progress
fg %1
# First, try graceful termination
kill $(pgrep firefox)
# If that doesn't work, force kill
kill -9 $(pgrep firefox)
# Kill all processes by user
sudo pkill -u problematic-user
# Kill all processes of specific type
sudo killall -9 zombie-process
# Monitor system load
uptime
# Watch memory usage
free -h
# Monitor disk I/O
iostat
# Real-time system monitoring
htop
# Monitor specific process
watch -n 1 'ps aux | grep mysql'
Every process has a priority level called "nice value" ranging from -20 (highest priority) to 19 (lowest priority).
# Start process with lower priority
nice -n 10 ./cpu-intensive-task.sh
# Change priority of running process
renice 5 -p PID
# Start with high priority (requires sudo)
sudo nice -n -10 ./important-task.sh
- R (Running): Currently executing or ready to execute
The load average shows system activity level:
# Check load average
uptime
# Output: load average: 1.23, 1.45, 1.67 (1min, 5min, 15min)
# Load interpretations:
# < 1.0: System has spare capacity
# = 1.0: System is fully utilized
# > 1.0: System is overloaded
# Find all Firefox processes
ps aux | grep firefox
# Count total number of processes
ps aux | wc -l
# Find processes consuming more than 5% CPU
ps aux | awk '$3 > 5.0 {print $0}'
# Start a simple background job
sleep 300 &
# Start multiple background jobs
sleep 200 & sleep 400 & sleep 600 &
# List all jobs
jobs
# Kill specific background job
kill %2 # Kills second job
# Bring job to foreground and stop it
fg %1
# Press Ctrl+Z to stop
# Then: kill %1
#!/bin/bash
# Create a simple system monitor
echo "=== System Process Monitor ==="
echo "Top 5 CPU consumers:"
ps aux --sort=-%cpu | head -6
echo -e "\nTop 5 Memory consumers:"
ps aux --sort=-%mem | head -6
echo -e "\nSystem Load:"
uptime
echo -e "\nMemory Usage:"
free -h
# Identify CPU-intensive processes
top -o %CPU
# Monitor specific process
top -p PID
# Reduce process priority
renice 10 -p PID
# Monitor memory usage over time
watch -n 5 'ps aux --sort=-%mem | head -10'
# Find processes with high memory growth
ps aux | sort -nrk 6 | head -10
# Find zombie processes
ps aux | grep -i zombie
# Find parent of zombie process
ps -o pid,ppid,state,comm
# Kill parent process (careful!)
kill PPID
1. Always try SIGTERM first: kill PID
2. Use SIGKILL only when necessary: kill -9 PID
3. Be careful with sudo: Double-check PIDs before killing
4. Never kill PID 1: This is the init process
# Monitor processes by unknown users
ps aux | grep -v "^$(whoami)"
# Check for processes with unusual network activity
netstat -tulpn
# Monitor failed login attempts
last -f /var/log/btmp
1. Regular Monitoring: Use htop
or top
regularly to understand normal system behavior
2. Process Cleanup: Regularly check for and terminate unnecessary processes
3. Resource Limits: Set appropriate ulimits for users and processes
4. Automation: Create scripts to monitor and alert on unusual process activity
1. Killing system processes: Always verify what a process does before killing it 2. Ignoring resource usage: Monitor CPU and memory usage regularly 3. Not using job control: Learn to use bg/fg for better terminal productivity 4. Force killing everything: Try graceful termination first
- ps: Take snapshots of running processes
Now that you understand process management, you're ready to learn about user and group managementโanother crucial aspect of Linux system administration. Understanding processes helps you monitor what's happening on your system, while user management helps you control who can run what processes.
# Process Monitoring
ps aux # List all processes
top # Real-time process monitor
htop # Enhanced process monitor
pgrep name # Find PID by process name
# Process Control
kill PID # Terminate process gracefully
kill -9 PID # Force terminate process
killall name # Kill all processes by name
pkill pattern # Kill processes matching pattern
# Job Control
command & # Run command in background
jobs # List active jobs
bg %1 # Move job 1 to background
fg %1 # Bring job 1 to foreground
Ctrl+Z # Stop current process
Remember: With great power comes great responsibility. Always understand what a process does before terminating it, and prefer graceful shutdowns over force kills whenever possible.
---
This is Part 9 of our comprehensive Linux mastery series.
Previous: User & Group Management - Master user administration and access control
Next: Environment Variables - Learn to configure system behavior
Intermediate Skills:
Ready for Environment Configuration? Continue with environment variables to customize your system behavior!
---
Ready to control who can run what on your system? Next, we'll explore user and group management to secure and organize your Linux environment.