Linux Inode Usage Monitor Shell Script (Prevent Disk Full)

linux inode monitor script

Disk space can look fine while inodes are exhausted — this script monitors inode usage with df -i and alerts before services crash.

What this script does

  • Checks inode use % on all mounted filesystems
  • Alerts when usage exceeds configurable threshold
  • Lists top directories by file count on affected mount
  • Email notification support
  • Complements disk space monitor scripts

Prerequisites

  • Linux with df and find
  • Optional: mail for alerts

Step 1: Save the script

sudo nano /usr/local/bin/inode-monitor.sh
sudo chmod +x /usr/local/bin/inode-monitor.sh

Step 2: Full script (scroll to read)

inode-monitor.sh
#!/usr/bin/env bash
set -euo pipefail

INODE_WARN=85
SCAN_PATH="/var"
ALERT_EMAIL="[email protected]"
LOG="/var/log/inode-monitor.log"

log(){ echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }

ALERT=0
while read -r pct mount; do
  pct=${pct%%%}
  [[ "$pct" =~ ^[0-9]+$ ]] || continue
  log "Inode use on $mount: ${pct}%"
  if (( pct >= INODE_WARN )); then
    log "WARNING: Inode usage ${pct}% on $mount"
    find "$SCAN_PATH" -xdev -type d -printf '%h\n' 2>/dev/null | sort | uniq -c | sort -rn | head -10 | tee -a "$LOG"
    ALERT=1
  fi
done < <(df -iP | awk 'NR>1 {print $5, $6}')

if (( ALERT )) && [[ -n "$ALERT_EMAIL" ]] && command -v mail >/dev/null; then
  tail -30 "$LOG" | mail -s "Inode alert: $(hostname -s)" "$ALERT_EMAIL"
  exit 1
fi
exit 0

Scroll inside the box to read the full script.

Step 3: Configure settings

  • INODE_WARN — alert threshold percent (default 85)
  • SCAN_PATH — path to scan for high file counts when alerting
  • ALERT_EMAIL — admin notification address
Linux inode usage monitor shell script
Linux inode usage monitor shell script

Step 4: Test manually

df -i
sudo /usr/local/bin/inode-monitor.sh

Schedule with cron

sudo crontab -e

Add:

0 */6 * * * /usr/local/bin/inode-monitor.sh >> /var/log/inode-monitor.log 2>&1

Related tutorials

Terminal screenshot is an original illustration created for Gnome IT Solutions (blog.gnomeitsolutions.com).