Just like your car, your Mac needs routine maintenance in order to run smoothly. Unlike your car, your Mac can perform some of this maintenance on its own. Two important tasks to perform regularly are Disk Utility’s Repair Permissions and OS X’s Unix maintenance scripts.
Repair Permissions ensures that system-level files have the correct privileges set; this is important because incorrect permissions can prevent applications from launching, cause problems with printing, and even affect startup. OS X’s Unix maintenance scripts perform tasks that free up disk space and keep the OS tidy by updating and backing up databases, cleaning out temporary files, and cleaning up system logs.
You should occasionally run Repair Permissions and the maintenance scripts when you boot your Mac into OS X. However, instead of having to remember to do these tasks yourself, you can tell your Mac to do them for you, using the built-in Unix utility cron and the CronniX utility (donations accepted; www .koch-schmidt.de/cronnix/).
Schedule Repair Permissions
1. Meet Cron
To automate an action with cron, you need to open a crontab (a cron table, or schedule) and then create a new entry — much as you’d create a repeating calendar appointment — that tells cron what to do and when.
CronniX gives you an easy-to-use interface for working with crontabs. Launch CronniX and then choose File: Open System Crontab. (Because these are system-level tasks, you’re going to edit the system-level crontab; however, users also have individual crontabs.)
You’ll notice that the default crontab already has three entries; I’ll talk about them later. Right now, you want to create a new entry, so click on the New button. The resulting schedule window lets you create a schedule for the desired action and then designate that action.
I run Repair Permissions once a week, Friday mornings at 10:15 a.m. (Whatever day and time you choose, make sure it’s a time when your Mac will be on and awake.) Using my setup as an example, enter 15 in the Minute field and 10 in the Hour field (cron uses 24-hour time, so 3 p.m. would be 15). These two fields tell cron to execute this action at 10:15 a.m. The Month and Day Of Month options are unimportant, so put a checkmark in those options’ boxes, or type an asterisk (*) in the value field — this tells cron to execute the event for any value of these fields. Finally, select the Fr (for Friday) option.
2. Tell Cron What to Do
Next you need to tell cron what you want it to do at the specified time. Although you can run Repair Permissions from Disk Utility, you can also run it from Terminal via a Unix command; this is the command we’ll tell cron to run.
In the Schedule window’s Command field, first erase the example contents, and then type the following line:
diskutil repairPermissions /
The slash (/) indicates that you want to run Repair Permissions on the startup volume. This command doesn’t give you any feedback; when you run Repair Permissions from Disk Utility, you get a report of the repairs it makes.
I like to see this report, so I’ve used the following command instead (it should all be on one line, and case is important):
diskutil repairPermissions / >> /Users/user name/Desktop/permissionrepairs.txt
(Replace user name with your user name.) Now your Mac will save a text report of repairs on the Desktop.
Make sure the Prepend “/usr/bin/open” option is not selected, and then click on the New button to create the new event. Your new scheduled event will appear in the crontab. Unfortunately, to test it, you’ll have to wait until the time it is supposed to run — CronniX’s handy “Run Now” command (available from the Task menu) doesn’t always work for system crontab entries. If all goes well at the scheduled time (and if you’ve elected to save a report), the report should show up on your desktop after permissions have been repaired.
Before exiting CronniX, click on the Save button to save the changes you made to the crontab. CronniX will ask for your administrator user name and password to verify that you have permission to make system-level changes. Your new crontab entry takes effect immediately. Repair Permissions will run in the background at the specified time, without your having to lift a finger.
Schedule the Scripts
1. Edit Daily Schedules
OS X comes with three Unix maintenance scripts — daily, weekly, and monthly — which you see in the default system crontab. These scripts are already scheduled to run on a daily, weekly, and monthly schedule, respectively. However, because of a long Unix history we won’t get into, the default times (the daily script runs at 3:15 a.m.; the weekly, at 4:15 a.m. on Saturday; and the monthly, on the first day of each month at 5:30 a.m.) mean that the scripts will never run for Mac users who shut down their Macs or put them to sleep at night.
Utilities such as Brian Hill’s free MacJanitor (
https://personalpages.tds.net/~brian_hill/macjanitor.html ) and Atomic Bird’s $8 Macaroni (www.atomicbird.com) let you manually run these scripts or periodically check to see whether they’ve run, but it’s more convenient to adjust the schedules so the scripts run when your Mac is likely to be awake. You’ll use CronniX again, but this time, you’ll edit the existing entries for the daily, weekly, and monthly scripts.
In CronniX, open the system-level crontab. In the Command column, you’ll see the three cron events that run these scripts.
Select the daily (here, periodic daily) entry and click on the Edit button. In the schedule window that appears, change the Minute and Hour fields from 15 and 3, respectively, to a time when your Mac is generally on and awake. I changed mine to 12 p.m., when I’m often at lunch, by entering 00 in the Minute field and 12 in the Hour field. (Leave the other fields alone, since you still want the script to run every day.) Click on Apply to save your changes.
2. Edit Weekly Schedules
Now you’ll alter the weekly day and time.
Select the entry for the weekly script and click on Edit. If you don’t typically use your Mac on weekends, deselect the Sa (for Saturday) option and select one of the week-day options. Then edit the Hour and Minute fields to reflect a time when your Mac will be on and awake. (Avoid choosing the same time you used for the daily script.) Click on Apply.
3. Edit Monthly Schedules
Making your final change to the monthly schedule is a bit trickier.
Since the monthly script is set to run on the same date each month, if the chosen date happens to fall on a weekend and your computer isn’t running on weekends, the script won’t run. For this reason, you should create two crontab entries for the monthly script.
Select the entry for the monthly script and click on Edit. I recommend keeping the Day Of Month field set to 1 (which means the script will run on the first day of every month). Enter appropriate Hour and Minute values, just as you did for the daily and weekly scripts. (I use 12:45 p.m.) Click on Apply to save changes.
Next, select the entry you just edited in CronniX and choose Edit: Duplicate Task. Select the duplicate entry, click on Edit, and then change the Day Of Month field to a different day. I recommend 5 — since it’s impossible for both the first and fifth days of any month to fall on a weekend, this ensures that your monthly maintenance script will run the first week of every month. (In some months, both of these dates will fall on weekdays, meaning the script will run twice. Don’t worry — this does no harm.) Click on Apply.
4. Don’t Forget to Save
You’re almost ready to sit back and let the scripts do your maintenance work.
Click on the Save button and then provide your admin user name and password. Your edits will take effect immediately and your maintenance scripts will run during normal working hours. If you’ve been using Mac OS X for a while and you’ve never run these scripts, you could gain a significant amount of hard-drive space the first time they run because of their cleaning actions.
5. More Maintenance
Running Repair Permissions and the maintenance scripts regularly will go a long way toward maintaining smooth operations on your Mac, but they don’t fix everything.
You should also run Disk Utility’s Repair Disk once a month or so, on both your startup drive and any additional volumes. Repair Disk checks your hard drive for damage and directory problems; such damage, if it’s serious enough, can prevent your Mac from even booting. It’s also a good idea to occasionally run a more advanced disk-repair utility such as Alsoft’s $80 DiskWarrior (800/257-6381, www.alsoft.com) to ensure that your disk’s directory is in tip-top shape.
Keeping your Mac running smoothly requires some work, but automating some of the tasks as described here, and maintaining a regular schedule of disk care, should grant you a stabler and more trouble-free experience in the long run.
If you don’t have a Unix background, the cron utility is most likely foreign to you. However, it’s one of the simplest and most useful Unix utilities you’ll find in OS X. Cron runs in the background whenever your Mac is on. Every minute, it checks schedules called crontabs to see whether any command or script is supposed to run. If so, it runs them. If not, it takes a nap for 60 seconds, then checks again, and so on.
Anything you can do via a Terminal command, Unix shell script, or Mac OS AppleScript, you can run via cron at a specific day, date, and time. (The step-by-step section of this column provides you with a couple of good examples.) You can even perform simple actions such as launching an application or opening a document (using the Terminal command open).
Crontabs — the schedules that tell cron what should run and when — are located in the invisible /etc/crontab directory, for the system crontab, and /var/cron/tabs, for user-specific crontabs. (Although we’re editing the system-level crontab in this article because we’re performing system-level tasks, you should generally edit only your own user-level crontab.) The crontab format is a bit messy. Each event consists of a single line with values for the minute, hour, day of month, month, day of week, user name, and command. For example, the default crontab entry that runs OS X’s weekly maintenance script is:
15 4 * * 6 root periodic weekly
This entry tells cron that at 4:15 a.m. every Saturday (the sixth day of the week — the week starts on Monday), regardless of the month or the day of the month, the root user should run the periodic weekly command. At 4:15 a.m. Saturday morning, cron finds a match for that entry and then runs the command. User-specific crontabs omit the user name variable, since all events in a user’s crontab are executed as that user.
You can view the system-level crontab by typing cat /etc/crontab in Terminal; likewise, you can edit crontabs in Terminal with the crontab command. However, viewing and working with crontabs in Terminal gets pretty messy, and a utility such as CronniX makes the process easier. CronniX lets you access your own user-level crontab or, assuming your account has administrative access, the system-level (root) crontab. Entries for the chosen crontab are displayed in a format similar to Terminal’s, but with labeled columns that you can rearrange if you find them easier to read in a different order. You can also create a new entry (via the New button), delete an entry (by selecting it and clicking on Delete), enable or disable any entry, or click on the Edit button to edit the selected entry. When you’re done, click on the Save button to save your changes, which take effect immediately. CronniX also lets you run a particular entry on cue (via the Run Now button). To view a different crontab in CronniX, choose Open For User or Open System Crontab from the File menu.
To learn more about cron, read the cron manual (type man cron in Terminal) or visit the CronniX Web site at www.koch-schmidt.de/cronnix/docs/index.html.