Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Check system maintenance tasks at startup System
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...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[13,407 views]  

Check system maintenance tasks at startup | 12 comments | Create New Account
Click here to return to the 'Check system maintenance tasks at startup' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Check system maintenance tasks at startup
Authored by: goatbar on Mar 18, '05 10:37:08AM

What about anacron?



[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: cwford on Mar 18, '05 10:37:42AM

Actually there is already an available utility to do this called anacron.

Anacron is a periodic command scheduler. It executes commands at intervals specified in days. Unlike cron, it does not assume that the system is running continuously. It can therefore be used to control the execution of daily, weekly and monthly jobs (or anything with a period of n days), on systems that don't run 24 hours a day. When installed and configured properly, Anacron will make sure that the commands are run at the specified intervals as closely as machine-uptime permits.

It also works great for my powerbook where I generally put it to sleep instead of shutdown/reboot.

You can find the package at http://www.apple.com/downloads/macosx/unix_open_source/anacron.html



[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: jpbjpbjpbjpb on Mar 21, '05 10:58:03AM

I've been using anacron for years now to deal with cron tasks on my laptop.

It builds easily, but you can install it with fink too, if you're uncomfortable with the command line.



[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: chabig on Mar 18, '05 11:49:19AM
Macaroni is also an excellent tool. It's a System Preference panel that takes care of running the scripts, repairing permissions, and running the user's own scripts, all automatically.

[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: RBR on Apr 02, '05 09:41:06AM

Macaroni is an excellent UI for running the periodic maintenance. It waits until the Mac has been idle for a period of time (the user can set the time) and then does its thing without intervention by the user...you can't forget it.

This is what Apple should have built into the OS in the first place.

It also has a delocalizer for removing unneeded languages.

It is well worth the $8.99.



[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: elmimmo on Mar 18, '05 11:57:14AM

Unfortunately this does not solve the problem for those who do NOT turn their computers off but have them sleeping at default system maintenance hours.

XJanitor is dead easy to set up AND does exactly the same as anacron, except that it is intended ONLY to make sure the system maintenance are carried on (while I guess you can use anacron for whatever other cron jobs too) whether your computer was off or *sleeping* at the default hours and without stressing your computer more than needed.

You just place the perl script somewhere on your hard-drive (/Library/Application Support or whatever), edit your system crontab with a single line and it gets the job done.

Besides, you can control wether you want to turn system maintenance temporarily off (so that it does not trigger at some unfortunate time when you need full processing power) and you can make sure it does not run when running your notebook on battery.

By its description, I guess one can make anacron do the same as XJanitor. I simply know only the latter. In any case, it is at least as easy (or even easier) to setup as this hint, and useful to a broader audience than the audience of this hint

.

[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: ld on Mar 21, '05 09:42:56PM

Hi there,

you're right. It doesn't help (on its own) for those who leave their laptops on. It wasn't meant to be a one-size-fits-all tip. But something that I decided to use. I'm a programmer and so like to find (and in so doing learn) ways of doing things myself rather than simply pull down a ready-made tool. Ready-made tools don't teach you about the system.

So, If there's other tools out there that fit the bill for your purposes - that's great. Having a choice is good. The other good thing about all the feedback is seeing where this type of hint could be improved. That's the great thing about this forum.

Anyway, the /Library/StartupItems is a standard built-in system tool. I was interested in putting something into place that met the following criteria:

  1. Compliments the stock system tasks (i.e., without removing/replacing them)
  2. Uses the log files produced by these tasks to verify their execution

If the thing that's missing is, "may not run if sleeping rather than rebooted" then it would only take a little more creativity to fit that bill... (e.g., setting an hourly cron job that kicks off the startup item).


@hourly /Library/StartupItems/Periodic/Periodic "start"

And only a little bit more creativity is required to enable you to turn this feature off for specific times of high-volume processing... e.g., create an AppleScript that's available from your Script Menu that will toggle the renaming of the StartupItem script (placing a script with the name Periodic in the right place so that the cron job doesn't get upset. (Left as an excercise for someone who's got a bit of spare time.)

---
regards,
LD

[ Reply to This | # ]

Check system maintenance tasks at startup
Authored by: peragrin on Mar 18, '05 12:46:25PM
I use MacJanitor I run it not on a schedule but remember to manually turn it on weekly or so. Since I don't turn my powerbook off, it just goes to sleep. I haven't rebooted or even logged out since 10.3.8 update.

---
I thought once I was found but it was only a dream

[ Reply to This | # ]

Check system maintenance tasks at startup
Authored by: Felix_the_Mac on Mar 18, '05 02:59:50PM

My favoured solution would be as follows:

1) Script to run Periodic tasks when the screen saver is activated

2) Only run tasks when the necessary time has elapsed since last run, i.e.Weekly = 7 days

3) Each time sleep is entered a check is performed to see if any of the Periodic tasks need to be run. The first one which needs to be run (in order of daily,weekly,monthly) will be executed, then the job will end.

4) The next time the screen saver activates the check is run and normally the 'daily' will be up to date so the weekly or monthly will execute. This should stop occurences were all the scripts get run 1 after the other which would be unacceptable if you came back to use the machine with the task was still running.

Does htis sound like a good system to you?
Can we do it?



[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: simsamsep on Mar 18, '05 04:44:25PM
I have been using the cronaid script in Jaguar and Panther and it works perfectly. You never need to think about the three maintenance scripts again, no user action needed at all, no startup items, no application. The jobs will automatically be executed daily, weekly and monthly when your machine is awake.

From its ReadMe:
cronaid will check to see if your maintenance scripts have not been run and will run them when the computer is awake. If they have been run, it does nothing.

For each of the three system maintenance scripts (daily, weekly, monthly) cronaid checks the date on the log of output for the script against the last time the script should have been run. The time is calculated by looking at the entry for the script in the system crontab file. If the script run is overdue, it is run. More precisely, the exact command entered in the crontab file is executed. This way it is exactly as if cron had run the entry had the computer been on.

[ Reply to This | # ]
cronaid
Authored by: sjk on Apr 03, '05 10:17:15PM
Thanks for the cronaid reference. Minor correction -- it's not a script:
% file cronaid
cronaid: Mach-O executable ppc


[ Reply to This | # ]
Check system maintenance tasks at startup
Authored by: osxpounder on Mar 21, '05 12:29:37PM

I think you mean "because" instead of "seeing as though".

---
--
osxpounder



[ Reply to This | # ]