Since a lot of Macs aren’t used as 24-hour servers, and are often sleeping or simply shut down, using Cron the old fashioned way doesn’t really hold up for regular UNIX maintenance to be performed.
This page is for an old version. An updated version with full instructions is available at http://theapotek.com/teknotes/archives/000040.html#000040. The new version is only for 10.2 and higher.
I have written a Perl script that simply checks to see how long it’s been since a particular task has been performed, compares the time with how often that task should be performed, and if necessary, executes the task. The script is extensible to any maintenance tasks that output to a log file (and any task can be made to do so). The script also includes the option to send you an e-mail (but only if the task(s) have been executed) including full script output.
Please note: You must have administrative privileges on your Mac in order to install and run this script.
- Download the XJanitor script.
- If your browser did not do so automatically, decompress the downloaded file using Stuffit Expander. You should have a new file called XJanitor.pl
- Decide where you want the script to live. If you have no clue where to put it, put it in the Library/Scripts folder of your home directory. If you’re a UNIX geek, you might put it in /etc.
The rest of this example assumes you will put it in your Library/Scripts folder.
- Move your decompressed file (XJanitor.pl) into the Library/Scripts folder in your user directory. If, when you open the Library directory of your home folder, you notice that you don’t have a Scripts folder, feel free to create one.
If you open XJanitor.pl, DON’T hit save!!!. The script is written in a runtime compile scripting language called Perl. Perl assumes its scripts have UNIX line endings. If you open up the script in a text editor, or word processor, it will be given Mac line endings, and if you subsequently save the file, you will cause XJanitor.pl to fail. If you want to examine the script to make sure it doesn’t do anything nefarious, just DON’T save it afterwards, or make sure to save it with UNIX line endings (from within, for example, BBEdit). Of course, if you’re using BBEdit, you probably don’t need these instructions.
- Now open your “Terminal” Application (/Applications/Utilities/Terminal). At the blinking prompt type:
chmod 700 ~/Library/Scripts/XJanitor.pl
and hit return. This will allow only you and the system itself to run this script.
- Now type:
sudo pico /etc/crontab
and hit enter. You will be prompted for your password (it’s the same one as your Mac OS X login). From now on, be extra detail oriented and careful!
- You will be presented with a document called ‘crontab’. You can’t use the mouse here; instead, use the arrow keys on your keyboard to navigate. If you scan through the document, you will find a line like this:
# do daily/weekly/monthly maintenance
Directly under this line, add another line like this, using tabs to line it up in columns:
15 * * * * root /Users/yourusername/Library/Scripts/XJanitor.pl
The spaces between elements should actually be tabs!
- Now, enter a pound sign (#) in front of the next three lines. This will tell the system not to run the commands on these lines. (There is no need to run them since our script has superseded these.) In the end, your crontab should look similar to this (but don’t be disturbed if the stuff above our changes is a bit different):
# /etc/crontab SHELL=/bin/sh PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin HOME=/var/log # #minute hour mday month wday who command # #*/5 * * * * root /usr/libexec/atrun # # Run daily/weekly/monthly jobs. 15 * * * * root /Users/yourusername/Library/Scripts/XJanitor.pl #15 3 * * * root periodic daily #30 4 * * 6 root periodic weekly #30 5 1 * * root periodic monthly
- Now just hit the control and x keys on your keyboard (ctrl-x). Pico will ask if you want to save your changes. Just type “y” for yes, and then hit return to write the new crontab and exit pico.
- Exit your Terminal session by typing exit at the prompt, hitting return, and then quitting the application.
Your system is now set up! The next time your computer is on at fifteen minutes past the hour it should execute all the maintenance scripts. From then on, it will execute the maintenance as scheduled, on a daily, weekly, and monthly basis, as long as you keep your computer running for an hour from time to time.
To see if your script is working, look at the following files’ modification dates: /var/log/daily.out, /var/log/weekly.out, and /var/log/monthly.out from time to time and see if the daily.out is older than a day or two, the weekly.out older than a week, and the monthly.out older than a month. Or, just check your /var/log/XJanitor.out file to see what happened the last time the script ran.
If you want to modify how often this script runs, please read up on how to use Cron, and simply edit the clock parameters. For example, for some people it might be enough to just check three times a day, morning, afternoon, and evening, like this:
15 9,15,21 * * * root /Users/yourusername/Library/Scripts/Xjanitor.pl
Just be aware that the maintenance tasks might not be run as regularly if you don’t have your computer on at one of these times.
For the Geeks: As noted above, you can also easily have the script send mail to you on each execution. Just fill in the $admin variable in the Perl script, and make sure you’ve set up sendmail correctly.
If you never use your computer for at least an hour, you A#1) are lucky, B#2) Whatever, C#3) probably don’t need any maintenance scripts run on your computer, or D#4) you may need to adjust the timing of the Cron above. The line you add should look like this:
15,45 * * * * root /Users/yourusername/Library/Scripts/XJanitor.pl
and will run the script at both quarter past and three quarters past every hour.