Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iCeFuSiOn

macrumors 6502a
Original poster
Jul 18, 2007
511
0
If you're having any problems with your Mac and you are using Mac OS X 10.5, then you're in luck. I've taken the time to write a shell script that you can execute via Terminal which will allow you to perform any of the following tasks:

  • Run maintenance scripts (daily, weekly, and monthly).
  • Flush user cache and user-level log files.
  • Flush global cache (/Library/Cache)
  • Flush system cache (/System/Library/Cache)
  • Check for corrupted preference files (user and global)
  • Flush Directory Service cache
  • Flush Apple Type Server (ATS) -- useful if you have corrupted font problems.
  • Update application prebinding
  • Verify disk integrity
  • Repair permissions on boot volume.

This is for all of you who don't feel like installing a third party program like Onyx to do the dirty work.

I've attached the file in a .ZIP to this post, just extract and run it from Terminal. All clean-up options are selectable so you don't have to run the same ones every time you run the script.

NOTE: As with any maintenance tasks, you should always have a recent backup on hand just in case for whatever reason something happens.

For those of you who are interested, here's the code:

Code:
#! /bin/bash

#	=====================================================
#	* Mac OS X Maintenance Script						*
#	* Written by Kristan M. Kenney (iCeFuSiOn)		    *
#	* Last modified on February 11, 2008 4:47:18 AM AST *
#	* Revision 1.0.1b									*
#	* For use on Mac OS X 10.5 "Leopard" Only!			*
#	* I AM NOT RESPONSIBLE IF YOU DO SOMETHING WRONG!   *
#   =====================================================

clear
echo "Mac OS X 10.5 Maintenance Script"
echo "WARNING: Close any running applications before continuing."
echo ""
echo -n "Would you like to proceed? (y/N): "; read PROCEED
if [ -z $PROCEED ] || [ "$PROCEED" = "n" ] || [ "$PROCEED" = "N" ]; then
  exit 0
fi
echo ""
echo -n "Run maintenance cron scripts (daily, weekly, and monthly)? (Y/n): "; read CRONSCR
if [ -z $CRONSCR ] || [ "$CRONSCR" = "y" ] || [ "$CRONSCR" = "Y" ]; then
	sudo periodic daily weekly monthly
	echo "Maintenance scripts have run successfully."
	echo ""
fi
echo ""
echo -n "Flush user cache and log entries? (Y/n): "; read USRLOG
if [ -z $USRLOG ] || [ "$USRLOG" = "y" ] || [ "$USRLOG" = "Y" ]; then
	echo "Flushing saved logs under current users Home directory ..."
	cd ~/Library/Logs
	sudo rm -rf ~/Library/Logs/*

	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 "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 ..."
	sudo rm -rf `lsof | grep com.apple.ATS/annex.aux \
	| grep Finder | cut -c 66-139`
	sudo rm -rf /private/var/folders/*/*/-Caches-/com.apple.ATS
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 "Enable logging for verification process (y/N): "; read LOG_VERIFICATION
if [ -z $LOG_VERIFICATION ] || [ "$LOG_VERIFICATION" = "n" ] || [ "$LOG_VERIFICATION" = "N" ]; then
	echo "Verifying disk integrity and repairing disk permissions ..."
	echo "Please note, this may take several minutes."
	sudo diskutil rerpairVolume /
	sudo diskutil repairPermissions /
else 
	echo "Verifying disk integrity and repairing disk permissions ..."
	echo "Please note, this may take several minutes."
	sudo diskutil rerpairVolume / > ~/Library/Logs/maintenance_script.log
	sudo diskutil repairPermissions / > ~/Library/Logs/maintenance_script.log
fi
echo ""

# All done! We can go ahead and restart the computer now.
echo "Maintenance tasks have completed."
echo "If you have performed system level tasks, please restart now."
echo "Otherwise, please log out and back in."

You can save the above code in a TextEdit document, just ensure you save it with an extension of .sh for the sake of simplicity. To make it executable, open Terminal, navigate to the location of the document, and run:

Code:
chmod a+x [filename].sh

I've fully tested this on 10.5.1 and there are no adverse effects or anything like that. Hope this helps, have a good day everyone! :)
 

Attachments

  • maintenance_script.zip
    2 KB · Views: 995

mrflip

macrumors newbie
Jul 3, 2007
2
0
If you run
Code:
	cd ~/Library/Logs
	sudo rm -rf *
and for whatever reason ~/Library/Logs didn't exist, you would suddenly become unhappy according to the formula
unhappiness = importance of location when you launched this script / recency of last backup.​
consider instead
Code:
	cd ~/Library/Logs && sudo rm -rf ~/Library/Logs/*
and maybe even do a (ls -lR | head ; printf "\n...\n<snip>\n...\n\n" ; ls -lR | tail) and then ask the user to confirm.

Honestly though -- like the WuTang clan, sudo rm -rf * is nothing to f' with. Be very careful running something like this from a script.
 

SaSaSushi

macrumors 601
Aug 8, 2007
4,156
553
Takamatsu, Japan
If you're having any problems with your Mac and you are using Mac OS X 10.5, then you're in luck. I've taken the time to write a shell script that you can execute via Terminal which will allow you to perform any of the following tasks:
  • Run maintenance scripts (daily, weekly, and monthly).
  • Flush user cache and user-level log files.
  • Flush global cache (/Library/Cache)
  • Flush system cache (/System/Library/Cache)
  • Check for corrupted preference files (user and global)
  • Flush Directory Service cache
  • Flush Apple Type Server (ATS) -- useful if you have corrupted font problems.
  • Update application prebinding
  • Verify disk integrity
  • Repair permissions on boot volume.
This is for all of you who don't feel like installing a third party program like Onyx to do the dirty work.

Thank you very much but doesn't this already exist?:

Maintenance Automator Script
 

iCeFuSiOn

macrumors 6502a
Original poster
Jul 18, 2007
511
0
If you run
Code:
	cd ~/Library/Logs
	sudo rm -rf *
and for whatever reason ~/Library/Logs didn't exist, you would suddenly become unhappy according to the formula
unhappiness = importance of location when you launched this script / recency of last backup.​
consider instead
Code:
	cd ~/Library/Logs && sudo rm -rf ~/Library/Logs/*
and maybe even do a (ls -lR | head ; printf "\n...\n<snip>\n...\n\n" ; ls -lR | tail) and then ask the user to confirm.

Honestly though -- like the WuTang clan, sudo rm -rf * is nothing to f' with. Be very careful running something like this from a script.

mrflip: Thank you for pointing this out to me. I've been used to writing Windows CMD script files now for a while so this one was new to me. I've gone ahead and made the change in the script, let me know if it's any better.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.