Check system maintenance tasks at startup

Mar 18, '05 09:58:00AM

Contributed by: ld

System maintenance for laptops can be a pain, seeing as though the standard crontab entries are set to run at times when both you and your computer are asleep. Changing these times is not a solution, as it does not guarantee they run. A previous hint, Run programs at startup via crontab, outlined that type of solution. That's fine for the daily script, but not for the weekly or monthly scripts. I've instead created a startup item called Periodic which tests the log files created by the periodic script which is run by cron at those sleepy times.

Right, enough talk ... issue the following commands in Terminal unless otherwise specified. Note that scripts that have #!/bin/sh at the top need that as the first line.

Step 1:Create the Startup Item:

$ sudo mkdir -p /Library/StartupItems/Periodic
$ cd /Library/StartupItems/Periodic
$ sudo pico Periodic
Now copy and paste this code into pico. Press Control-X to exit pico (answering "Y" when asked to save). Now type the following commands:
$ sudo chmod +x Periodic
$ sudo pico StartupParameters.plist
Paste the following into pico and then exit as above.
{
  Description     = "Task: /usr/sbin/periodic [daily | weekly | monthly]";
  Provides        = ("Daily", "Weekly", "Monthly");
  Requires        = ("DirectoryServices", "Disks", "Network Time");
  Uses            = ("NFS");
  OrderPreference = "Last";
  Messages =
  {
    start = "Starting Periodic";
    stop  = "Stopping Periodic";
  };
}
Step 2: add /etc/weekly.local script:

This will help with our other scripts pattern matching for weeks:
$ sudo chmod +x /etc/weekly.local
$ sudo pico /etc/weekly.local
Paste in the following code, exiting and saving as before:
#!/bin/sh

theLog="/var/log/weekly.out"
thisWeek=`date '+%U'`
thisYear=`date '+%Y'`

if [ ! -f $theLog ]; then
    touch $theLog
fi

echo "Year:$thisYear Week:$thisWeek [`date '+%a %b %e %H:%M:%S %Z'`]" >> $theLog
That's it. Now each time you reboot, the startup item Periodic will check to see whether it needs to run a maintenance task that was skipped.

[robg adds: I haven't tested this one...]

Comments (12)


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