Overview
This script performs automatic system maintenance tasks such as cleaning packages, clearing cache, removing old logs, and cleaning Docker resources.
Purpose: Keep Linux servers clean, optimized, and healthy automatically.
Full Script
#!/bin/bash
LOGFILE="/var/log/last-7days-maintenance.log"
# Ensure script runs as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Logging
exec > >(tee -a $LOGFILE) 2>&1
# Detect OS and cleanup packages
if grep -qi "ubuntu" /etc/os-release; then
echo "Ubuntu detected - cleaning packages"
apt update -y
apt autoremove --purge -y
apt autoclean -y
apt clean
elif grep -qiE "rhel|centos|rocky|alma" /etc/os-release; then
echo "RHEL-based OS detected - cleaning packages"
yum update -y
yum autoremove -y
yum clean all
fi
# Clear memory cache safely
echo "Clearing RAM cache"
sync
echo 3 > /proc/sys/vm/drop_caches
# Clean old temp files (older than 7 days)
echo "Cleaning temp files older than 7 days"
find /tmp -type f -mtime +7 -delete
find /var/tmp -type f -mtime +7 -delete
# Vacuum journal logs
echo "Cleaning journal logs older than 7 days"
journalctl --vacuum-time=7d
# Restart systemd daemon safely
echo "Reloading systemd daemon"
systemctl daemon-reexec
# Store last 7 days error logs
echo "Saving error logs"
journalctl -p err --since "7 days ago" --no-pager > /var/log/last-7days-errors.log
# Docker cleanup
if command -v docker &> /dev/null; then
echo "Cleaning Docker resources older than 30 days"
docker container prune --filter "until=720h" -f
docker image prune -a --filter "until=720h" -f
docker network prune --filter "until=720h" -f
docker volume prune -f
docker builder prune --filter "until=720h" -f
else
echo "Docker not installed, skipping"
fi
Step-by-Step Explanation
1. Root Privilege Check
if [ "$EUID" -ne 0 ]; then exit 1 fi
- Ensures script runs only as root
- Required for system cleanup operations
2. Logging
exec > >(tee -a $LOGFILE) 2>&1
- Saves output to log file
- Log location: /var/log/last-7days-maintenance.log
3. OS Detection and Package Cleanup
Ubuntu:
apt update apt autoremove apt clean
RHEL/CentOS:
yum update yum autoremove yum clean all
- Updates packages
- Removes unused packages
- Frees disk space
4. Clear RAM Cache
sync echo 3 > /proc/sys/vm/drop_caches
- Clears memory cache safely
- Improves memory availability
5. Clean Temp Files
find /tmp -type f -mtime +7 -delete
- Deletes files older than 7 days
- Frees disk space
6. Clean Journal Logs
journalctl --vacuum-time=7d
- Removes logs older than 7 days
- Keeps system logs optimized
7. Reload systemd
systemctl daemon-reexec
- Reloads systemd safely
8. Save Error Logs
journalctl -p err --since "7 days ago"
- Saves last 7 days error logs
- Useful for troubleshooting
9. Docker Cleanup
docker container prune docker image prune docker volume prune
- Removes unused Docker resources
- Frees disk space
Benefits
- Automated system cleanup
- Improves performance
- Frees disk space
- Removes unused packages
- Cleans Docker resources
- Maintains system health
Recommended Usage
Run weekly using cron:
crontab -e 0 2 * * 0 /root/maintenance.sh
This runs every Sunday at 2 AM.
Summary
- Works on Ubuntu, CentOS, RHEL, Rocky, AlmaLinux
- Cleans packages, logs, temp files, and Docker
- Stores logs for monitoring
- Safe for production servers