Main Page Content
Cron A Regular Workhorse
So, you can write CGI scripts. And they get kicked off when a user goes to your page, and fills in a form.But what if you want to have things going on even if you don't get any users? What if you want to run a big database job nightly, ready for your first morning's visitor? Or maybe you want to have an autoresponder check a POP box every few minutes, and fire out appropriate response emails?If you're on a Unix-type server, you're in luck. *nix can kick off your script at any given time interval, without your intervention, and we're going to show you how, using an autoresponder Perl script as an example.
What you'll need:
- A Unix (including Linux) web server where your scripts are living;
- Telnet access to that server (shouldn't be a problem - try throwing your Telnet application at www.yourdomain.com using the user ID & password you use for FTP);
- A text editor and FTP application;
- A non-paranoid sysadmin who hasn't disabled cron - if they let you install your own CGIs, you'll probably be OK.
- For the demo script we're using, you'll need Perl, with the Mail::POP3 module, a POP box which the module can access and sendmail access (ask your sysadmin about these).
- Two beers (80/- from Edinburgh's Caledonian brewery recommended).
So what is Cron?
It's a process that's running all the time on the server. What it does is continuously check through each user's list of scheduled jobs, and if one's due to run, it runs it. The lists are called Crontabs, and can contain any Unix command, including running a script. You can edit your Crontab directly, which can be quick and dirty, or you can take the easier method of submitting a text file instead. As I'm all for an easy life, we'll be using the second method.The demo script
OK, here's the Perl script we'll be running. It checks the POP box which bots@easyweb.co.uk throws mail into, gets any mail, and emails the sender back an appropriate message, depending on the incoming email's subject. If you want to use this, then you're welcome, but remember to tell people where you got it ;-)Install this script, as normal, making sure it's executable, and take a note of the full path to it (it might be something like/home/yourlogin/cgi-bin/autoresponder.cgi
).You'll now need to test that the script works. Email the mailbox, then telnet into your server, and (assuming that your script is living in the cgi-bin subdirectory of your space), enter the following commands:cd cgi-bin autoresponder.cgiAll things being equal, you should get an email back, and you can crack open your first beer.
Setting up the Cron job
Now you have a working script, you'll need to kick it off fairly frequently. We'll be keeping a balance between not overloading the server (keeping your sysadmin and users happy), while still having it run often enough, we'll run the job every 5 minutes.Crontab format
The general format for Crontabs is:minute hour day month weekday commandNote that
hour
in 24 hour format- 0 is Sunday, 6 is Saturday for
day
command
can be any system command, or the full path to a user script
10 21 30 9 * /home/web3060/cgi-bin/autoresponder.cgiThe other 2 things you need to know is that an asterisk in any field means all values, so
0 23 * * * /usr/local/bin/disktidy
will run at eleven PM every night, 0 23 1 * * command
will run at eleven PM every 1st of the month, 15 5 * * 1 command
will run at 5:15 am every Monday etc, and that if you want more than one value in a field, separate them by commas (remember not to put a space within a field, or the system will get confused). This means that to fire our script every 5 minutes every hour, every day, every month, we'll need the following:0,5,10,15,20,25,30,35,40,45,50,55 * * * * /full/path/to/autoresponder.cgiCopy the above, crack open that text editor and paste it in - replacing
/full/path/to/autoresponder.cgi
with the equivalent for your system save it as autoresp.ctb and FTP the file into your own space (your home directory for preference).Now telnet into your webspace and enter your Crontab command by entering the following:crontab autoresp.ctbYou can check that this has worked by entering
crontab -l
which should give back a list containing the Crontab entry above (y'know - 0,5,10 etc). One more command which you might like to take a note of now is crontab -r
which removes your Crontab, so you can back out your change if needed.Now all you have to do is run your test. Email the mailbox, and sit back for 5 minutes or so. Your server might not the job exactly at the time specified (and if you've specified an hour of day, beware of timezones too), as it considers background jobs like this as less important than other stuff it's doing, like delivering web pages to users who think every second counts. But after a few minutes you should get an email back. Congratulations! You can crack open that other beer safe in the knowledge that you can now do stuff that 90% of other web designers can't.