๐ Monitoring Running Processes: Simple Guide
Want to see whatโs happening on your system? Letโs learn to monitor processes! ๐ป This guide shows you easy ways to watch running programs. Your system will have no secrets! ๐
๐ค What are Running Processes?
Running processes are all the programs working on your computer right now. Think of them like busy workers in a factory!
Running processes are like:
- ๐ Workers doing different jobs in your computer
- ๐ง Programs that keep your system running smoothly
- ๐ก Tasks that happen behind the scenes
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux system with terminal access
- โ Basic knowledge of command line
- โ A few minutes to practice
- โ Curiosity about what your system does
๐ Step 1: Basic Process Viewing
See All Running Processes
Letโs look at whatโs running on your system. Itโs easy! ๐
What weโre doing: Looking at all the programs currently running.
# Show all running processes
ps aux
# Show processes in a tree format
ps auxf
# Show just your processes
ps aux | grep $USER
What this does: ๐ Shows you every program running right now.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 1234 567 ? Ss 10:00 0:01 /sbin/init
root 123 0.1 0.5 2345 1012 ? S 10:01 0:00 [kthreadd]
user 1234 2.3 1.2 3456 2034 pts/0 R+ 10:05 0:03 vim file.txt
What this means: You can see every program and how much memory it uses! โ
๐ก Important Tips
Tip: The PID number is like each processโs ID card! ๐ก
Warning: Donโt kill important system processes! โ ๏ธ
๐ ๏ธ Step 2: Real-time Process Monitoring
Watch Processes Live
Now letโs watch processes update in real time. Donโt worry - itโs still easy! ๐
What weโre doing: Watching processes change live, like a dashboard.
# Show live process activity
top
# Better version with colors
htop
# Simple system monitor
watch -n 1 'ps aux | head -10'
# Monitor specific processes
watch -n 2 'ps aux | grep nginx'
Code explanation:
top
: Shows live process list with CPU and memory usagehtop
: Prettier version of top with colors and mouse supportwatch -n 1
: Refreshes the command every 1 secondgrep nginx
: Filters to show only nginx processes
Expected Output:
top - 10:15:30 up 2:45, 1 user, load average: 0.15, 0.20, 0.18
Tasks: 45 total, 1 running, 44 sleeping
%Cpu(s): 2.3 us, 1.2 sy, 0.0 ni, 96.5 id
KiB Mem: 1024000 total, 512000 free, 256000 used, 256000 buff/cache
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user 20 0 15.1m 8.2m 4.5m S 2.3 0.8 0:03.45 vim
What this means: Great job! Youโre watching your system work live! ๐
Create Process Monitoring Scripts
What weโre doing: Making simple scripts to watch important processes.
# Create a process monitor script
cat > /usr/local/bin/process-monitor.sh << 'EOF'
#!/bin/bash
# Simple Process Monitor
echo "๐ฅ๏ธ Alpine Linux Process Monitor"
echo "=============================="
echo "Date: $(date)"
echo ""
# Show system load
echo "๐ System Load:"
uptime
echo ""
# Show top 5 CPU processes
echo "๐ฅ Top 5 CPU Users:"
ps aux --sort=-%cpu | head -6
echo ""
# Show top 5 memory processes
echo "๐พ Top 5 Memory Users:"
ps aux --sort=-%mem | head -6
echo ""
# Show process count
echo "๐ Process Statistics:"
echo "Total processes: $(ps aux | wc -l)"
echo "Running processes: $(ps aux | grep -c ' R ')"
echo "Sleeping processes: $(ps aux | grep -c ' S ')"
EOF
chmod +x /usr/local/bin/process-monitor.sh
# Test the monitor
/usr/local/bin/process-monitor.sh
What this does: Creates your own process monitoring dashboard! ๐
๐ฎ Letโs Try It!
Time for hands-on practice! This is the fun part! ๐ฏ
What weโre doing: Testing our process monitoring skills.
# Start a test process in background
sleep 300 &
echo "Started background process: $!"
# Find our test process
ps aux | grep sleep
# Monitor it
top -p $!
# When done, stop it
kill $!
You should see:
Started background process: 1234
user 1234 0.0 0.0 1234 456 pts/0 S 10:10 0:00 sleep 300
Awesome work! ๐
๐ Quick Summary Table
Command | Purpose | What it Shows |
---|---|---|
๐ง ps aux | List all processes | Current running programs |
๐ ๏ธ top | Live monitoring | Real-time system activity |
๐ฏ htop | Better live view | Colorful process dashboard |
๐ watch ps | Refresh view | Updated process list |
๐ฎ Practice Time!
Letโs practice what you learned! Try these simple examples:
Example 1: Find Heavy Processes ๐ข
What weโre doing: Finding processes that use lots of CPU or memory.
# Find processes using most CPU
echo "๐ฅ CPU Heavy Processes:"
ps aux --sort=-%cpu | head -5
# Find processes using most memory
echo "๐พ Memory Heavy Processes:"
ps aux --sort=-%mem | head -5
# Find processes by name
echo "๐ Web Server Processes:"
ps aux | grep -E "(nginx|apache|httpd)"
# Count processes by user
echo "๐ฅ Processes by User:"
ps aux | awk '{print $1}' | sort | uniq -c | sort -nr
What this does: Shows you which programs are working hardest! ๐
Example 2: Process Health Check ๐ก
What weโre doing: Creating a health check for important services.
# Create service health checker
cat > /usr/local/bin/service-health.sh << 'EOF'
#!/bin/bash
# Service Health Checker
echo "๐ฅ Service Health Check"
echo "====================="
# Check important services
services=("sshd" "chronyd" "networking")
for service in "${services[@]}"; do
if pgrep "$service" > /dev/null; then
echo "โ
$service: Running (PID: $(pgrep $service))"
else
echo "โ $service: Not running"
fi
done
echo ""
# Check system resources
echo "๐ System Resources:"
echo "Load: $(uptime | awk -F'load average:' '{print $2}')"
echo "Memory: $(free | grep Mem | awk '{printf "%.1f%%", $3/$2 * 100.0}')"
echo "Processes: $(ps aux | wc -l)"
EOF
chmod +x /usr/local/bin/service-health.sh
# Run the health check
/usr/local/bin/service-health.sh
What this does: Creates an automatic system health checker! ๐
๐จ Fix Common Problems
Problem 1: Too many processes running โ
What happened: Your system is slow because too many programs are running. How to fix it: Find and stop unnecessary processes!
# Find processes using most resources
ps aux --sort=-%cpu | head -10
# Stop a specific process safely
kill -TERM 1234
# If it won't stop, force it
kill -KILL 1234
Problem 2: Canโt find a specific process โ
What happened: You know a program is running but canโt see it. How to fix it: Use better search methods!
# Search by name
pgrep -f "program-name"
# Search with more details
ps aux | grep -i "program"
# Find by port (if it's a server)
netstat -tulpn | grep :80
Problem 3: Process information is confusing โ
What happened: Process output has too much information. How to fix it: Use simpler formats!
# Simple process list
ps -eo pid,user,cmd
# Just running processes
ps r
# Custom format
ps -eo pid,ppid,%cpu,%mem,cmd --sort=-%cpu
Donโt worry! Process monitoring takes practice. Youโre doing great! ๐ช
๐ก Simple Tips
- Start with simple commands ๐
- Use
ps aux
first - Learn one tool well ๐ฑ - Master
top
orhtop
- Watch regularly ๐ค - Check your system often
- Donโt panic ๐ช - High CPU usage is often normal
โ Check Everything Works
Letโs make sure everything is working:
# Test basic monitoring
echo "Testing process monitoring..."
# Check ps command
ps aux > /dev/null && echo "โ
ps command works"
# Check if top is available
which top > /dev/null && echo "โ
top command available"
# Check our custom scripts
ls -la /usr/local/bin/*monitor* /usr/local/bin/*health* 2>/dev/null
# Run a quick process check
echo "Current process count: $(ps aux | wc -l)"
echo "Process monitoring test completed! โ
"
Good output:
Testing process monitoring...
โ
ps command works
โ
top command available
-rwxr-xr-x 1 root root 1234 Jun 4 10:15 /usr/local/bin/process-monitor.sh
-rwxr-xr-x 1 root root 2345 Jun 4 10:20 /usr/local/bin/service-health.sh
Current process count: 47
Process monitoring test completed! โ
๐ What You Learned
Great job! Now you can:
- โ View all running processes on your system
- โ Monitor processes in real-time with top and htop
- โ Create custom monitoring scripts
- โ Find and troubleshoot problematic processes!
๐ฏ Whatโs Next?
Now you can try:
- ๐ Learning about process priorities and nice values
- ๐ ๏ธ Setting up automated process monitoring alerts
- ๐ค Creating process management scripts
- ๐ Building system dashboards with process information!
Remember: Every system administrator was once a beginner. Youโre doing amazing! ๐
Keep practicing and youโll become a process monitoring expert too! ๐ซ