Previous section   Next section

Hack 44 Running the Same Thing Over and Over...

figs/moderate.giffigs/hack44.gif

Use cron, the Unix scheduling service, to run a hack at particular times of the day, week, month, or year.

If sending TiVo email [Hack #57] isn't enough to remind you to walk your dog, or if you just want to run the same hack at regular intervals, then cron, the Unix scheduling service, is all you need.

Grab yourself a copy of cron (ftp://ftp.dtype.org/pub/tivo/dtype/cron-3.0pl1_TiVo-2.tar.gz), which M. Drew Streib has compiled.

Set up a directory structure to house the cron binary and sample crontab file�the actual list of scheduled events:

bash-2.02# mkdir /var/hack/etc
bash-2.02# mkdir /var/hack/etc/cron.d
bash-2.02# mkdir/var/hack/cron

Move both files over to your TiVo [Hack #36], dropping cron into /var/hack/bin and crontab into /var/hack/etc. Make cron executable:

bash-2.02# chmod 755 /var/hack/bin/cron

Now you can just start cron and have it run in the background:

bash-2.02# cron &

To actually schedule events, you are going to need to edit the crontab file directly. The syntax for this file is a little esoteric, so bear with me. Open up that file using your preferred text editor Section 3.11, and you'll see a sample line for your reference:

# m h dom mon dow    command
*/10 * * * *  date >> /var/hack/cron.test.out

The first five items on that line control the frequency of the command. These columns are the minute, the hour, the day of the month, the month, and the day of the week. You can specify each with numbers, put in a * wildcard to have the event occur at all those intervals (e.g., every minute, every hour, every day, etc.), or divide it using the / sign to have it run every nth of those intervals. Deciphering the previous line, cron is scheduled to run the command�in this case, output the date to a file, /var/hack/cron.test.out�once every 10 minutes at every hour of every day of every month, regardless of the day of the week.

Back to our patient pooch. Let's say we want to send a reminder to your TV screen using newtext2osd [Hack #41] at 6 p.m. every weekday (just when you'd be inclined to forget and watch the evening news) and at 1 p.m. on weekend days. No problem, just add the following lines to /var/etc/crontab:

0 18 * * 1-5   newtext2osd -s 10 -x 10 -y 10 -t "walk the dog"
0 13 * * 0,6   newtext2osd -s 10 -x 10 -y 10 -t "walk the dog"

The first line tells TiVo's cron to remind you at 6 p.m. (that's hour 18, military time) on weekdays (1-5, Sunday being day 0). The second line does the same for Sunday and Saturday (days 0 and 6) at 1 p.m. (hour 13).

Proof positive that Unix can indeed save the integrity of your carpets!

Hacking the Hack

Of course, you can use cron to schedule any of the command-line hacks in this book. Have it generate a new list of your favorite actors [Hack #91] every day to download and use on your web site. Have it clear unnecessary debug files from particular directories weekly so that your partitions [Hack #29] don't get too full. Give anything you find yourself doing on the command line on a regular basis to cron to handle for you; it never forgets.


  Previous section   Next section
Top