O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



Putting periodic to work
Mac OS X uses the periodic tool to rotate logs, clean up temporary files, and other system administration tasks. With a few changes you can put periodic to work performing your tasks as well as receive its output by email.

Contributed by:
Jason Deraleau
[04/16/03 | Discuss (0) | Link to this hack]

In order to perform the necessary administrative tasks on your system, Mac OS X makes use of the cron daemon and the periodic utility. The periodic script gets called at three different intervals: Daily at 3:15 AM, Weekly on Saturday at 4:30 AM, and Monthly on the 1st at 5:30 AM. If your machine doesn't run during these times, you can use a utility like Maintain Your Cron to run the scripts at a more convenient time. This hack should still work in conjunction with such a tool.

Sometimes you install crucial utilities that you'd really rather not have consuming your time. Maybe you want Fink to update itself every week. Maybe you have Virex and you want to scan your machine for viruses every day. If the application can be run from the command line (or even if it can be AppleScripted through osascript!), you can create a custom periodic file to execute it auotmatically for you.

At one point when the periodic application runs its various scripts, it attempts to execute a script of the matching name with .local appended to it. It looks for this file in the /etc folder. For example, the daily periodic script attempts to run commands in /etc/daily.local. You can make use of this to run your CLI utilities. Below are two examples of scripts that run common utilities. Remember that to create these files in /etc, you need the permissions of root, so use sudo or su.

#!/bin/sh
#
# /etc/daily.local
#

# General parameters
PATH=/bin:/usr/bin:/sbin:/usr/sbin
. /etc/hostconfig

# Update Fink, if present
if [ -f /sw/bin/fink ]; then
 /sw/bin/fink selfupdate-cvs && /sw/bin/fink update-all
fi


#!/bin/sh
#
# /etc/weekly.local
#

# General parameters
PATH=/bin:/usr/bin:/sbin:/usr/sbin
. /etc/hostconfig

# Check disk permissions
/usr/sbin/disktool repairPermissions /

# Virus scan with Virex
echo "Scannning for viruses..."
cd /usr/local/vscanx
./vscanx -rc --summary --ignore-links --one-file-system / >/dev/null

These are great examples to get you started. By default, the output of your periodic scripts are put in a file in /var/log that is named after the script and then appended with .out (e.g. /var/log/daily.out). If you'd instead like to have these logs emailed to you, add a line that says "MAILTO=myemail@mydomain.tld" to the top of your /etc/crontab file. Then, create a file called /etc/periodic.conf.local that contains the following:

# Output options
daily_output=""
weekly_output=""
monthly_output=""

Keep on hacking!

See also: man periodic, man cron


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.