Terminal 101: Setting Up and Running Disk Maintenance
Posted 07/09/2012 at 11:00am
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Hardware maintenance is one way to ensure that your Mac has a long and prosperous life of reading and writing bits, but software maintenance is also very important to preserving the speed and performance of your machine. By default, your Mac will automatically perform scheduled maintenance at regular daily, weekly, and monthly intervals; however, it may be important to run some of these scripts more often than prescribed by OS X. Read on to learn how to check when the scripts last run, and run them manually through the Terminal.
Why Run these Scripts Manually?
Occasionally, the scripts may not run properly (or at all) if your computer is frequently shut down or in sleep mode. In this case, these scripts allow you to manually invoke the scripts to ensure that OS X is properly cleaning up and rotating the log files.
What is a log file rotation?
In Unix-based systems, it is important to rotate the log files on daily, weekly, or monthly intervals to ensure that the log file doesn’t get out of hand. Without rotating the log files, they will constantly grow until they eventually expand to take all available disk space. To prevent that, rotation of the files is important. When a log file is rotated, the current log is archived with a date/time stamp. When the files become old, they will be deleted from the system to conserve disk space.
1. Determining When the Scripts Last Ran

The first step in running the scripts is to see when the maintenance scripts last ran. To do this, you’ll want to open Terminal and type in the following command:
ls -al /var/log/*.out
When you press the Return key, you will see a short list of the date and times that the daily, weekly, and monthly scripts were last run by OS X.
2. Running the Daily Maintenance Script

The daily maintenance script removes old log files, “scratch” and “junk” files, backs-up important system databases, compiles reports on systems and network statistics, and rotates the system.log file so that the logs don’t become unwieldy. After OS X 10.5 (Leopard), the daily script also prunes the asl.db file (Apple System Logging file).
By default, this script will run automatically every day at 03:15 local time.
To run this script manually, you’ll want to type in the following command into the Terminal, followed by the enter key:
sudo periodic daily
You will be prompted to authenticate as an administrator user in order to run the scripts by entering your account password. After doing that, the script will begin to run. Note that you will not be given feedback while the script is running. Do not close the Terminal window while the script is running. After the script has finished running, the prompt will return, allowing you to type another command.
3. Running the Weekly Maintenance Script

The weekly script rebuilds the locate and whatis databses, and (depending on the version of OS X), will rotate the following log files: ftp.log, lookupd.log, lpr.log, mail.log, netinfo.log, ipfw.log, ppp.log, and secure.log.
By default, the script is scheduled to run every Saturday at 03:15 hours local time. Before OS X 10.4 (Tiger), the script was scheduled to run at 04:30 hours local time.
To run this script manually, we’ll use the same command as above, only substituting the “daily” with “weekly,” like so:
sudo periodic weekly
As before, you will be prompted to authenticate as an administrator
4. Running the Monthly Maintenance Script

The monthly script creates per-user usage reports and rotates (depending on OS X version), the following log files: wtmp, install.log, and cu.modem.log.
By default, this script will automatically be run on the first day of the month at 05:30 hours local time.
You can run this maintenance script manually by typing the following command into the terminal:
sudo periodic monthly
5. Running the Scripts Together

You can run the daily, weekly, and monthly scripts together by entering the following command into the Terminal:
sudo periodic daily weekly monthly
You will be required to authenticate as an administrator before running the scripts.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.