Cron is a time-based job scheduler in Unix/Linux computer operating systems. If you run a webserver, you can create cronjobs with the crontab(cron table) file, or using the similarly named application within your webserver’s control panel interface. I’m going to outline just how powerful and useful having automated tasks are in the daily maintenance schedule of your website. Its uses are endless, but popular ways to use Cron Jobs are to make daily backups of your databases, automatically update your sitemap.xml document, or to send you an email about the health of your webserver! Today, we’ll look at automatically generating and updating your website’s sitemap.xml document with the help of a cron job, as a real-world example for Cron Job usage.

Automatic XML Sitemap Updates

Ok, admittedly a Cron Job can’t do this – well, not all by itself anyhow. As you may have figured out, it can kick off a script, say twice a day, which will crawl your website, collect all the internal links, format them correctly to a standardized sitemap xml document, and save the file into the root directory, thereby eliminating the need to manually generate and update the sitemap document ever again!

So here’s where you get the Sitemap Generator Script: http://code.google.com/p/perlsitemapgenerator/

This is a perl script that will generate your website’s sitemap document. Then you’ll just need to write a small command, or script, into the crontab for the job to execute.

Here’s how the Cron Job command is formatted:

* * * * * command to be executed
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 7) (0 or 7 is Sun, or use names)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

And here’s an example:
0 12 * * * /home/bko/sitemapgen.pl

Which basically says: Run the sitemap generator script located in /home/bko/ every day, at 12pm (noon).

Pretty simple, right? There are plenty of other ways to utilize Cron Jobs, but the primary way is to create a script which will kick off some action or duty that needs to be performed repetitively.

Share: