Hack 91 Scheduling Tasks Without cron

figs/beginner.gif figs/hack91.gif

If you want to run any of the hacks in this book on a regular basis, your best option is to use cron, a powerful Linux-based scheduler. But what if you're on a different OS or don't have access for some other reason?

You've probably noticed that we mention cron often in this book. cron is a task scheduler for Unix that lets you set up a file of scheduled jobs (a crontab ) that will run automatically without your supervision.

That's the good news. The bad news is that many web-hosting services do not offer access to cron . Alternatively, you might be using Perl on an operating system without cron (Windows, Mac OS 9, etc.). If you're in that boat, all is not lost. There are several alternatives to standard cron , from just going about the problem a different way to finding third-party services.

Do You Really Need Anything cron-Like?

Yes, nothing satisfies your mad scientist jones like the thought of dozens of little Perl scripts, waiting until exactly the right moment to be unleashed onto the world. But think about it a minute. Do you really need to use cron to run your scripts? Can you just run your scripts when the need arises? Make sure you actually have to use timed scheduling before you go any further with these alternatives.

Running Scripts on the Client Side

Okay, you have to schedule your scripts somehow. If you're not using your scripts in a web-hosting environment, why not try something on the client side? There are many scheduling utilities out there for Windows and Mac; just visit a software site like http://www.download.com and search for " scheduling utility " or " schedule utility ".

Windows users already have one on board. You'll find Scheduled Tasks under My Computer or Control Panels. And it's easy as pie to get up and running. For a new entry, choose Add Scheduled Task, as shown in Figure 5-1.

Figure 5-1. Adding a Scheduled Task
figs/sphk_0501.gif

This will start the Scheduled Task Wizard. Next, choose the program you'd like to schedule (see Figure 5-2). Since you'll be setting up one of the scripts in this book, rather than using a garden-variety Windows application, choose Browse to pinpoint its location on your hard drive.

Figure 5-2. Browsing for a script to run
figs/sphk_0502.gif

Give the task a name and choose a schedule for it, as shown in Figure 5-3.

Figure 5-3. Creating a schedule on which to run the script
figs/sphk_0503.gif

Your options will differ depending on your choice of frequency. In my case, I chose Daily, so my options look like those shown in Figure 5-4. You'll have a chance to tweak your settings further by choosing the Advanced Properties checkbox on the wizard's final screen, as shown in Figure 5-5.

Figure 5-4. Further tweaking our new Daily task
figs/sphk_0504.gif
Figure 5-5. Setting a scheduled task's Advanced Properties
figs/sphk_0505.gif

After you schedule your new task, it'll be listed in the Scheduled Tasks folder andso long as your computer is on at the timewill run when you've requested .

Using Perl's sleep Function

Here's a cheap and cheesy way to make sure your Perl script runs regularly: don't ever let it stop. Just jacket your Perl program with a sleep statement:

 while (1) { sleep 14400;  [your code here]  } 

The number 14000 represents the number of seconds the program will pause.

If you're using your scripts in a web-hosting environment, this particular way around cron is likely to irritate the average ISP. Be sure to ask before you start running it. This kind of workaround is really designed for those who have their own boxes and, for whatever reason, can't use cron .


Scheduling with Something Besides cron

Okay, so you really, really, really need to use cron . Or at least you need some way to run a script on a regular basis. You don't have to use cron ; you can use fake cron . There are a couple of scripts available online:


Fake Cron (http://www.smarterscripts.com/cron/)

Launches scripts based on an image loading in one of your web pages. While you don't need cron to get this program to work, you will need the LWP::UserAgent module.


Crone (http://mareksscripts.w2w.cc/txt/crone_readme.txt)

A script scheduler that works either by loading via an image tag or via a SSI call. You can set it to run based on time or after every x visitors to a page. If you have sendmail , Crone will also send you an email upon execution.

Those just won't do, huh? Okay, you have one more option.

Using Hosted cron Services

If you just have to use cron and you're not willing to take the big step of moving your site to another, more cron -friendly host than the one you have now, there's one more option: cron services. Just as there are web-hosting services, there are services that will schedule cron jobs on your behalf . Here are two of them:


Script Schedule (http://www.script-schedule.com)

Offers the ability to schedule jobs or run jobs when triggered by an email. Pricing ranges from free to $15 a year.


Fastcron (http://www.fastcron.com/)

Doesn't appear from its web site to be as extensive as Script Schedule, but it has a flat rate of $9.95 a year.



Spidering Hacks
Spidering Hacks
ISBN: 0596005776
EAN: 2147483647
Year: 2005
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net