A comprehensive collection of automated maintenance scripts for macOS systems, designed to keep your development environment clean, updated, and backed up. The suite includes individual maintenance scripts and a centralized weekly automation job.
# Run all maintenance tasks manually
~/Scripts/run_all_maintenance.sh
# Check system status
~/Scripts/maintenance_status.sh
# Run individual maintenance scripts
~/Scripts/brew_maintenance.sh
~/Scripts/system_cleanup.sh
~/Scripts/document_backup.sh
~/Scripts/package_updates.sh
~/Scripts/dev_maintenance.shrun_all_maintenance.sh- Master script that orchestrates all maintenance tasksbrew_maintenance.sh- Homebrew package management and cleanupsystem_cleanup.sh- System cache cleanup and optimizationdocument_backup.sh- Document and configuration file backuppackage_updates.sh- System and application package updatesdev_maintenance.sh- Development environment maintenance (npm, pip, gems, etc.)maintenance_status.sh- System status dashboard and script health check
raycast-brew-maintenance.sh- Quick Homebrew maintenance via Raycastraycast-system-cleanup.sh- Quick system cleanup via Raycastraycast-document-backup.sh- Quick document backup via Raycastraycast-package-updates.sh- Quick package updates via Raycast
test_maintenance_job.sh- Test script for debugging automated jobs
All scripts are designed to work with proper environment loading:
#!/usr/bin/env bash -lThe -l flag ensures:
- âś… Login shell behavior
- âś… Loads
~/.bash_profile,~/.bashrc,~/.profile - âś… Environment variables are available (PATH, HOMEBREW_PREFIX, etc.)
- âś… Package managers (brew, npm, pip) are accessible
Ensure these are properly configured in your shell profile:
# Homebrew
export PATH="/opt/homebrew/bin:$PATH"
export HOMEBREW_PREFIX="/opt/homebrew"
# Node.js/npm
export PATH="$PATH:$HOME/.npm-global/bin"
# Python
export PATH="$PATH:$HOME/.local/bin"
# Go
export GOPATH="$HOME/go"
export PATH="$PATH:$GOPATH/bin"- Job Name:
com.user.maintenance.weekly - Schedule: Every Monday at 9:00 AM
- Shell: Runs with login shell (
bash -l) to ensure environment variables are loaded - Working Directory:
~/Scripts/ - Logs:
- stdout:
~/Scripts/maintenance_weekly.log - stderr:
~/Scripts/maintenance_weekly_error.log
- stdout:
- Plist File:
~/Library/LaunchAgents/com.user.maintenance.weekly.plist - Log Files:
~/Scripts/maintenance_weekly.log~/Scripts/maintenance_weekly_error.log
- Test Script:
~/Scripts/test_maintenance_job.sh
launchctl list com.user.maintenance.weeklylaunchctl start com.user.maintenance.weekly# Stop the job
launchctl unload ~/Library/LaunchAgents/com.user.maintenance.weekly.plist
# Start the job
launchctl load ~/Library/LaunchAgents/com.user.maintenance.weekly.plist# View stdout log
tail -f ~/Scripts/maintenance_weekly.log
# View stderr log
tail -f ~/Scripts/maintenance_weekly_error.log
# Run test script to trigger and view logs
~/Scripts/test_maintenance_job.sh- âś… Runs with login shell (sources ~/.bash_profile, ~/.bashrc, etc.)
- âś… Sets working directory to ~/Scripts/
- âś… Captures both stdout and stderr to separate log files
- âś… Weekly schedule (Mondays at 9 AM)
- âś… Automatic loading at user login
- âś… Environment variables preserved
~/Scripts/brew_maintenance.sh
# Updates all Homebrew packages, casks, and cleans up old versions
# Log: ~/Scripts/brew_maintenance.log~/Scripts/system_cleanup.sh
# Clears system caches, temporary files, and optimizes storage
# Log: ~/Scripts/system_cleanup.log~/Scripts/document_backup.sh
# Creates timestamped backups of Documents, Desktop, and config files
# Backup location: ~/Backups/
# Log: ~/Scripts/document_backup.log~/Scripts/package_updates.sh
# Updates macOS software, App Store apps, and system packages
# Log: ~/Scripts/package_updates.log~/Scripts/dev_maintenance.sh
# Updates npm, pip, gems, Rust, Go modules, and cleans dev caches
# Includes health check for common development tools~/Scripts/maintenance_status.sh
# Shows status of all scripts, last run times, and system health
# No log file (displays real-time information)~/Scripts/run_all_maintenance.sh
# Orchestrates all maintenance tasks with timing information
# Runs: system_cleanup → package_updates → brew_maintenance → document_backupThe Raycast scripts provide one-click access to maintenance tasks:
- Type "Brew Maintenance" in Raycast
- Type "System Cleanup" in Raycast
- Type "Document Backup" in Raycast
- Type "Package Updates" in Raycast
# Individual script logs
~/Scripts/brew_maintenance.log
~/Scripts/system_cleanup.log
~/Scripts/document_backup.log
~/Scripts/package_updates.log
# Weekly automated job logs
~/Scripts/maintenance_weekly.log # stdout
~/Scripts/maintenance_weekly_error.log # stderr# View recent activity
tail -f ~/Scripts/maintenance_weekly.log
# View errors
tail -f ~/Scripts/maintenance_weekly_error.log
# View all logs at once
ls -la ~/Scripts/*.log
# Check log sizes
du -sh ~/Scripts/*.logLogs are automatically managed but you can clean them manually:
# Archive old logs
mkdir -p ~/Scripts/logs_archive
mv ~/Scripts/*.log ~/Scripts/logs_archive/
# Or simply clear current logs
truncate -s 0 ~/Scripts/*.logProblem: Scheduled job isn't executing
Diagnosis:
# Check if job is loaded
launchctl list | grep maintenance
# Check job status
launchctl list com.user.maintenance.weeklySolutions:
# Reload the job
launchctl unload ~/Library/LaunchAgents/com.user.maintenance.weekly.plist
launchctl load ~/Library/LaunchAgents/com.user.maintenance.weekly.plist
# Test manual trigger
launchctl start com.user.maintenance.weeklyProblem: "Permission denied" errors
Solutions:
# Make all scripts executable
chmod +x ~/Scripts/*.sh
# Fix ownership if needed
chown $(whoami) ~/Scripts/*.sh
# Check script permissions
ls -la ~/Scripts/*.shProblem: Commands not found (brew, npm, pip, etc.)
Diagnosis:
# Test environment in non-login shell
bash -c 'echo $PATH'
# Test environment in login shell
bash -l -c 'echo $PATH'
# Check if tools are accessible
bash -l -c 'which brew npm pip3'Solutions:
- Ensure proper shebang in scripts:
#!/usr/bin/env bash -l - Add missing paths to
~/.bash_profileor~/.zshrc:export PATH="/opt/homebrew/bin:$PATH" export PATH="$PATH:$HOME/.local/bin"
- Source your profile manually:
source ~/.bash_profile # or ~/.zshrc
Problem: Backup script fails or creates empty backups
Diagnosis:
# Check backup directory
ls -la ~/Backups/
# Check disk space
df -h ~
# Test backup manually
~/Scripts/document_backup.shSolutions:
# Create backup directory if missing
mkdir -p ~/Backups
# Check and clean up old backups
find ~/Backups -name "*.tar.gz" -mtime +30 -delete
# Fix permissions
chmod 755 ~/BackupsProblem: Homebrew, npm, or pip updates fail
Solutions:
# Fix Homebrew permissions
sudo chown -R $(whoami) $(brew --prefix)/*
# Update Homebrew itself first
brew update
# Fix npm permissions
npm config set prefix ~/.npm-global
# Update pip itself
python3 -m pip install --upgrade pipProblem: Logs not appearing or permission denied
Solutions:
# Check log directory permissions
ls -la ~/Scripts/
# Fix log file permissions
chmod 644 ~/Scripts/*.log
# Create missing log files
touch ~/Scripts/{brew_maintenance,system_cleanup,document_backup,package_updates}.logRun scripts with debug output:
# Enable debug mode
bash -x ~/Scripts/run_all_maintenance.sh
# Or add to script temporarily
set -x # at the beginning of script
set +x # at the end of scriptCreate a test script to verify environment:
#!/usr/bin/env bash -l
echo "PATH: $PATH"
echo "Brew: $(which brew 2>/dev/null || echo 'Not found')"
echo "Node: $(which node 2>/dev/null || echo 'Not found')"
echo "Python3: $(which python3 2>/dev/null || echo 'Not found')"
echo "Current directory: $(pwd)"
echo "User: $(whoami)"
echo "Shell: $SHELL"# Monitor script execution time
time ~/Scripts/run_all_maintenance.sh
# Monitor system resources during execution
top -pid $(pgrep -f maintenance)
# Check disk usage before/after
df -h / && ~/Scripts/system_cleanup.sh && df -h /- Create new script in
~/Scripts/ - Make it executable:
chmod +x new_script.sh - Add to
run_all_maintenance.shif needed - Update this documentation
Edit the plist file:
# Edit schedule
nano ~/Library/LaunchAgents/com.user.maintenance.weekly.plist
# Reload after changes
launchctl unload ~/Library/LaunchAgents/com.user.maintenance.weekly.plist
launchctl load ~/Library/LaunchAgents/com.user.maintenance.weekly.plistAdd custom environment variables to your shell profile (~/.bash_profile or ~/.zshrc):
# Custom paths
export PATH="/custom/path:$PATH"
# Custom variables for scripts
export BACKUP_LOCATION="/custom/backup/path"
export CLEANUP_AGGRESSIVE="true"- Monitor the logs after the first automatic run
- Adjust the schedule time if needed by editing the plist file
- Add any additional environment variables to the plist if required
- Consider adding notification scripts for completion status
- Set up log rotation if logs grow too large