๐ฅ Setting Up Process Health Checks on Alpine Linux: Simple Guide
Letโs set up process health monitoring on Alpine Linux! ๐ This tutorial shows you how to watch your system and catch problems before they become big issues. Perfect for keeping your server healthy! ๐
๐ค What are Process Health Checks?
Process health checks are like doctors for your computer! They watch your programs and services to make sure everything is working correctly.
Process health checks are like:
- ๐ Security guards that watch your programs 24/7
- ๐ฅ Health monitors that check if services are running properly
- ๐ก Early warning systems that alert you when something goes wrong
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux running on your system
- โ Root access or sudo privileges
- โ Basic knowledge of terminal commands
- โ Some running services to monitor
๐ Step 1: Install Monitoring Tools
Get the Right Monitoring Software
Letโs install tools to watch our processes! Itโs super easy! ๐
What weโre doing: Installing process monitoring and health check utilities.
# Update package list first
apk update
# Install process monitoring tools
apk add htop procps-ng
# Install system monitoring utilities
apk add sysstat
# Install service monitoring tools
apk add monit
# Check tools are installed
htop --version
What this does: ๐ Gives you powerful tools to monitor and check process health.
Example output:
htop 3.2.1
What this means: Your monitoring tools are ready to use! โ
๐ก Important Tips
Tip: These tools help you see what your computer is doing! ๐ก
Warning: Some monitoring uses a little CPU, but itโs worth it! โ ๏ธ
๐ ๏ธ Step 2: Basic Process Monitoring
See Whatโs Running Right Now
Letโs look at all the processes on your system! This is interesting! ๐
What weโre doing: Checking which processes are currently running.
# See all running processes
ps aux
# Show processes in a tree format
ps auxf
# Use htop for interactive monitoring
htop
Code explanation:
ps aux
: Shows all processes with detailsps auxf
: Shows processes with parent-child relationshipshtop
: Interactive process viewer (press โqโ to quit)
Expected Output (partial):
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 1628 568 ? Ss 10:00 0:01 /sbin/init
root 82 0.0 0.1 2108 712 ? S 10:00 0:00 /usr/sbin/sshd
What this means: You can see every program running on your system! ๐
Check Specific Services
Letโs check if important services are healthy! ๐ฏ
What weโre doing: Monitoring specific system services and their status.
# Check service status with OpenRC
rc-status
# Check specific service health
rc-service sshd status
# Monitor a specific process
pgrep -l sshd
# Get detailed process info
ps -p $(pgrep sshd) -o pid,ppid,cmd,%cpu,%mem
You should see:
* sshd [ started ]
82 sshd: /usr/sbin/sshd [listener] 0 of 10-100 startups
Awesome! Your services are running healthy! ๐
๐ Step 3: Set Up Automated Health Checks
Create Simple Health Check Scripts
Letโs write scripts that check process health automatically! ๐
What weโre doing: Creating custom scripts to monitor important processes.
# Create a health check script
cat > /usr/local/bin/health-check.sh << 'EOF'
#!/bin/sh
echo "๐ฅ System Health Check - $(date)"
echo "================================"
# Check if SSH service is running
if pgrep sshd > /dev/null; then
echo "โ
SSH service: Running"
else
echo "โ SSH service: Not running"
fi
# Check system load
LOAD=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
echo "๐ System load: $LOAD"
# Check memory usage
MEM=$(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100}')
echo "๐ง Memory usage: ${MEM}%"
# Check disk usage
DISK=$(df / | tail -1 | awk '{print $5}')
echo "๐พ Disk usage: $DISK"
echo "Health check completed! ๐"
EOF
# Make script executable
chmod +x /usr/local/bin/health-check.sh
# Test the health check
/usr/local/bin/health-check.sh
What this does: Creates a script that checks your systemโs vital signs! โ
Set Up Automatic Monitoring with Monit
Letโs use Monit to automatically watch processes! This is powerful! ๐ฎ
What weโre doing: Configuring Monit to automatically monitor and restart services.
# Create monit configuration
cat > /etc/monitrc << 'EOF'
# Monit configuration for Alpine Linux
set daemon 60 # Check every 60 seconds
set logfile /var/log/monit.log # Log file location
set pidfile /run/monit.pid # PID file location
# Web interface (optional)
set httpd port 2812
allow localhost
# Monitor SSH service
check process sshd with pidfile /run/sshd.pid
start program = "/sbin/rc-service sshd start"
stop program = "/sbin/rc-service sshd stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
# Monitor system resources
check system localhost
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
EOF
# Set proper permissions
chmod 0600 /etc/monitrc
# Start monit service
rc-service monit start
# Enable monit at boot
rc-update add monit default
Code explanation:
set daemon 60
: Checks every 60 secondscheck process sshd
: Monitors SSH service specificallyif failed port 22
: Restarts SSH if port 22 doesnโt respondcheck system localhost
: Monitors overall system health
What this means: Monit will automatically restart services if they fail! ๐
๐ Step 4: Advanced Health Monitoring
Create Process Watchdog
Letโs create a smart watchdog that monitors custom processes! ๐
What weโre doing: Building an advanced monitoring system for any process.
# Create advanced process monitor
cat > /usr/local/bin/process-watchdog.sh << 'EOF'
#!/bin/sh
PROCESS_NAME="$1"
MAX_RESTARTS=3
RESTART_COUNT=0
LOG_FILE="/var/log/process-watchdog.log"
if [ -z "$PROCESS_NAME" ]; then
echo "Usage: $0 <process_name>"
exit 1
fi
log_message() {
echo "$(date): $1" >> "$LOG_FILE"
echo "$1"
}
while true; do
if ! pgrep "$PROCESS_NAME" > /dev/null; then
log_message "โ ๏ธ Process $PROCESS_NAME not running!"
if [ $RESTART_COUNT -lt $MAX_RESTARTS ]; then
log_message "๐ Attempting to restart $PROCESS_NAME (attempt $((RESTART_COUNT + 1)))"
# Try to restart the service
rc-service "$PROCESS_NAME" restart
RESTART_COUNT=$((RESTART_COUNT + 1))
sleep 10
if pgrep "$PROCESS_NAME" > /dev/null; then
log_message "โ
Process $PROCESS_NAME restarted successfully!"
RESTART_COUNT=0
fi
else
log_message "โ Max restart attempts reached for $PROCESS_NAME"
sleep 300 # Wait 5 minutes before trying again
RESTART_COUNT=0
fi
else
log_message "โ
Process $PROCESS_NAME is healthy"
RESTART_COUNT=0
fi
sleep 60 # Check every minute
done
EOF
# Make watchdog executable
chmod +x /usr/local/bin/process-watchdog.sh
# Test the watchdog (run in background)
nohup /usr/local/bin/process-watchdog.sh sshd &
echo "Watchdog started for SSH service! ๐"
What this does: Creates a smart guardian that automatically restarts failed processes! ๐
Monitor Custom Applications
Letโs monitor your own applications too! ๐ฏ
What weโre doing: Setting up health checks for web applications and custom services.
# Create web service health checker
cat > /usr/local/bin/web-health-check.sh << 'EOF'
#!/bin/sh
SERVICE_URL="http://localhost:8080"
TIMEOUT=10
echo "๐ Web Service Health Check"
echo "=========================="
# Check if web service responds
if curl -s --max-time $TIMEOUT "$SERVICE_URL" > /dev/null; then
echo "โ
Web service: Responding"
echo "๐ Response time: $(curl -s -w '%{time_total}' -o /dev/null "$SERVICE_URL")s"
else
echo "โ Web service: Not responding"
echo "๐ง Consider restarting the web service"
fi
# Check if port is listening
if netstat -tuln | grep ':8080' > /dev/null; then
echo "โ
Port 8080: Listening"
else
echo "โ Port 8080: Not listening"
fi
EOF
chmod +x /usr/local/bin/web-health-check.sh
echo "Web health checker created!"
๐ Quick Monitoring Table
Tool | Purpose | Command | Result |
---|---|---|---|
๐ htop | Live process viewer | htop | โ Interactive monitoring |
๐ ps | Process listing | ps aux | โ Current processes |
๐ฅ monit | Automatic monitoring | monit status | โ Service health |
๐ sysstat | System statistics | iostat | โ Performance metrics |
๐ฎ Practice Time!
Letโs practice what you learned! Try these simple examples:
Example 1: Quick System Check ๐ข
What weโre doing: Creating a one-command system health overview.
# Create quick health overview
cat > /usr/local/bin/quick-health.sh << 'EOF'
#!/bin/sh
echo "๐ Quick System Health Overview"
echo "=============================="
echo "๐ป Hostname: $(hostname)"
echo "โฐ Uptime: $(uptime -p)"
echo "๐ฅ Users: $(who | wc -l) logged in"
echo "๐ Processes: $(ps aux | wc -l) running"
echo "๐ง Memory: $(free -h | grep Mem | awk '{print $3 "/" $2}')"
echo "๐พ Disk: $(df -h / | tail -1 | awk '{print $3 "/" $2 " (" $5 " used)"}')"
echo "Health check complete! โ
"
EOF
chmod +x /usr/local/bin/quick-health.sh
/usr/local/bin/quick-health.sh
What this does: Gives you a quick overview of your system health! ๐
Example 2: Process Alert System ๐ก
What weโre doing: Creating a simple alert when processes use too much CPU.
# Create CPU usage alert
cat > /usr/local/bin/cpu-alert.sh << 'EOF'
#!/bin/sh
CPU_THRESHOLD=80
echo "๐ฅ CPU Usage Monitor"
echo "==================="
# Check for high CPU processes
HIGH_CPU=$(ps aux | awk 'NR>1 && $3>'"$CPU_THRESHOLD"' {print $11 " (" $3 "%)"}' )
if [ -n "$HIGH_CPU" ]; then
echo "โ ๏ธ High CPU usage detected:"
echo "$HIGH_CPU"
else
echo "โ
All processes using normal CPU"
fi
EOF
chmod +x /usr/local/bin/cpu-alert.sh
./cpu-alert.sh
What this does: Warns you when processes use too much CPU! ๐
๐จ Fix Common Problems
Problem 1: Monit wonโt start โ
What happened: Monit service fails to start. How to fix it: Check configuration file permissions and syntax.
# Fix monit configuration
chmod 0600 /etc/monitrc
# Test monit configuration
monit -t
# Start monit with verbose output
monit -v
Problem 2: Health checks show false alarms โ
What happened: Scripts report problems when everything is fine. How to fix it: Adjust thresholds and add delays.
# Add delays to avoid false positives
sleep 5 # Wait before checking
# Adjust CPU threshold in scripts
CPU_THRESHOLD=90 # Higher threshold
# Check multiple times before alerting
for i in 1 2 3; do
# Check three times before deciding
done
Donโt worry! Tuning monitoring takes time to get perfect! ๐ช
๐ก Simple Tips
- Start with basic monitoring ๐ - Begin with simple health checks
- Set reasonable thresholds ๐ฑ - Donโt make alerts too sensitive
- Test your scripts ๐ค - Make sure health checks work correctly
- Monitor the monitors ๐ช - Check that monitoring tools are working
โ Check Everything Works
Letโs make sure all health monitoring is working:
# Test all health check systems
echo "=== Process Health Check System Status ==="
echo "1. Testing basic health check:"
/usr/local/bin/health-check.sh
echo "2. Checking monit status:"
monit status
echo "3. Verifying log files:"
ls -la /var/log/monit.log /var/log/process-watchdog.log 2>/dev/null || echo "Logs will be created when needed"
echo "4. Testing quick health:"
/usr/local/bin/quick-health.sh
echo "All health monitoring systems ready! โ
"
Good output shows:
=== Process Health Check System Status ===
1. Testing basic health check:
๐ฅ System Health Check - Mon Jun 17 12:00:00 UTC 2025
โ
SSH service: Running
๐ System load: 0.15
๐ง Memory usage: 45.2%
๐พ Disk usage: 25%
2. Checking monit status:
The Monit daemon 5.33.0 uptime: 5m
All health monitoring systems ready! โ
๐ What You Learned
Great job! Now you can:
- โ Monitor running processes and system health
- โ Set up automatic service restart with Monit
- โ Create custom health check scripts
- โ Build process watchdogs for critical services
- โ Monitor web applications and custom processes
- โ Set up alerts for high resource usage
๐ฏ Whatโs Next?
Now you can try:
- ๐ Learning about advanced monitoring with Prometheus
- ๐ ๏ธ Setting up centralized logging and monitoring
- ๐ค Creating custom dashboards for system monitoring
- ๐ Building automated incident response systems!
Remember: Good monitoring prevents small problems from becoming big disasters! Youโre doing amazing! ๐
Keep monitoring and your systems will stay healthy! ๐ซ