What is CRON and What Can it Do? - Cron for website and system maintenance
(Page 2 of 4 )
One of the best and most popular uses of cron is for system maintenance. This is true both for website maintenance and server maintenance in general.
For those of you with websites, there could be a number of system activities you would want to run at night, including:
• Cleaning up your database
• Calculating daily totals
• Sending yourself mail with daily statistics (# of hits, subscriptions, etc)
• Expiring memberships
• Updating calendars
For example:
Let's say you have a discussion forum, and you give out awards based on forum participation. During the month, your users like to see where they stand, since it inspires healthy competition. The calculations for reading through the entire database to figure out rankings are both CPU intensive AND time consuming. In order to keep the load down on your box, and the page load time to a minimum, you design a cron that runs at 1:00 AM every morning, calculates all the data for the previous day, and writes it to a text file. Then when people want to view the stats, you merely load the data from the text file to display it. This way, the intensive work runs only once a day, reducing the overall load on your server. At the same time, the end users get a very fast page load, since it doesn’t have to go through the whole database every time. This is a great example of using cron jobs effectively for your website.
In addition to standard website maintenance, those who have a dedicated server may want to use the cron to schedule system maintenance as well. This is how the good system administrators set up jobs to keep their systems in peak performance. Jobs a system admin may include are:
- Cleaning up disk space
- Automatic optimization of db tables
- Rotating Log Files
- Cleaning up cache
- Backing up databases and/or files
- Checking for invalid links
- Scan for viruses or rootkits
As far as system maintenance is concerned, this is a very small list. Depending on your system, you may have dozens of cron jobs scheduled for times throughout the day.
For example:
When running dynamic websites (websites where the content comes from a database), one of the most important things is the health of your database. So it would make sense to have a cron job that runs around 2-3 AM that checks the integrity of your database, and repairs any issues it finds along the way. I can tell you from experience that this sort of scheduled maintenance script is a true life saver.
Next: Cron for customer contacts >>
More Web Hosting Articles Articles
More By Rich Smith