Ensure that cron scripts run

Mar 09, '02 08:39:51PM

Contributed by: Anonymous

Having put together some incremental backup software, I was very annoyed to find that cron wasn't running the /etc/daily script because cron doesn't run while my computer is asleep, and my computer is always asleep at 3:15am when /etc/daily is set to be run.

So, I made a very simple perl script that fixes this problem. It checks the modification times for the daily, weekly, and monthly log files. If they are more than a day, week, or month old, respectively, it runs the appropriate commands.

To learn more, read on...

There are two steps:

First save the text below as a perl script in a good place, I chose to call it /usr/local/bin/crontastic.pl. You can use something like pico or emacs in the terminal or an app like BBEdit that can save with Unix line endings:

#!/usr/bin/perl
system "sh /etc/daily 2>&1 | tee /var/log/daily.out | mail -s "`hostname` daily output" root"
 if !-e '/var/log/daily.out' or -M '/var/log/daily.out' > 1;

system "sh /etc/weekly 2>&1 | tee /var/log/weekly.out | mail -s "`hostname` weekly output" root"
 if !-e '/var/log/weekly.out' or -M '/var/log/weekly.out' > 7;

system "sh /etc/monthly 2>&1 | tee /var/log/monthly.out | mail -s "`hostname` monthly output" root"
 if !-e '/var/log/monthly.out' or -M '/var/log/monthly.out' > 30;
Make sure that the script is executable by, by typing this in the terminal:
chmod a+x /usr/local/bin/crontastic.pl
Now, adjust your /etc/crontab file. First comment out each of the daily, weekly, and monthly lines, by putting a "#" at the beginning of those line so that cron won't try to run those lines anymore. Second, tell cron to run the crontastic script every half hour (I figure everyone's computer will be awake for at least a half hour at a time):
*/30  *  *  *  *  root  /usr/local/bin/crontastic.pl
And there you have it.




On a semi-related matter, to tell that the cron script has started running, in each of my /etc/daily, /etc/monthly, and /etc/weekly files, I have put a version of this code near the beginning:
now=`date "+%A %B %e, %l:%M %p"`
/usr/bin/osascript <<EOF
say "Running cron monthly on $now"
EOF
Note that those are back-tics on the now=... lines and not single quotes.
This is based on a recent tip and will have your computer tell you that it's running the file.

--Gene

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020309203951180