#! /bin/bash# =====================================================# * Mac OS X 10.5 General Maintenance Script *# * Written by Kristan M. Kenney *# * Last modified on April 11, 2008 6:05:04 PM ADT *# * Code Revision M7H290 r1 *# * ================================================= *# * I AM NOT RESPONSIBLE IF YOU DO SOMETHING WRONG! *# =====================================================# American parts, Russian parts, they're all made in Taiwan!clearecho "********************************************************************"echo "* Mac OS X 10.5 Maintenance Script *"echo "* Revision 1.1.0 v. M7H290 [11/04/08 6:04 PM] *"echo "********************************************************************"echo ""echo "This utility allows you to flush several caches on your computer,"echo "execute maintenance tasks, check the integrity of your preferences,"echo "verify and repair your start-up volume and repair permissions, as"echo "well as reset system preferences to Apple defaults and more."echo ""echo "**WARNING!** This script can run several tasks as 'root' by using"echo "the 'sudo' command. When used improperly,'sudo' can cause severe"echo "damage to your operating system and data."echo ""echo "As with any maintenance application or task, you should always have"echo "a current backup of your data on another volume or an external disk."echo ""echo "Your Mac is running the following system software:"sw_versecho -n "Would you like to proceed with maintenance tasks? (y/n): "; read PROCEEDif [ -z $PROCEED ] || [ "$PROCEED" = "n" ] || [ "$PROCEED" = "N" ]; then exit 0ficlearsudo -vclearecho ""echo "Would you like to run all the tasks at once, or would you like"echo "to run each command seperately (providing control over which"echo -n "maintenance tasks are executed? (ALL/man): "; read TASK_SELECTIONif [ -z $TASK_SELECTION ] || [ "$TASK_SELECTION" = "all" ] || [ "$TASK_SELECTION" = "ALL" ] || [ "$TASK_SELECTION" = "a" ] || [ "$TASK_SELECTION" = "A" ]; then# =========================================================================================================================== # AUTOMATIC MAINTENANCE MODE # Performs all system tasks without prompting the user for selection. # This is ideal if you are looking for a quick spring cleaning. # Quit all running applications except for the maintenance script # to ensure that there are no conflicts or data corruption. echo "Quitting all open applications, please wait ..." osascript -e 'set exclude to {"Finder"}' \ -e' tell application "System Events"' \ -e 'set end of exclude to displayed name of (info for (path to frontmost application))' \ -e 'set app_list to name of every process whose visible is true' \ -e 'end tell' \ -e 'repeat with this_app in app_list' \ -e 'if this_app is not in exclude then' \ -e 'tell application this_app' \ -e 'activate' \ -e 'quit' \ -e 'end tell' \ -e 'end if' \ -e 'end repeat' echo "" sleep 5 echo "" echo "Running all maintenance tasks, please wait ..." echo "" # RING 2 RING 2 RING 2 RING 2 RING 2 RING 2 RING 2 # Contains basic maintenance tasks which can be run without elevation. echo "Running maintenance cron tasks, please wait ..." echo "NOTE: This process can take several minutes to complete..." sudo periodic daily weekly monthly # RING 1 RING 1 RING 1 RING 1 RING 1 RING 1 RING 1 # Contains maintenance tasks which require sudo elevation. echo "Flushing user-level logs ..." cd ~/Library/Logs sudo rm -rf ~/Library/Logs/* echo "Flushing user cache ..." cd ~/Library/Caches sudo rm -rf ~/Library/Caches/* echo "Flushing global cache ..." cd /Library/Caches sudo rm -rf /Library/Caches/* # RING 0 RING 0 RING 0 RING 0 RING 0 RING 0 RING 0 # Contains tasks which require sudo elevation which touch upon system-level functions. echo "Flushing system cache ..." cd /System/Library/Caches sudo rm -rf /System/Library/Caches/* echo "Updating application prebinding ..." sudo update_prebinding -root / -force echo "Flushing Directory Service resolver cache ..." dscacheutil -flushcache echo "Flushing LaunchServices database ..." /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user echo "Resetting AirPort wireless card ..." /usr/sbin/networksetup -setairportpower off > /dev/null /usr/sbin/networksetup -setairportpower on > /dev/null echo "Flushing Apple Type Services font cache databases (per-user and global) ..." atsutil databases -removeUser > /dev/null sudo atsutil databases -remove > /dev/null echo "Re-launching Apple Type Services server..." atsutil server -shutdown > /dev/null sleep 5 atsutil server -ping > /dev/null echo "Flushing system-level logs ..." cd /var/log sudo rm -rf /var/log/alf.log sudo rm -rf /var/log/hdiejectd.log sudo rm -rf /var/log/daily.out sudo rm -rf /var/log/fsck_hfs.log sudo rm -rf /var/log/system.log sudo rm -rf /var/log/system.log.*.bz2 sudo rm -rf /var/log/windowserver.log echo "Verifying disk integrity and repairing disk permissions ..." echo "Please note, this may take several minutes." sudo diskutil verifyVolume / echo "" sudo diskutil repairPermissions /else# =========================================================================================================================== # MANUAL MAINTENANCE MODE # Run maintenance tasks in a user selectable order (which allows a finer control over what gets flushed, etc.) echo "" echo -n "Run maintenance cron tasks (daily, weekly, and monthly)? (y/n): "; read CRONSCR if [ -z $CRONSCR ] || [ "$CRONSCR" = "y" ] || [ "$CRONSCR" = "Y" ]; then echo "Running maintenance cron tasks, please wait ..." echo "NOTE: This process can take several minutes to complete." sudo periodic daily weekly monthly echo "" fi echo "" echo -n "Flush user-level log entries? (y/n): "; read USRLOG if [ -z $USRLOG ] || [ "$USRLOG" = "y" ] || [ "$USRLOG" = "Y" ]; then echo "Flushing user-level log entries ..." cd ~/Library/Logs sudo rm -rf ~/Library/Logs/* fi echo "" echo -n "Flush user-level cache? (total size: " du -ch {~/Library/Caches/,`getconf DARWIN_USER_CACHE_DIR`} \ | cut -f1 | tail -1 | tr -d '\n' && echo -n ") (y/n): "; read USRCACHE if [ -z $USRCACHE ] || [ "$USRCACHE" = "y" ] || [ "$USRCACHE" = "Y" ]; then echo "Flushing user cache ..." rm -rf ~/Library/Safari/Downloads.plist cd ~/Library/Caches sudo rm -rf ~/Library/Caches/* cd `getconf DARWIN_USER_CACHE_DIR` sudo rm -rf `getconf DARWIN_USER_CACHE_DIR` fi echo "" echo -n "Flush global cache? (y/n): "; read GLOBAL_CACHE if [ -z $GLOBAL_CACHE ] || [ "$GLOBAL_CACHE" = "y" ] || [ "$GLOBAL_CACHE" = "Y" ]; then echo "Flushing global cache ..." cd /Library/Caches sudo rm -rf /Library/Caches/* fi echo "" echo -n "Flush system cache? (y/n): "; read SYS_CACHE if [ -z $SYS_CACHE ] || [ "$SYS_CACHE" = "y" ] || [ "$SYS_CACHE" = "Y" ]; then echo "Flushing system cache ..." cd /System/Library/Caches sudo rm -rf /System/Library/Caches/* sudo rm -rf /System/Library/Extensions.mkext sudo rm -rf /System/Library/Extensions/Caches/com.apple.kext.info fi echo "" echo -n "Check for corrupted preference files on your computer? (y/n): "; read PREF_INTEGRITY if [ -z $PREF_INTEGRITY ] || [ "$PREF_INTEGRITY" = "y" ] || [ "$PREF_INTEGRITY" = "Y" ]; then echo "Checking for corrupted or damaged preference files," echo "any errors will be reported here ..." echo "" echo "* User preferences ..." cd ~/Library/Preferences plutil -s *.plist echo "" echo "* Global preferences ..." cd /Library/Preferences plutil -s *.plist echo "" echo "* System preferences ..." cd /Library/Preferences/SystemConfiguration plutil -s *.plist fi echo "" echo -n "Flush Directory Service cache? (y/n): "; read DSERVCACHE if [ -z $DSERVCACHE ] || [ "$DSERVCACHE" = "y" ] || [ "$DSERVCACHE" = "Y" ]; then echo "Flushing Directory Service resolver cache ..." dscacheutil -flushcache fi echo "" echo -n "Flush LaunchServices database (fixes duplicate Open With entries, etc) (y/n):"; read LS_DATABASE if [ -z $LS_DATABASE ] || [ "$LS_DATABASE" = "y" ] || [ "$LS_DATABASE" = "Y" ]; then echo "Flushing LaunchServices database ..." /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user fi echo "" echo -n "Update application prebinding? (y/n): "; read APP_PREBIND if [ -z $APP_PREBIND ] || [ "$APP_PREBIND" = "y" ] || [ "$APP_PREBIND" = "Y" ]; then echo "Updating application prebinding ..." sudo update_prebinding -root / -force fi echo "" echo -n "Flush Apple Type Server (ATS) cache? (y/n): "; read AppleATS_CACHE if [ -z $AppleATS_CACHE ] || [ "$AppleATS_CACHE" = "y" ] || [ "$AppleATS_CACHE" = "Y" ]; then echo "Flushing Apple Type Services font cache databases (per-user and global) ..." atsutil databases -removeUser > /dev/null sudo atsutil databases -remove > /dev/null echo "Re-launching Apple Type Services server..." atsutil server -shutdown > /dev/null sleep 5 killall SystemUIServer atsutil server -ping > /dev/null fi echo "" echo -n "Flush Spotlight search index on the start-up volume? (y/n): "; read FLUSH_SPOTLIGHT_INDEX if [ -z FLUSH_SPOTLIGHT_INDEX] || [ "$FLUSH_SPOTLIGHT_INDEX" = "y" ] || [ "$FLUSH_SPOTLIGHT_INDEX" = "Y" ]; then echo "Flushing Spotlight search index ..." sudo mdutil -E / > /dev/null fi echo "" echo -n "Update application prebinding? (y/n): "; read APP_PREBIND if [ -z $APP_PREBIND ] || [ "$APP_PREBIND" = "y" ] || [ "$APP_PREBIND" = "Y" ]; then echo "Updating application prebinding ..." sudo update_prebinding -root / -force fi echo "" echo -n "Flush system-level logs? (y/n): "; read SYS_LOGS if [ -z $SYS_LOGS ] || [ "$SYS_LOGS" = "y" ] || [ "$SYS_LOGS" = "Y" ]; then echo "Flushing system-level logs ..." cd /var/log sudo rm -rf /var/log/alf.log sudo rm -rf /var/log/hdiejectd.log sudo rm -rf /var/log/daily.out sudo rm -rf /var/log/fsck_hfs.log sudo rm -rf /var/log/system.log sudo rm -rf /var/log/system.log.*.bz2 sudo rm -rf /var/log/windowserver.log fi echo "" echo -n "Clear the terminal command history? (y/n): "; read CLEAR_BASH_HIST if [ -z $CLEAR_BASH_HIST ] || [ "$CLEAR_BASH_HIST" = "y" ] || [ "$CLEAR_BASH_HIST" = "Y" ]; then echo "Clearing the terminal command history ..." history -c fi echo "" echo -n "Clear the recent items list?"; read FLUSH_RECENT_ITEMS if [ -z FLUSH_RECENT_ITEMS ] || [ "$FLUSH_RECENT_ITEMS" = "y" ] || [ "$FLUSH_RECENT_ITEMS" = "Y" ]; then echo "Clearing the recent items list ..." sudo rm ~/Library/Preferences/com.apple.recentitems.plist fi# =========================================================================================================================== # APPLICATION SPECIFIC MAINTENANCE OPTIONS # These items control things such as Safari cache, Adium chat transcripts, and other temporary files. clear echo -n "Would you like to perform application-specific maintenance as well? (y/n): "; read APP_SPECIFIC_MAINTENANCE_OPTS if [ -z APP_SPECIFIC_MAINTENANCE_OPTS ] || [ "$APP_SPECIFIC_MAINTENANCE_OPTS" = "y" ] || [ "$APP_SPECIFIC_MAINTENANCE_OPTS" = "Y" ]; then echo "" echo -n "Flush Safari cache and data? (y/n): "; read FLUSH_SAFARI if [ -z FLUSH_SAFARI ] || [ "$FLUSH_SAFARI" = "y" ] || [ "$FLUSH_SAFARI" = "Y" ]; then echo -n "Flushing Safari cache (total size: " && du -h `getconf DARWIN_USER_CACHE_DIR`com.apple.Safari | cut -f1 | tr -d '\n' && echo ") ..." rm -rf ~/Library/Safari/Downloads.plist rm -rf ~/Library/Safari/History.plist rm -rf ~/Library/Safari/HistoryIndex.sk rm -rf ~/Library/Safari/LastSession.plist rm -rf ~/Library/Safari/WebpageIcons.db rm -rf ~/Library/Safari/Form\ Values cd `getconf DARWIN_USER_CACHE_DIR`com.apple.Safari rm -rf `getconf DARWIN_USER_CACHE_DIR`com.apple.Safari fi echo "" echo -n "Flush Adium chat transcripts? (y/n): "; read FLUSH_ADIUM_DATA if [ -z FLUSH_ADIUM_DATA ] || [ "$FLUSH_ADIUM_DATA" = "y" ] || [ "$FLUSH_ADIUM_DATA" = "Y" ]; then echo "Flushing Adium chat transcripts ..." rm -rf ~/Library/Application\ Support/Adium\ 2.0/Users/Default/Logs/* fi fi# =========================================================================================================================== # APPLICATION PREFERENCES RESET # These items allow the user to reset certain application preferences to their default Apple values. clear echo -n "Would you like to reset any of your application preferences at this time? (y/n): "; read RESET_SPECIFIC_APPSPREFS if [ -z RESET_SPECIFIC_APPSPREFS ] || [ "$RESET_SPECIFIC_APPSPREFS" = "y" ] || [ "$RESET_SPECIFIC_APPSPREFS" = "Y" ]; then echo "" echo -n "Reset Safari preferences? (y/n): "; read RSPREF_SAFARI if [ -z RSPREF_SAFARI ] || [ "$RSPREF_SAFARI" = "y" ] || [ "$RSPREF_SAFARI" = "Y" ]; then osascript -e 'quit application "Safari"' rm -rf ~/Library/Safari/Downloads.plist rm -rf ~/Library/Safari/History.plist rm -rf ~/Library/Safari/HistoryIndex.sk rm -rf ~/Library/Safari/LastSession.plist rm -rf ~/Library/Safari/WebpageIcons.db rm -rf ~/Library/Safari/Form\ Values rm -rf ~/Library/Preferences/com.apple.Safari.plist rm -rf ~/Library/Preferences/com.apple.Safari.RSS.plist fi echo "" echo -n "Reset Mail preferences? (y/n): "; read RSPREF_MAIL if [ -z RSPREF_MAIL ] || [ "$RSPREF_MAIL" = "y" ] || [ "$RSPREF_MAIL" = "Y" ]; then echo -n "Do you want to preserve old mailboxes and message data? (y/n): "; read RSPREF_MAIL_PRESERVEMAIL if [ -z RSPREF_MAIL_PRESERVEMAIL ] || [ "$RSPREF_MAIL_PRESERVEMAIL" = "n" ] || [ "$RSPREF_MAIL_PRESERVEMAIL" = "N" ]; then echo "Resetting Mail application settings and removing old mailbox and message data ..." osascript -e 'quit application "Mail"' rm -rf ~/Library/Preferences/com.apple.Mail.plist rm -rf ~/Library/Mail else echo "" echo "Resetting Mail application settings and preserving old mailbox and message data ..." osascript -e 'quit application "Mail"' mv ~/Library/Preferences/com.apple.Mail.plist ~/Library/Preferences/com.apple.Mail.plist.old mv ~/Library/Mail ~/Library/Mail.old echo "" echo "NOTE: You can find your previous mailboxes and preferences at the locations below:" echo "" echo "Mailbox and message data: ~/Library/Mail.old" echo "Mail application preferences: ~/Library/com.apple.Mail.plist.old" sleep 5 fi fi fi# =========================================================================================================================== # SYSTEM PREFERENCES RESET # These items control certain system preferences and allows you to reset them to Apple defaults. clear echo -n "Would you like to reset any of your system preferences at this time? (y/n): "; read RESET_SPECIFIC_PREFS if [ -z RESET_SPECIFIC_PREFS ] || [ "$RESET_SPECIFIC_PREFS" = "y" ] || [ "$RESET_SPECIFIC_PREFS" = "Y" ]; then echo "" echo -n "Reset Bluetooth preferences? (y/n): "; read RSPREF_BLUETOOTH if [ -z RSPREF_BLUETOOTH ] || [ "$RSPREF_BLUETOOTH" = "y" ] || [ "$RSPREF_BLUETOOTH" = "Y" ]; then cd ~/Library/Preferences rm -rf ~/Library/Preferences/com.apple.Bluetooth.plist cd ~/Library/Preferences/ByHost rm -rf ~/Library/Preferences/ByHost/com.apple.Bluetooth.*.plist cd /Library/Preferences rm -rf /Library/Preferences/com.apple.Bluetooth.plist fi echo "" echo -n "Reset Energy Saver preferences?"; read RSPREF_ENERGY if [ -z RSPREF_ENERGY ] || [ "$RSPREF_ENERGY" = "y" ] || [ "$RSPREF_ENERGY" = "Y" ]; then echo "Resetting Energy Saver preferences ..." sudo rm /Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist sudo rm /Library/Preferences/SystemConfiguration/com.apple.AutoWake.plist sudo rm ~/Library/Preferences/com.apple.systemuiserver.plist killall Finder; killall SystemUIServer fi echo "" echo -n "Reset AirPort wireless connection list? (y/n): "; read RSPREF_AIRPORT_SW if [ -z RSPREF_AIRPORT_SW ] || [ "$RSPREF_AIRPORT_SW" = "y" ] || [ "$RSPREF_AIRPORT_SW" = "Y" ]; then echo "Resetting AirPort wireless card ..." echo "" echo "* Turning AirPort card off ..." /usr/sbin/networksetup -setairportpower off > /dev/null echo "Done." echo "" echo "* Clearing previous wireless connection information ..." cd /Library/Preferences/SystemConfiguration rm -rf /Library/Preferences/SystemConfiguration/NetworkInterfaces.plist rm -rf /Library/Preferences/SystemConfiguration/com.apple.network.identification.plist rm -rf /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist echo "Done." echo "" echo "* Turning AirPort card on ..." /usr/sbin/networksetup -setairportpower on > /dev/null echo "Done." fi fi# =========================================================================================================================== # DISK INTEGRITY # Allows the user to verify integrity of the start-up volume. clear echo "Would you like to verify and repair disk permissions on the start-up volume?" echo -n "(this is highly recommended to ensure filesystem consistency) (y/n): "; read VERIFY_DISK_PERMISSIONS if [ -z $VERIFY_DISK_PERMISSIONS ] || [ "$VERIFY_DISK_PERMISSIONS" = "y" ] || [ "$VERIFY_DISK_PERMISSIONS" = "Y" ]; then echo -n "Enable logging for verification process (y/n): "; read LOG_VERIFICATION if [ -z $LOG_VERIFICATION ] || [ "$LOG_VERIFICATION" = "y" ] || [ "$LOG_VERIFICATION" = "Y" ]; then echo "Verifying disk integrity and repairing disk permissions ..." echo "Please note, this may take several minutes." sudo diskutil verifyVolume / > ~/Library/Logs/maintenance_script.log echo "" sudo diskutil repairPermissions / > ~/Library/Logs/maintenance_script.log echo "Repair operation completed. If you wish to review the log, it is located at" echo "~/Library/Logs/maintenance_script.log" else echo "Verifying disk integrity and repairing disk permissions ..." echo "Please note, this may take several minutes." sudo diskutil verifyVolume / echo "" sudo diskutil repairPermissions / fi fifi# ========================================================================================================================== # END OF MAINTENANCE COMMANDS # All maintenance tasks have been completed, so we can go ahead and clear the screen to # remove previous input, and then prompt the user to either log off, restart, or simply quit.sleep 2clearecho "ALL MAINTENANCE TASKS HAVE BEEN COMPLETED..."echo "***************************************************************************"echo "It is advised that if you have performed any system level tasks"echo "such as flushing the system cache, or updating application pre-binding,"echo "that you restart your computer."echo ""echo "If you have not run system level tasks and have only cleared out your user"echo "cache, it is safe to simply log out and log back in for the changes to"echo "take effect."echo ""echo "Please note: If you've cleared all of the caches on your system, you may"echo "notice that it takes a few seconds longer for your applications to load."echo "This is normal, as the system must re-build the cache. If you've performed"echo "system-level cache flushing, you may also notice similar behavior on the"echo "boot process. Subsequent boot attempts will be as fast as before, if not"echo "slightly faster."echo "***************************************************************************"echo ""# Present the user with the correct options. Default action is "quit" if an invalid choice# has been made.echo -n "Would you like to clear all SUDO command history? (y/n): "; read SUDOCLEAR if [ -z $SUDOCLEAR ] || [ "$SUDOCLEAR" = "y" ] || [ "$SUDOCLEAR" = "Y" ]; then echo -n "Would you like to log off, restart, or quit? (l/r/q): "; read EXIT_CHOICE if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "q" ] || [ "$EXIT_CHOICE" = "Q" ]; then sudo -K exit 0 else if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "l" ] || [ "$EXIT_CHOICE" = "L" ]; then sudo -K echo "" && echo "Logging out in 5 seconds ..." sleep 5 osascript -e 'ignoring application responses' \ -e 'tell application "loginwindow"' \ -e' «event aevtrlgo»' \ -e 'end tell' \ -e 'end ignoring' else if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "r" ] || [ "$EXIT_CHOICE" = "R" ]; then echo "" && echo "Shutting down in 5 seconds..." sleep 5 sudo shutdown -r now sudo -K else exit 0 fi fi fi else echo -n "Would you like to log off, restart, or quit? (l/r/q): "; read EXIT_CHOICE if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "q" ] || [ "$EXIT_CHOICE" = "Q" ]; then exit 0 else if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "l" ] || [ "$EXIT_CHOICE" = "L" ]; then echo "" && echo "Logging out in 5 seconds ..." sleep 5 osascript -e 'ignoring application responses' \ -e 'tell application "loginwindow"' \ -e' «event aevtrlgo»' \ -e 'end tell' \ -e 'end ignoring' else if [ -z $EXIT_CHOICE ] || [ "$EXIT_CHOICE" = "r" ] || [ "$EXIT_CHOICE" = "R" ]; then echo "" && echo "Shutting down in 5 seconds..." sleep 5 sudo shutdown -r now else exit 0 fi fi fi fi# End of script.