tmp-cleanup-maintenance.sh
1 #!/bin/bash 2 # /tmp Cleanup Maintenance Script 3 # Prevents accumulation of old temporary files that can block rustc 4 # 5 # Usage: Run manually or via cron 6 # Recommended: crontab -e 7 # 0 2 * * * /home/devops/working-repos/alpha-delta-context/components/_plans/tmp-cleanup-maintenance.sh 8 9 set -euo pipefail 10 11 LOG_FILE="/var/log/tmp-cleanup.log" 12 13 log() { 14 echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE" 15 } 16 17 # Check /tmp usage before cleanup 18 BEFORE=$(ls -la /tmp | wc -l) 19 log "Starting /tmp cleanup. Current entries: $BEFORE" 20 21 # Clean old files (>1 day old) 22 log "Cleaning old files..." 23 sudo find /tmp -maxdepth 1 -type f -mtime +1 \ 24 -not -name '.testnet_password' \ 25 -delete 2>/dev/null || true 26 27 # Clean empty directories (>1 day old) 28 log "Cleaning empty old directories..." 29 sudo find /tmp -maxdepth 1 -type d -mtime +1 -empty \ 30 -delete 2>/dev/null || true 31 32 # Clean non-empty old directories (>2 days old for safety) 33 log "Cleaning non-empty old directories (>2 days)..." 34 sudo find /tmp -maxdepth 1 -type d -mtime +2 \ 35 -exec rm -rf {} \; 2>/dev/null || true 36 37 # Check /tmp usage after cleanup 38 AFTER=$(ls -la /tmp | wc -l) 39 CLEANED=$((BEFORE - AFTER)) 40 41 log "Cleanup complete. Entries before: $BEFORE, after: $AFTER, cleaned: $CLEANED" 42 43 # Check filesystem 44 df -h /tmp | tail -1 | awk '{print "Disk usage: " $3 " / " $2 " (" $5 ")"}' 45 df -i /tmp | tail -1 | awk '{print "Inode usage: " $3 " / " $2 " (" $5 ")"}' 46 47 # Test rustc temp directory creation 48 if ! rustc --version >/dev/null 2>&1; then 49 log "WARNING: rustc not available or failed" 50 exit 1 51 fi 52 53 log "/tmp maintenance completed successfully" 54 exit 0