Maintenance Shell Script – iMac (Lion)

Shell script performs certain maintenance tasks on OS X 10.7. It has been verified on OS X 10.5

##### Declaring some functions

iMac_uptime()
{
echo “”
echo -n “This iMac has been running for ”
uptime | awk ‘{print $3, $4, $5 “mins” }’ | sed ‘s/,/ /g’ | sed ‘s/:/ hour /g’
}

cacheSize()
{
SIZE=0
for a in `du -sk /Library/Caches/ \
/System/Library/Caches/ ~/Library/Caches/ | cut -f 1`
do
SIZE=`expr $SIZE + $a`
done
SIZEINMEGABYTE=`expr $SIZE \/ 1024`
echo “The Caches size is $SIZEINMEGABYTE MB”
}

clearCache()
{

cacheSize

echo -n “cleaning Caches … ”
sudo rm -r ~/Library/Caches/*
sudo rm -r /Library/Caches/*
sudo rm -r /System/Library/Caches/*
echo done

cacheSize
}

repairPermissions()
{
sudo diskutil repairPermissions / \
| grep -v “We are using special permissions for the file or directory” \
| grep -v “We are using a special gid for the file or directory”
# If you have more then one disk/partition please copy the code above
# and replace the “/” on the first line of code with the mount point of
# your partition / disk. Example: /Volumes/MyDisk
}

{
sudo diskutil verifyVolume /
# If you have more then one disk/partition please copy the code above
# and replace the “/” on the first line of code with the mount point of
# your partition / disk. Example: /Volumes/MyDisk
}

runPeriodic()
{
echo “running periodic jobs … ”
sudo periodic daily weekly monthly
echo done
}

updatePrebindings()
{
echo “updating prebinding information … ”
sudo update_prebinding -root /
echo done
}

verifyPreferenceFiles()
{
sudo plutil -s ~/Library/Preferences/*.plist
sudo plutil -s /Library/Preferences/*.plist
}

menu()
{
clear
date
echo ”

1 Repair Filesystem Permissions
2 Verify Disks
3 Run Periodic (cron) jobs
4 Update Prebindings
5 Verify Preference Files
6 Display Caches size
7 Clear Caches
8 All
u iMac Uptime
q Quit Script


echo -n “Please enter your choice: ”
read CHOICE
}
################################################################################

while true
do
menu
case $CHOICE in

1)
repairPermissions
echo “”
echo -n “Press ENTER to continue”
read
;;

2)
verifyDisks
echo “”
echo -n “Press ENTER to continue”
read
;;

3)
runPeriodic
echo “”
echo -n “Press ENTER to continue”
read
;;

4)
updatePrebindings
echo “”
echo -n “Press ENTER to continue”
read
;;

5)
verifyPreferenceFiles
echo “”
echo -n “Press ENTER to continue”
read
;;

6)
cacheSize
echo “”
echo -n “Press ENTER to continue”
read
;;

7)
clearCache
echo “”
echo -n “Press ENTER to continue”
read
;;

8)
verifyDisks
repairPermissions
verifyPreferenceFiles
runPeriodic
updatePrebindings
clearCache
echo “”
echo -n “Press ENTER to continue”
read
;;

u|U)
iMac_uptime
echo “”
echo -n “Press ENTER to continue…”
read
;;

q|Q)
exit 0
;;

*)
echo menu
;;

esac
done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s