Drupal + Crontab = lots of emails with attached headaches or how to get cron.php to work on site5 hosting

Date January 23, 2008

Posted in: Bash, Programming

Tags: , , , , , , , , , ,

Granted, I’m not trying to sound like a smarty pants here because I really know absolutely nothing about cron and how it works.

So let’s start from the beginning (moves hands to symbolize starting a flash back sequence)

From Wiki

In computing, cron is a time-based scheduling service in Unix-like computer operating systems. The name is derived from Greek chronos (??????), meaning time.

cron has been recreated several times in its history.

Say you want something to automatically run on your server (linux/unix) at certain timed intervals, and example would be, oh say a backup or other routines that need to be run at certain intervals. On a Drupal site I run, they have a built in script to update things using cron. It will update a list of rss feeds and other routine maintenance items. So I first started out with looking at the Drupal site for any documentation (shhhh, it’s not like I asked a guy for directions) on how to set it up. There were plenty of examples shown, but didn’t make a whole of sense to me.

For example:

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

Sure…
So I read through hoping to find something that looked remotely familiar to me. I found one all the way at the bottom.
It looked something like this:

0,20,40 * * * * php /home/USERNAME/public_html/ADD-ON-DOMAIN-DIRECTORY/cron.php

The person wrote that this ran every 20 minutes, but I didn’t need it that often, I figured every hour would be enough (and I’m contemplating changing that.
So I tried a command like this:

0 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * * php /public_html/domain_name/cron.php

That ran every hour, and sent me an email but I would get errors in that email that read;
Status: 404
X-Powered-By: PHP/4.4.6
Content-type: text/html

No input file specified.

But every so often I would check my logs and find that it did update, but it wasn’t very regular. So I thought I better bring in some heavy artillery and ask the resident linux guru for help on it. It turned out I wasn’t too far off the mark, but just needed to tell it where php was, and make sure it knew where the file was.

Knowing where php was, wasn’t out of my knowledge, I just didn’t think about it (I didn’t know) so when he typed it in, it was pretty standard. The second change was bit new too me. It was the use of the tilde - huh what’s that you say?

From Wiki:

In Unix shells, the tilde indicates the current user’s home directory (e.g., /home/username). When prepended to a particular username, it indicates that user’s home directory (e.g., ~janedoe for the home directory of user janedoe, typically /home/janedoe).

You might see where I’m going with this. But let me put it all together and then break it apart so I can explain it.
The new syntax is this:

0 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * * /usr/bin/php ~/public_html/helamans-army/cron.php

So let’s start with the numbers. You have 5 values represented as:
* * * * *
- - - - -
| | | | |
| | | | +—– day of week (0 - 6) (Sunday=0)
| | | +——- month (1 - 12)
| | +——— day of month (1 - 31)
| +———– hour (0 - 23)
+————- min (0 - 59)

so for mine you have 0 min and 0,1,2,3…23 which represents every hour. I think after reading about cron, I might change it to something a little easier to read like 0-23 which would represent the exact same thing with less characters. Then I leave the rest blank, but if I wanted to I could change it to run once a day at 8pm on Mondays.
That would look like this:

0 20 0 0 1

20 being the 20th hour (8pm) and 1 being Monday. Catching on?

So lets move on the command to run portion. After the timing of the event you need to tell it what to run. I need php to execute my php script. But my problem was not telling it where php was and only assuming it was in it’s path. But it isn’t, and I would guess it’s better to be safe then sorry for others trying to emulate it.

The guru had me log in through ssh, site5 provides a simple to use java base ssh window and I logged right in. Then I used this command to find php.

whereis php

Which told me it is in the /usr/bin directory, so I put that in place. Now comes the cool little thing for running my script. Just like how it needs to be told where php is, it also needs to be told where the script is. But since I don’t know anything before public_html I use the ~ to tell it to go to my home directory and start there.

So the entire script looks like this.

0 20 0 0 1 /usr/bin/php ~/public_html/path_to_script/cron.php

Now I just need to write one to run backups for each of my sites that will run once a week and delete the one previous. I’ll let you know how that went when I’m done.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>