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 SideOkay, 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 TaskThis 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 runGive 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 scriptYour 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 taskFigure 5-5. Setting a scheduled task's Advanced PropertiesAfter 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 FunctionHere'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.
Scheduling with Something Besides cronOkay, 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:
Those just won't do, huh? Okay, you have one more option. Using Hosted cron ServicesIf 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:
|