Commands.page Logo

Can htop Alert When CPU Usage Exceeds Threshold

This article explains whether the htop system monitor supports automated alerts for high CPU usage on Ubuntu. It clarifies the tool’s limitations and provides practical alternative methods to monitor CPU thresholds effectively.

Direct Answer on htop Capabilities

No, htop cannot be configured to send alerts or notifications when CPU usage exceeds a specific threshold. The tool is designed as an interactive process viewer for real-time manual inspection. While it visually changes bar colors from green to red based on load, it lacks a background daemon or scripting hook to trigger external alarms, emails, or logs automatically.

Alternative Method: Bash Scripting

To achieve CPU threshold alerts on Ubuntu, you can use a simple Bash script combined with cron. This method polls the CPU usage and triggers a command if the limit is breached.

  1. Create a script named cpu_alert.sh.
  2. Add the following logic to check usage directly from /proc/stat:
#!/bin/bash
THRESHOLD=80
USAGE=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}')
if [ $(echo "$USAGE > $THRESHOLD" | bc) -eq 1 ]; then
    echo "High CPU Usage Detected: $USAGE%" | mail -s "CPU Alert" [email protected]
fi
  1. Make the script executable with chmod +x cpu_alert.sh.
  2. Schedule it to run every minute using crontab -e.

Alternative Method: Dedicated Monitoring Tools

For robust infrastructure monitoring, consider dedicated tools instead of process viewers. Solutions like Netdata, Prometheus with Node Exporter, or Nagios are built specifically for threshold alerting. These tools run as services, collect metrics over time, and support integrations for Slack, Email, or PagerDuty notifications when CPU limits are exceeded on your Ubuntu system.

Summary

While htop is excellent for live debugging, it does not support automated alerting. Implementing a custom Bash script or deploying a dedicated monitoring service is the required approach for receiving notifications when CPU usage surpasses your defined limits.