A simple Mac OS X maintenance shell script

I, like so many of my fellow Macintosh users want to keep my OS X installation well maintained. So i sat down and read a few man pages describing those nifty cli utilities that are included in OS X. Since OS X is a UNIX style operating system I decided to create a small shell script to perform the following maintenance tasks for me .

Repair Filesystem Permissions
Verify Disks
Run Periodic (cron) jobs
Update Prebindings
Verify Preference Files
Displa Cache size
Clear Cache

If you find the script interesting and want to use it on your mac you need to perform a few simple steps:

1. Copy the script to a text editor. I prefer vi or TextEdit.app.
2. Save the file. I will use the file name “MaintainMyMac.sh” during the rest of this description.
3. Make the file executable. To do that start Terminal.app and type chmod +x <path to MaintainMyMac.sh>
4. Now you can run the shell cript. E.g. ./MaintainMyMac.sh

I hope that you find this script as useful as I do.

You will be using this script at your own risk. I will not be responsible for loss of data, dogs, cats, keys or anything else.

#!/bin/bash
# This shell script performs system maintenance tasks in OS X
# It has been verified on OS X 10.5
 
##### Declaring some functions
 
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
}
 
verifyDisks()
{
  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
  echo "
 
  1 Repair Filesystem Permissions
  2 Verify Disks
  3 Run Periodic (cron) jobs
  4 Update Prebindings
  5 Verify Preference Files
  6 Displa Caches size
  7 Clear Caches
  8 All
  q Quit MaintainMyMac
  "
  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
    ;;
 
    q|Q)
    exit 0
    ;;
 
    *)
    echo menu
    ;;
 
  esac
done
Jan 26th, 2009 | Posted in Computers

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">