If you are using a content management system to create a website, it is likely that you will eventually encounter a situation where you need to set up a Cron job. Cron is a program installed on Unix / Linux based servers that allows users to schedule tasks to be run automatically at specific dates or times. The name "Cron" is derived from the Greek word for time - chronos. Since most people use shared hosting, this guide will provide the basics for setting up your own crontab and automating tasks with your web hosting provider on a shared server...
I will be using Drupal as an example for setting up Cron because Cron is an integral part of the Drupal core. If you have downloaded and installed the new Drupal 5 CMS, you will notice that there is an error in the administrative log when you first sign in with your administrative account.
When you look at the status report you can go ahead and click the link "run cron manually" and this will remove the error. However, you will eventually want to set up a Cron job to do this. Having the Cron maintenance run regularly on an automatic schedule is important for keeping your site indexed. If this is not done, new content that is added will not be included in search results. Cron can also perform other tasks in Drupal, such as cleaning up log files. Also, some of the contributed modules require that Cron maintenance is run regularly.
With the Drupal 5 installation package, there is a script included for Cron and it is called cron.php. This file is located in the root directory of your Drupal installation. You can actually run Cron maintenance by entering, for example, the URL "http://www.yourwebsite.com/cron.php" into a web browser. But we want to get this done automatically, so we need to call on the help of the Lynx browser. Lynx is a text browser that is often installed on servers. You will need to contact your hosting provider to make sure that they have Lynx installed (most probably do).
Once you know that Lynx is installed, you will need to find the configuration panel for setting up a Cron job in your hosting account. If your web host uses Cpanel or another control panel, you will be provided with a GUI for entering cron jobs.
The first thing to do is enter the command. The following example is the command I use to make Lynx run cron.php:
lynx -source http://www.yourwebsite.com/cron.php
You may need to adjust this command depending on your hosting provider.
The next step is to decide on a schedule to have this Cron job run. Running Cron does use system resources, so you don't want to have it running every minute if you don't need to. It is up to you to decide on an appropriate schedule for having each Cron job run. Understanding the format for entering a schedule can take a little getting used to. Here is a quick rundown on scheduling options that can be entered:
Minute — any integer from 00 to 59 (specifies the minute the job will be run on)
Hour — any integer from 0 to 23 (specifies the hour the job will be run on)
Day — any integer from 1 to 31 (must be a valid day if a month is specified)
Month — any integer from 1 to 12
Weekday — any integer from 0 to 7, where 0 or 7 represents Sunday
The asterik (*) specifies that the task should be run every time for any sub-constraint. This statement doesn't even make sense to me so let's just have a look at the examples to figure it out. You can also use the command */ to have jobs run every interval. Here the examples to help you get the hang of it.
Run cron.php task every 30 minutes:
*/30 * * * * lynx -source http://www.yourwebsite.com/cron.php
Run cron.php task once a week at 1:00 am every saturday (you can see that saturday is represented by 0):
0 0 1 * * 0 lynx -source http://www.yourwebsite.com/cron.php
Run cron.php task on the first day of every month at 12:00 am (midnight):
0 0 0 01 * * lynx -source http://www.yourwebsite.com/cron.php
The main site for Themebot.com uses the Joomla CMS. I have set up a Cron job to aggregate the newsfeeds thru feedgator every 12 hours. Here is an example of what it looks like (yourusername is the username for your hosting account. You may need to contact your hosting provider for information on the absolute path to your files):
Run task every 12 hours at midnight and at noon:
0 0 1 * * 0 /usr/local/bin/php /home/yourusername/public_html/administrator/components/com_feedgator/cron.feedgator.php >> /dev/null
© 2006-2023 überbytes LLC