Learning the Mac OS X Terminal, Part 5
Subject:   Cronning for non 24 hour computers
Date:   2002-07-03 13:41:56
From:   kwidholm
Chris, another excellent article! However, since a lot of Macs aren't used as 24 hour servers, and are put to sleep often, using Cron the old fashioned way doesn't really hold up for regular maintenance to be performed. I have written a Perl script that gets executed every hour which 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 have output to a log file (and any task can be made to do so). The script also sends an e-mail only if the task(s) have been executed, including full script output.

Here's the script. You can also download it from <>

#!/usr/bin/perl -w
use strict;

# Janitor script. Run through root cron on a daily or hourly basis. The script
# will check for daily, monthly, weekly maintenance, and, if necessary
# will perform the maintenance needed.
# By Kristofer Widholm, <>
# A script like this should always be in the public domain 'cause anybody
# can do it. Accordlingly, so is this script.
my($summary) = "";
my($done) = 0;

my($admin) = "admin"; # Local mail address (usually username) of sysadmin
my($hostname) = `hostname`; # Or whatever you call your machine
my($sleep) = 60; # How many seconds should the script pause between each executed script?

# Check modified time on out files
# Pass the name of the test, the logfile, the script name,
# and the interval at which the script should run, expressed in seconds
# Array of: test name -Location of log -Script -Interval
$tests[0] = (["daily","/var/log/daily.out","/etc/daily",86400]);#1 day
$tests[1] = (["weekly","/var/log/weekly.out","/etc/weekly",604800]);#7 days
$tests[2] = (["monthly","/var/log/monthly.out","/etc/monthly",2419200]);#28 days
# Add other scripts you want to run here:
$tests[3] = (["quarterly","/var/log/quarterly.out","/Users/admin/Library/Scripts/quarterly",7889400]);#91.3 days
#$tests[4] = (["test","/Users/kit/Desktop/test.out","/Users/kit/Desktop/test",60]);

for ($i = 0; $i <= $#tests; $i++) {
@fileinfo = stat($tests[$i][1]); #[$i][1] is the output file
$sinceMod = time() - $fileinfo[9]; #Last modified is index 9 in file info array returned by stat
if ( $sinceMod > ($tests[$i][3]) ) { #index [$i][3] is the running interval
if (system("$tests[$i][2] >& $tests[$i][1]") > -1) {
$summary .= ($i+1) . "> XJanitor ran the $tests[$i][0] script:\n";
$summary .= `cat $tests[$i][1]`;
$summary .= "\n\n";
if ($i < $#tests) { sleep($sleep); } # Sleep between scripts, but not after last one.
else {
$summary .= ($i+1) . "> ERROR: XJanitor could not run the $tests[$i][0] script!!!\n";
else {
$summary .= ($i+1) . "> XJanitor did not need to run the $tests[$i][0] script.\n";

if ($done > 0) { # Don't bother me with mail if scripts weren't run
open(MAIL,"| mail -s \"XJanitor output ($hostname)\" $admin");
print MAIL `date` . "\n";
print MAIL "$summary";

1 to 3 of 3
  1. Cronning for non 24 hour computers
    2002-07-12 21:32:41  dm2243 [View]

  2. Cronning for non 24 hour computers: Addendum on permissions
    2002-07-03 13:55:24  kwidholm [View]

  3. Cronning for non 24 hour computers: URL Fix
    2002-07-03 13:47:46  kwidholm [View]

1 to 3 of 3