Appearance
Linux Process Management Cheat Sheet
Overview
Linux process management involves monitoring, controlling, and optimizing running processes and system resources. This comprehensive guide covers process monitoring tools, signal handling, job control, system resource management, and performance optimization techniques essential for system administrators and IT professionals.
⚠️ Warning: Process termination commands like
kill -9
can cause data loss. Always try graceful termination first and ensure proper backups.
Process Viewing and Monitoring
Basic Process Information
bash
# Display running processes
ps
ps aux # All processes with detailed info
ps -ef # Full format listing
ps -u username # Processes for specific user
ps -p PID # Specific process by PID
# Process tree view
pstree
pstree -p # Show PIDs
pstree username # Processes for specific user
# Real-time process monitoring
top
htop # Enhanced interactive process viewer
Advanced Process Monitoring
bash
# Detailed process information
ps auxf # Process tree format
ps -eo pid,ppid,cmd,pcpu,pmem # Custom columns
# Process by resource usage
ps aux --sort=-pcpu # Sort by CPU usage
ps aux --sort=-pmem # Sort by memory usage
# Long-running processes
ps -eo pid,etime,cmd # Show elapsed time
# Zombie processes
ps aux | grep -w Z
ps -eo stat,pid,cmd | grep -w Z
Real-time System Monitoring
bash
# Interactive process monitor
top
# Top commands:
# k - kill process
# r - renice process
# M - sort by memory
# P - sort by CPU
# q - quit
# Enhanced process monitor
htop
# htop features:
# F9 - kill process
# F7/F8 - nice/renice
# F6 - sort options
# F10 - quit
# System activity reporter
sar -u 1 10 # CPU usage every 1 second, 10 times
sar -r 1 10 # Memory usage
sar -d 1 10 # Disk activity
Process Control and Signals
Signal Types
bash
# Common signals:
# SIGTERM (15) - Graceful termination
# SIGKILL (9) - Force kill (cannot be caught)
# SIGHUP (1) - Hangup (reload config)
# SIGSTOP (19) - Stop process
# SIGCONT (18) - Continue stopped process
# SIGINT (2) - Interrupt (Ctrl+C)
Killing Processes
bash
# Kill by PID
kill PID
kill -15 PID # Send SIGTERM (default)
kill -9 PID # Force kill with SIGKILL
kill -HUP PID # Send hangup signal
# Kill by process name
killall process_name
killall -9 process_name
# Kill by pattern
pkill pattern
pkill -f pattern # Match full command line
pkill -u username # Kill user's processes
# Interactive process killer
top # Press 'k' then enter PID
htop # Press F9 then select process
Process Priority and Nice Values
bash
# View process priorities
ps -eo pid,ni,cmd
# Start process with specific priority
nice -n 10 command # Lower priority (+10)
nice -n -5 command # Higher priority (-5)
# Change running process priority
renice 10 PID # Set nice value to 10
renice -5 -p PID # Set nice value to -5
renice 5 -u username # Renice all user processes
Job Control
Background and Foreground Jobs
bash
# Run command in background
command &
# List active jobs
jobs
jobs -l # Show PIDs
# Bring job to foreground
fg
fg %1 # Bring job 1 to foreground
# Send job to background
bg
bg %1 # Send job 1 to background
# Suspend current job
Ctrl+Z
# Resume suspended job
fg # Resume in foreground
bg # Resume in background
Persistent Processes
bash
# Run command immune to hangups
nohup command &
nohup command > output.log 2>&1 &
# Detach from terminal
disown %1 # Disown job 1
disown -a # Disown all jobs
# Screen sessions
screen # Start new session
screen -S name # Named session
screen -r # Reattach to session
screen -ls # List sessions
# Tmux sessions
tmux # Start new session
tmux new -s name # Named session
tmux attach -t name # Attach to session
tmux list-sessions # List sessions
System Resource Monitoring
CPU Monitoring
bash
# CPU usage overview
top
htop
vmstat 1 # Virtual memory statistics
iostat 1 # I/O and CPU statistics
# Per-CPU statistics
mpstat # Multi-processor statistics
mpstat -P ALL 1 # All CPUs every second
# CPU load averages
uptime
w # Who is logged in and load
cat /proc/loadavg # Load average file
Memory Monitoring
bash
# Memory usage
free
free -h # Human readable
free -m # In megabytes
free -s 1 # Update every second
# Detailed memory information
cat /proc/meminfo
vmstat 1 # Virtual memory stats
# Memory usage by process
ps aux --sort=-pmem | head -10
top -o %MEM # Sort by memory in top
# Memory map of process
pmap PID
pmap -d PID # Detailed mapping
Disk I/O Monitoring
bash
# Disk usage
df -h # Filesystem usage
du -sh /path # Directory usage
# I/O statistics
iostat
iostat -x 1 # Extended stats every second
iotop # Real-time I/O monitoring
# Disk activity by process
iotop -o # Only show active processes
pidstat -d 1 # Per-process I/O stats
Network Monitoring
bash
# Network connections
netstat -tuln # TCP/UDP listening ports
ss -tuln # Modern replacement for netstat
ss -p # Show process names
# Network activity
netstat -i # Interface statistics
iftop # Real-time network usage
nethogs # Network usage by process
# Network connections by process
lsof -i # Network files
lsof -i :80 # Processes using port 80
Process Analysis and Debugging
Process Information
bash
# Detailed process info
cat /proc/PID/status
cat /proc/PID/cmdline
cat /proc/PID/environ
# Process file descriptors
lsof -p PID
ls -la /proc/PID/fd/
# Process memory maps
cat /proc/PID/maps
pmap PID
# Process threads
ps -T -p PID
top -H -p PID
System Calls and Debugging
bash
# Trace system calls
strace command
strace -p PID # Attach to running process
strace -f command # Follow forks
# Library calls
ltrace command
ltrace -p PID
# Debug running process
gdb -p PID
Performance Analysis
bash
# Process CPU usage over time
pidstat 1 # Per-process statistics
pidstat -p PID 1 # Specific process
# Memory usage tracking
pidstat -r 1 # Memory statistics
pidstat -d 1 # Disk I/O statistics
# System performance overview
vmstat 1 10 # Virtual memory stats
sar -A # All system activity
System Services and Daemons
Systemd Service Management
bash
# Service status
systemctl status service_name
systemctl is-active service_name
systemctl is-enabled service_name
# Start/stop services
systemctl start service_name
systemctl stop service_name
systemctl restart service_name
systemctl reload service_name
# Enable/disable services
systemctl enable service_name
systemctl disable service_name
# List services
systemctl list-units --type=service
systemctl list-units --failed
Service Logs
bash
# View service logs
journalctl -u service_name
journalctl -u service_name -f # Follow logs
journalctl -u service_name --since today
# System logs
journalctl
journalctl -f # Follow system logs
journalctl --since "1 hour ago"
journalctl -p err # Error messages only
Process Automation and Scheduling
Cron Jobs
bash
# Edit crontab
crontab -e
crontab -l # List cron jobs
crontab -r # Remove all cron jobs
# System cron directories
ls /etc/cron.d/
ls /etc/cron.daily/
ls /etc/cron.hourly/
ls /etc/cron.weekly/
ls /etc/cron.monthly/
# Cron log
tail -f /var/log/cron
At Jobs (One-time scheduling)
bash
# Schedule one-time job
at 15:30
at now + 1 hour
at tomorrow
# List scheduled jobs
atq
at -l
# Remove scheduled job
atrm job_number
Troubleshooting Common Issues
High CPU Usage
bash
# Find CPU-intensive processes
top -o %CPU
ps aux --sort=-pcpu | head -10
# CPU usage by command
pidstat -u 1
# System load investigation
uptime
vmstat 1
sar -u 1 10
High Memory Usage
bash
# Memory-intensive processes
ps aux --sort=-pmem | head -10
top -o %MEM
# Memory leaks detection
valgrind --leak-check=full command
# Out of memory investigation
dmesg | grep -i "killed process"
grep -i "out of memory" /var/log/messages
Zombie Processes
bash
# Find zombie processes
ps aux | awk '$8 ~ /^Z/ { print $2 }'
ps -eo stat,pid,cmd | grep -w Z
# Kill parent of zombie
ps -o ppid= -p zombie_pid
kill parent_pid
Process Not Responding
bash
# Check if process is hung
kill -0 PID # Test if process exists
strace -p PID # See what process is doing
# Force termination
kill -TERM PID # Try graceful first
sleep 5
kill -KILL PID # Force if necessary
Performance Optimization
Process Limits
bash
# View current limits
ulimit -a
cat /proc/PID/limits
# Set limits
ulimit -n 4096 # File descriptors
ulimit -u 1024 # Max user processes
ulimit -m 1048576 # Memory limit (KB)
# System-wide limits
cat /etc/security/limits.conf
CPU Affinity
bash
# View CPU affinity
taskset -p PID
# Set CPU affinity
taskset -p 0x1 PID # CPU 0 only
taskset -p 0x3 PID # CPU 0 and 1
taskset -c 0,1 PID # CPU 0 and 1
# Start process with affinity
taskset -c 0 command
Resources
- Linux Process Management Guide
- Systemd Documentation
- Performance Analysis Tools
- Process Monitoring Best Practices
This cheat sheet provides comprehensive process management commands for Linux systems. Always understand the impact of process control commands before using them in production environments.