Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  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 <http://www.theapotek.com/teknotes/XJanitor.pl.sit>


#!/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, <http://www.theapotek.com>
# A script like this should always be in the public domain 'cause anybody
# can do it. Accordlingly, so is this script.
# SCRIPT VARIABLES
my($summary) = "";
my($sinceMod,$i);
my($done) = 0;
my(@fileinfo,@tests);
# END SCRIPT VARIABLES



# YOUR CONFIGURATION VARIABLES
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]);
# END YOUR CONFIGURATION



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";
$done++;
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
chop($hostname);
open(MAIL,"| mail -s \"XJanitor output ($hostname)\" $admin");
print MAIL `date` . "\n";
print MAIL "$summary";
close(MAIL);
}
exit;

Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • Cronning for non 24 hour computers
    2002-07-12 21:32:41  dm2243 [View]

    I got an error

    Use of uninitialized value in subtraction (-) at /etc/XJanitor.pl line 38.

    and the script stalled.

    thoughts?

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

    If you want to use the above script for the maintenance tasks, you'll need to be sure that the user running the script has permission to run the daily, weekly, monthly scripts as well as permission to output to the log directory. I have set up a separate admin user on my machine, and granted rights only to this user to these files. The /var/log directory is owned by admin, and in general I've let admin take over many of the tasks normally performed by root, in order to minimize the amount of processes someone could hijack.

    Alternatively, for system-wide administration, run XJanitor.pl from the system cron, and for personal ones, run a different copy of XJanitor.pl, with different parameters set to run scripts and output that require no administrative privileges (as in the backing up example above). Each user could have his or her own copy of XJanitor.pl in her ~/Library/Scripts folder, for example.
  • Cronning for non 24 hour computers: URL Fix
    2002-07-03 13:47:46  kwidholm [View]

    The actual URL for the script mentioned above is <http://www.theapotek.com/teknotes/XJanitor.sit>. Evidently, my Web host won't let anybody download files with '.pl' in them unless they're in the cgi-bin directory.