Fixing iNodes on a cPanel Server
iNodes are essential elements of a filesystem, as they store metadata about files and directories.
iNodes are essential elements of a filesystem, as they store metadata about files and directories. On cPanel servers, exceeding iNode limits can lead to file management issues, impacting email, backups, and general server operations. This guide will walk you through identifying, reducing, and monitoring iNode usage on your cPanel server using terminal commands, as well as automating inode tracking with the track_inodes.sh
script.
Step 1: Check iNode Usage in cPanel:
- Access cPanel and navigate to the “Disk Usage” or “File Manager” section. Here you can check the iNode usage for each directory. Note the areas with high iNode counts, as these may need cleanup.
- Inspect Email and Cache Directories: Email and cache folders are common causes of high inode usage. Delete unnecessary files, or remove unneeded emails to free up inodes.
Step 2: Identifying High iNode Usage via Terminal
- Access the Server via SSH: Open your terminal and connect to your server.
- List iNode Usage in Directories: To identify directories with high iNode counts, use the following command:
Replace/home/username/public_html
with the path you want to inspect. - Identify Top Files or Folders by iNode Count:
Ref. Code 2.1
ssh username@your_server_ip
Ref. Code 2.2
find /home/username/public_html -type d -exec du -a --inodes {} + | sort -n
Ref. Code 2.3
du -a --inodes /home/username | sort -nr | head -20
This command shows the top 20 files and directories by inode usage.
Step 3: Clean Up Files to Reduce iNode Usage
- Remove Unnecessary Files: Use the rm command to delete files, especially in directories with many small, unimportant files. For example:
- Clear Cache and Temporary Files – Ref Code 3.1
- Clear cPanel Cache – Ref Code 3.2.1
- Clear Email Cache – Ref Code 3.2.2
- Remove Unneeded Backups – Ref Code 3.3
Ref. Code 3.1 – This clears files in the tmp
folder, but be cautious with rm -rf
as it deletes files and directories permanently.
rm -rf /home/username/public_html/tmp/*
Ref Code 3.2.1
rm -rf /home/username/.cpanel/caches/*
Ref Code 3.2.2
rm -rf /home/username/mail/*/cur/*
Ref Code 3.3 – Replace /home/username/
with the actual home directory path for your account.
rm -rf /home/username/backups/*
Step 4: Automating iNode Tracking with track_inodes.sh
To monitor inode usage and quickly identify when limits are being approached, you can use a custom shell script, track_inodes.sh
. This script logs iNode usage in specified directories, allowing you to monitor changes over time.
Script Content for track_inodes.sh
This script will:
– Track and log the inode count in the specified directory.
– Log newly added files from the last 24 hours, allowing you to identify sudden spikes in inode usage.
#!/bin/bash
# Directory to check
DIR="/home/username/public_html"
# Log file location
LOG_FILE="/home/username/inode_history.log"
# Get the current date
DATE=$(date +"%Y-%m-%d")
# Get the current inode count in the specified directory
INODE_COUNT=$(find "$DIR" -type f | wc -l)
INODE_COUNT_SERVER=$(find / -type f | wc -l) # Optional: Count inodes on the server
# Log date and inode counts to the log file
echo "$DATE: Total files in $DIR: $INODE_COUNT" >> "$LOG_FILE"
echo "$DATE: Total files on server: $INODE_COUNT_SERVER" >> "$LOG_FILE"
# Find and log newly added files within the last 24 hours
echo "$DATE: Newly added files in $DIR:" >> "$LOG_FILE"
find "$DIR" -type f -ctime -1 >> "$LOG_FILE"
Setting Up and Running the Script
- Upload the Script to Your Server: Place the track_inodes.sh file in your preferred directory (e.g., /home/username/).
- Make the Script Executable. – Ref Code 4.2
- Run the Script Manually. – Ref Code 4.3
- Automate the Script with Cron (Optional): Set up a cron job to run the script daily. – Ref Code 4.4
- Add the following line to run the script daily at midnight. – Ref Code 4.5
Ref Code 4.2
chmod +x /home/username/track_inodes.sh
Ref Code 4.3
/home/username/track_inodes.sh
Ref Code 4.4
crontab -e
Ref Code 4.5
0 0 * * * /home/username/track_inodes.sh
Monitoring and Reviewing Logs
The inode_history.log
file will contain entries showing daily inode usage, and files added within the past 24 hours. Review this log regularly to monitor inode usage trends and avoid exceeding limits.
cat /home/username/inode_history.log
By following these steps and using the track_inodes.sh
script, you can effectively manage inode usage on your cPanel server, preventing potential disruptions and maintaining optimal server performance.