Friday, October 5, 2012

What is cron and how to configure it with Drupal (secure) on a shared hosting

I can’t name myself a heavy Linux user developer, I don’t know by heart hundreds of commands, but recently I had to setup a hosting for NetBeans PHP Community Council. You can read my post about it at NetBeans PHP team blog. And I needed to setup cron to make my notifications in Drupal works.

So what is cron? Shortly cron is a time based job scheduler that allows you to run certain bash scripts in a certain frequency. What could we do with it? Well, a lot, think of email sending or verifying a certain flag in a web service, cleansing the database, you name it.

explaining cron configuration: CRONTAB

So cron has a configuration file: crontab, it describes the frequency at which the job runs and also the script that describes what will actually be done.

If you are in command line you can access crontab by typing

crontab -e

this will open the configuration file in your default Unix editor, usually vi.

FREQUENCY DESCRIPTION

Ok so how we describe frequency, there are 5 stars, which by default are placeholders. A star means ‘every’

* * * * *

Ok so what all these start mean

*  - minutes (from 0 to 59)

* hours (from 0 to 23)

* – days of month (from 1 to 31)

* – month (from 1 to 12)

* – day of week (0 to 6)

So the previous example means

* * * * - every minute

* * * * 0 – every Sunday

*/5 * * * * – every 5 minutes

45 * * * * – every hour on 45th minute

15,45 * * * * – every hour on 15th and 45th minute

* * */15 * * – twice a month

I hope you got the idea. Also cron has some predefined values like @yearly, @monthly, @weekly, @daily and others.

CONFIGURE DRUPAL TO WORK WITH CRON

In Drupal we have different moving parts. One of these are notifications, which are very important and even more important in OpenAtrium, Drupal’s flavor. Notifications work with cron. To send all the notifications that are in the queue you can go to your Drupal site’s cron.php script, for example, at http://somedrupal.org/cron.php. At this point you’ll have in your administration panel, Administer –> Reports –> Recent logs, some entries related to cron script.

2012-10-09_2354

Now to make this script run at a certain frequency we’ll need to create a job for it. Drupal’s guide propose to make such an entry in your crontab:

45 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php

When you set it up in your crontab –e everything should work fine.

CONFIGURE SHARED HOSTING TO WORK WITH CRON AnD WGET

On my shared hosting solution I hadn’t rights to execute wget command from /usr/bin/wget and my cron failed, so I made a ticket and the guys from the hosting made me a simple copy of wget binary into my /home with all the rights I needed. After that I modified the script from crontab so it could work with my local copy of wget, and everything cron related was working without a single issue from that day.

SECURE ACCESS TO CRON.PHP WITH .HTACCESS

Now that everything works ok, the question is how to configure your site that your cron.php script couldn’t be accessed from the browser. Otherwise anyone could just refresh /cron.php 5 millions time and put a big load on server.

We can avoid it by limiting access to cron.php file via .htaccess file.

  1. Find out your server’s name: use the command  uname –n.
  2. Put the following in .htaccess file
  3. <Files "cron.php">
      Order deny,allow
      Allow from WEBSERVERNAME
      Allow from 127.0.0.1
      Deny from all
    </Files>

  4. Change the WEBSERVERNAME with your own server’s name.
  5. Test that you can’t access the cron.php from browser
  6. Test that the cron job on linux is running and is executed OK.

 

That’s all I wanted to share with you today about cron, if you have any questions or suggestions please write a comment.