#! /bin/bash# =====================================================# * Mac OS X Maintenance Script *# * Written by Kristan M. Kenney (iCeFuSiOn) *# * Last modified on February 23, 2008 9:46:12 PM AST *# * Revision 1.0.5 *# * For use on Mac OS X 10.5 "Leopard" Only! *# * I AM NOT RESPONSIBLE IF YOU DO SOMETHING WRONG! *# =====================================================clearecho "**********************************************************"echo "* Mac OS X 10.5 Maintenance Script *"echo "* Revision 1.0.5 - 23/02/08 9:46 PM *"echo "**********************************************************"echo ""echo "This utility allows you to flush several caches on your"echo "computer, run maintenance tasks (usually run by cron),"echo "check the integrity of your preference files, and verify"echo "and repair your start-up volume and repair permissions."echo ""echo "WARNING! This script can run several tasks as 'root' by"echo "utilizing the 'sudo' command. When used improperly,"echo "'sudo' can cause severe damage to your system."echo ""echo "As with any maintenance application or task, you should"echo "always have a current backup of your data."echo ""echo ""echo -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 ************ echo "" echo "Running all maintenance tasks, please wait ..." echo "" # Execute maintenance cron scripts... echo "Running maintenance cron tasks, please wait ..." echo "NOTE: This process can take several minutes to complete..." sudo periodic daily weekly monthly # Flush all cache data ... 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/* echo "Flushing system cache ..." cd /System/Library/Caches sudo rm -rf /System/Library/Caches/* # Flushing system resolver cache and database information ... 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 # Run maintenance tasks (cron scripts, force application pre-binding update, and verify start-up disk. echo "Updating application prebinding (dyld cache) ..." sudo update_prebinding -root / -force echo "Flushing Apple Type Services font cache databases (per-user and global) ..." atsutil databases -removeUser atsutil databases -remove echo "Re-launching Apple Type Services server..." atsutil server -shutdown > /dev/null sleep 5 atsutil server -ping > /dev/null 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 "Maintenance scripts have completed successfully." 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? (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/* 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/* 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 "* User preferences ..." cd ~/Library/Preferences plutil -s *.plist echo "* Global preferences ..." cd /Library/Preferences 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 (dyld cache) ..." 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 atsutil databases -remove echo "Re-launching Apple Type Services server..." atsutil server -shutdown > /dev/null sleep 5 atsutil server -ping > /dev/null fi 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 # APPLICATION SPECIFIC MAINTENANCE OPTIONS # These items control things such as Safari cache, Adium chat transcripts, and other temporary files. echo -n "Flush Safari cache and data? (y/n): "; read FLUSH_SAFARI if [ -z FLUSH_SAFARI ] || [ "$FLUSH_SAFARI" = "y" ] || [ "$FLUSH_SAFARI" = "Y" ]; then echo "Flushing Safari cache ..." 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 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 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 / fi fi 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 -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 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 echo "" echo -n "Reset AirPort wireless card? (y/n): "; read RSPREF_AIRPORT_HW if [ -z RSPREF_AIRPORT_HW ] || [ "$RSPREF_AIRPORT_HW" = "y" ] || [ "$RSPREF_AIRPORT_HW" = "Y" ]; then echo "Resetting AirPort wireless card ..." echo "" echo "* Turning AirPort card off ..." /usr/sbin/networksetup -setairportpower off > /dev/null echo "Done." echo "" echo "* Turning AirPort card on ..." /usr/sbin/networksetup -setairportpower on > /dev/null echo "Done." 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/com.apple.airport.preferences.plist echo "Done." echo "" echo "* Turning AirPort card on ..." /usr/sbin/networksetup -setairportpower on > /dev/null echo "Done." fi fi echo "" echo -n "Clear the BASH command line history? (y/n): "; read CLEAR_BASH_HIST if [ -z $CLEAR_BASH_HIST ] || [ "$CLEAR_BASH_HIST" = "y" ] || [ "$CLEAR_BASH_HIST" = "Y" ]; then echo "Clearing BASH command line history ..." history -c fi echo "" echo "Would you like to verify and repair disk permissions on the start-up volume?" echo -n "(this is highly recommended to ensure consistency of the start-up volume) (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# 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 5clearecho "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 # TODO: Add AppleScript command line code to execute clean log out. 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 # TODO: Add AppleScript command line code to execute clean log out. 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.