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

Ensure that cron scripts run UNIX
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
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[6,467 views]  

Ensure that cron scripts run | 12 comments | Create New Account
Click here to return to the 'Ensure that cron scripts run' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Quote problem?
Authored by: kwalker on Mar 09, '02 11:56:15PM

Have you tested this? Don't you need to escape the double quotes in q/"`hostname` daily output"/?



[ Reply to This | # ]
Quote problem?
Authored by: genecutl on Mar 11, '02 11:44:24AM

Yes, you do need to escape the quotes by preceding them with backslashes. Thanks for pointing that out.



[ Reply to This | # ]
Fix the #$^%&* GeekLog Bug!!!
Authored by: thinkyhead on Mar 12, '02 04:18:17AM

I'm going to keep pointing this out until Rob or somebody at Mac OS X Hints fixes it: Backslashes are being eaten by GeekLog! Somewhere in the GeekLog code there's a misplaced "stripslashes" command that has caused many many scripts posted here to be broken and useless. The fix would take 5 minutes. Unfortunately going back and fixing all the scripts stored in the database is going to take a lot longer.



[ Reply to This | # ]
This seems to do it...
Authored by: Stephan on Mar 10, '02 03:45:23AM
Perl complained about the original script and I didn't need the mailing stuff. This script uses ' ` ' with the shell commands.
#!/usr/bin/perl
system `sh /etc/daily 2>&1 | tee /var/log/daily.out`
if !-e '/var/log/daily.out' or -M '/var/log/daily.out' > 1;
system `sh /etc/weekly 2>&1 | tee /var/log/weekly.out`
if !-e '/var/log/weekly.out' or -M '/var/log/weekly.out' > 7;
system `sh /etc/monthly 2>&1 | tee /var/log/monthly.out`
if !-e '/var/log/monthly.out' or -M '/var/log/monthly.out' > 30;


[ Reply to This | # ]
This seems to do it...
Authored by: kwalker on Mar 10, '02 10:13:21AM

If you use the backquotes (`), the "system" at the start of the line is unnecessary. It would be better to retain the "system" and use regular single quotes (').



[ Reply to This | # ]
Use anacron instead of cron
Authored by: Anonymous on Mar 10, '02 03:59:52AM

I installed anacron via Fink <http://fink.sourceforge.net/> and let it run the daily/weekly/monthly-scripts instead of cron. The good thing with anacron is that it understands that computers can by turned off or sleeping. It checks when the scripts was last run and if it is more than e.g. a day/week/month it will run them.

It's very easy to set up since the anacron setting file (from Fink) is prepared to do this. Just comment out the tasks in crontab and uncomment them in anacrontab. Then add a task in cron that will run anacron every once in a while. It works very well!



[ Reply to This | # ]
Run cron from sleep
Authored by: mattegan on Mar 10, '02 06:55:27AM

You can have your Mac wake from sleep and run the crontab.

Open your System Preferences
Select the Enegy Saver
Click on the Option tab
Select 'Wake for network Administrative Access'

This will let your cron shell script run at 3AM ;-)

Also - you could download and install CronniX 1.5
This is a great GUI to edit the times when it runs the scripts.



[ Reply to This | # ]
what about laptops?
Authored by: JazzDude on Mar 10, '02 01:10:58PM

will these scripts run on an iBook with the lid closed? i found that every time i wake up the thing by moving the mouse it immediately goes back to sleep (as you can tell by the pulsating light).



[ Reply to This | # ]
No options tab...
Authored by: rusto on Mar 11, '02 08:20:20AM
...on my EnergySaver pref panel, see screenshot here. I'm running 10.1.3 on a G3/350.

[ Reply to This | # ]
Run cron from sleep
Authored by: genecutl on Mar 11, '02 11:51:31AM

I do have "Select 'Wake for network Administrative Access'" checked, but cron wasn't running which is why I made my perl script.

I did come across Cronnix which looked like it did the same thing, but I always prefer a 3-line perl script to a $15 shareware product. In fact, when you can get the job done with a 3-line perl script why look for anything else?



[ Reply to This | # ]
Run cron from sleep
Authored by: sven on Mar 21, '02 03:30:02PM

Hm, CronniX is freeware :-)



[ Reply to This | # ]
Run cron from sleep
Authored by: zarf on Mar 17, '02 07:45:13PM

Um, changing the wake for network administrative access setting has never worked for me to make run the cron scripts.



[ Reply to This | # ]