#!/bin/bash # # /usr/local/sbin/custom-cleanup-script.sh # Selective /tmp cleanup at boot, with safety checks and logging. set -u TARGET=/tmp LOG=/var/log/tmp-cleanup.log # Reset log on every boot echo "=== $(date) ===" > "$LOG" # Safety check 1: TARGET must be exactly /tmp if [ "$TARGET" != "/tmp" ]; then echo "FATAL: TARGET is '$TARGET', not '/tmp'. Aborting." >> "$LOG" exit 1 fi # Safety check 2: TARGET must be a real directory, not a symlink if [ ! -d "$TARGET" ] || [ -L "$TARGET" ]; then echo "FATAL: $TARGET is not a real directory. Aborting." >> "$LOG" exit 1 fi # Safety check 3: cd must succeed cd "$TARGET" || { echo "FATAL: cannot cd to $TARGET. Aborting." >> "$LOG" exit 1 } # Safety check 4: pwd must be exactly /tmp after cd if [ "$(pwd -P)" != "/tmp" ]; then echo "FATAL: pwd is '$(pwd -P)', not '/tmp'. Aborting." >> "$LOG" exit 1 fi echo "Safety checks passed. Cleaning $TARGET" >> "$LOG" # Remove SlackBuild work directory entirely rm -rf SBo >> "$LOG" 2>&1 # Remove generated Slackware packages find . -maxdepth 1 -type f \( -name "*.tgz" -o -name "*.txz" \) -print -delete >> "$LOG" 2>&1 # Remove SlackBuild DESTDIR directories find . -maxdepth 1 -type d -name "package-*" -print -exec rm -rf {} + >> "$LOG" 2>&1 # Remove extracted source directories matching name-version pattern find . -maxdepth 1 -type d -regex './[a-zA-Z][a-zA-Z0-9_-]*-[0-9][0-9.]*.*' -print -exec rm -rf {} + >> "$LOG" 2>&1 # Remove build logs find . -maxdepth 1 -type f -name "*.log" -delete >> "$LOG" 2>&1 # Remove orphaned wget lock files find . -maxdepth 1 -name ".wget-*_lck_*" -delete >> "$LOG" 2>&1 # Remove Go build caches find . -maxdepth 1 -name "go-build*" -exec rm -rf {} + >> "$LOG" 2>&1 echo "" >> "$LOG" du -sh /tmp >> "$LOG" 2>&1