Clearing Safari Cache Automatically when it Quits

I work for a school district that uses Safari 2.0.4 on eMac’s running 10.4.7 and 10.4.8 and the students have their network home folders located on a 10.4.8 Server.

I would like to have Safari clear its cache every time a student quits Safari or logs out, but don’t see a way to have this done automatically. Is there a script or utility out there that clears the cache automatically?

Thanks for the help!

MacBook Pro, Mac OS X (10.4.8)

Posted on Dec 7, 2006 7:55 AM

Reply
11 replies

Dec 11, 2006 1:24 PM in response to ewalk

Hi ewalk,

No probs. It's a good workout for the old grey matter! 🙂

I learnt scripting on Solaris servers at work so can't recommend an OS X specific book as such (I've never read one) but I always found this book very useful. O'Reilly books tend to pretty good, so if I had to get a more bash specific one I'd probably go for Learning the bash Shell.

Re: Workgroup Manager, now you are stretching my knowledge 🙂 I've never worked with the server version of OS X so I'm not familiar with any 'gotchas' you need to be aware of. I can't see why the script would need to be modified. The standard $HOME variable should still point to the correct user area. If you want to double check before letting it loose, change this line:

rm -rf $HOME/Library/Caches/Safari/* >> $logFile 2>&1


to:

echo "rm -rf $HOME/Library/Caches/Safari/*" >> $logFile 2>&1


That will then just print out what would be executed to the logfile. If the path to the cache folder is correct for the network setup then restore it back to the original command.

Good luck!

User uploaded file

iMac G5 20" 2GB RAM, Soundsticks, Netgear 814, Aiport Extreme Mac OS X (10.4.8)

Dec 11, 2006 9:51 AM in response to ewalk

Hello again,

Ok, let's give this a go then 🙂

Personally, for editing scripts I like to use the command line 'vi' editor, but for starting with that may scare you off so lets use the regular TextEditor application for the time being.

Fire up TextEditor and open a new document. Change the type of the document to be a plain text file. This can be done via the Format menu -> Make Plain Text option (if it says Make Rich Text, then the file is already in plain text mode).

In the editor, copy and paste the following, but remove the line numbers I've added:

1 #!/bin/bash
2
3 logFile=/tmp/SafariCleanerLog.txt
4
5 echo "-----------------" >> $logFile
6 echo `date` >> $logFile
7 echo "" >> $logFile
8 echo "Deleting Safari cache files in: $HOME/Library/Caches/Safari/" >> $logFile 2>&1
9 echo "" >> $logFile
10
11 rm -rf $HOME/Library/Caches/Safari/* >> $logFile 2>&1


Some lines may appear to wrap around in the webpage, so use the line numbers as your guide to know if something is supposed to fit on one line or not.

This is what it does:
- The 1st line indicates what particular shell application to use to execute this script (/bin/bash in this case)
- The 3rd creates a variable that I'm using to store the path to a log file for what this script is doing
- Lines 5 to 9 append some logging information to the logfile we've decided to report to
- Line 11 does the actual deletion of the Safari cache files. rm is short for remove, aka delete, and the -rf means recursively (i.e. all subdirectories in the Safari cache folder) and forceably (i.e. don't askif you are sure for each and every file). The cryptic 2>&1 at the very end is to make any error messages that rm might generate appear in our logfile. If you try out the commands to see how they work, please please please be very careful with rm, once a file has been rm'ed its gone - it doesn't go to the Trashcan

Note that if you want to see more about what each of these commands do, you can use the man pages either from the Terminal itself or here. ('echo' is a built-in command to bash, so you would need to read the bash manpage for information on that)

Ok, so now we have a script. Save that on your Desktop with a name that makes sense for you (e.g. SafariCacheCleaner.sh - sh is the standard extension used to indicate something is a script, it's not required)

Now we need to execute a few Terminal commands to make this work when a student logs out. Fire up the Terminal from /Applications/Utilities/Terminal.app

The first thing we need is somewhere to place the script that is out of bounds to regular users, students, etc. OS X already has an area for scripts in /Library/Scripts/, so you may want to create your own area under this folder. For example, I created a 'Custom' folder via the following:

sudo mkdir /Library/Scripts/Custom


You may want to replace Custom with something else, like SchoolAdmin for instance. sudo is a command that gives the executor temporary administration rights. When you press return after typing the above command, you will be asked for a password - enter the password for your current user account and press return (note that you will not see anything appear in the Terminal while you are typing, this is a security feature).

Next up, we need to copy the script to this location and give it the correct permissions to be executed:

sudo cp ~/Desktop/SafariCacheCleaner.sh /Library/Scripts/Custom/

sudo chmod 755 /Library/Scripts/Custom/SafariCacheCleaner.sh


In this case, sudo will probably not prompt you for the password as it trusts access for a few minutes. If you leave things for, say, 10 minutes, it would ask you for your password again.
cp is the command for copying files/folders.
chmod is used to change permissions on a file. In this case, 7 means the owner of the file can write/read/execute the file, the first 5 means anyone in the same unix group as the owner can read/execute the file and the second 5 means everyone else can read/execute it.

Lastly, we need to tell OS X that we want this script to run everytime a user logs out. This can be done with the following command (this is one long line that the browser may wrap):

sudo defaults write com.apple.loginwindow LogoutHook /Library/Scripts/Custom/SafariCacheReset.sh


Hopefully, that should be it. You can test it out by logging out of your account and logging back in. The /Users/YourUsername/Library/Caches/Safari/ should be sparkling clean now.

I've tried to be a bit verbose to explain what's going on, so it's not as complex as it may look at first.
If you have any questions at all, feel free to ask - it's better to be sure of what you are doing when it comes to the Terminal sometimes.

User uploaded file

Dec 7, 2006 9:26 AM in response to ewalk

Hi,

It should be fairly straightforward to achieve what you wish with a Logout script. The following article discusses running scripts at login, but also alludes to the same feature being available for logout events via the 'LogoutHook' argument:

http://www.bombich.com/mactips/loginhooks.html

Specify the script that performs the removal of the Safari cache location ($HOME/Library/Caches/Safari at a guess) and hopefully that will do the trick.

User uploaded file

Dec 7, 2006 11:32 AM in response to iBod

Thanks for the reply. I appreciate it.

I didn't find a script that specifically removed the Safari cache. Was I missing something? I am not a good script creator, so this might be over my head, but have some resources that might be able to help me.

In the meantime, I found Server Cleanup 2 at:

http://web.mac.com/bancher/iWeb/My%20Utilities/Home.html

This fantastic utility does a bunch of things including removing the Safari and Firefox cache. It also redirects the cache files for these browsers (and many others) to the /tmp folder locally, which I think achieves the same results. I am just not sure when the /tmp folder gets cleared. Does anyone know?

Thanks again for the suggestions.

Dec 7, 2006 11:44 AM in response to ewalk

Hi,

I didn't find a script that specifically removed the Safari cache.


Ah sorry, you mentioned scripts in your original post and I just assumed you were already a little familiar with them.

If you'd like a script I'm happy to whip one up for you that will delete the Safari cache. (It looks Server Cleanup 2 does all you need and more however 🙂

I am just not sure when the /tmp folder gets cleared. Does anyone know?


/tmp is deleted when the machine is rebooted.

User uploaded file

Dec 11, 2006 12:42 PM in response to iBod

This is fantastic. Exactly what I am looking for with an amazing explanation. I truly appreciate all your hard work.

Is there a book that you recommend to help me with scripting? I can get around in Terminal, but when it comes to writing scripts, I am lost.

I am going to execute this as a logout script in WorkGroup Manager. Is there anything that I would need to change because it is a multi-user environment?

Again, many, many thanks.

Dec 13, 2006 2:43 PM in response to ewalk

I realize that your problem has been solved, but there is a much easier way to do this with Onyx, a free OS X utility. Simply check the box that says "remove Safari cache." While you are using Safari, the cache will be filled with whatever you're looking at, but when you quit, it will be emptied. This can be con firmed by looking at the caches window of the Debug menu.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Clearing Safari Cache Automatically when it Quits

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.