This updated version of includes an AppleScript controlled system whereby which you can turn maintenance off for discreet periods of time (when giving a presentation, or if you’re working on a video editing project and don’t want any heavy disk usage by any other processes). Also, the script has been improved, and has much better console output than before. Lastly, if you change the way the script is called by Chron, you can make sure maintenance won’t run when your laptop is running off the battery.

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.

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.

  1. Download the XJanitor script.
  2. If your browser did not do so automatically, decompress the downloaded file using Stuffit Expander. You should have a folder with the following files:, XJanitorControl, and the License.
  3. 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, or /private/var/root/Library/Scripts.

    The rest of this example assumes you will put it in your Library/Scripts folder.

  4. Move into the Library/Scripts folder in your user directory. Move the XJanitorControl script to /Library/Scripts (so it’s available for all users on your system). 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, 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 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.

  5. Now open your “Terminal” Application (/Applications/Utilities/Terminal). At the blinking prompt type:
    chmod 700 ~/Library/Scripts/

    and hit return. This will allow only you and the system itself to run this script.
  6. 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!
  7. 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/

    If you are installing this on a laptop, you might want to add this line instead:
    15   *   *   *   *   root   @AppleNotOnBattery   /Users/yourusername/Library/Scripts/

    This tells the crontab to only run XJanitor if you’re plugged in. This can help save some extra juice.

    The spaces between items on this line are tabs, not spaces!

  8. Now, enter an octothorpe (#) 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

    #minute hour mday month wday who command
    #*/5 * * * * root /usr/libexec/atrun
    # Run daily/weekly/monthly jobs.
    15 * * * * root /Users/yourusername/Library/Scripts/

    #15 3 * * * root periodic daily
    #30 4 * * 6 root periodic weekly
    #30 5 1 * * root periodic monthly

  9. 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.
  10. 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.

The XJanitorControl script should be pretty straightforward. Simply run the AppleScript when you want to suspend maintenance for a specific period of time. It will ask you for how long (3 hours to 1 week) you want maintenance suspended. You simply choose a time period and that’s it. Maintenance will not be performed until the time is up. However, if you restart your computer during this time, maintenance will automatically resume.

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/

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/

and will run the script at both quarter past and three quarters past every hour.