Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Setting up a Drupal site, day two - Cron jobs

Aaaaaargh! Cron jobs! They are so basic, and I spent most of a day getting one Cron job to run. What the hell is a cron job, anyway? It's an alarm clock with all the ease of use of an 80's VCR timer. "Cron" as in chronograph (with bad spelling). A Cron job says stuff like "do this every 5 minutes" or "run this program on Saturday afternoon, around about teatime." Basic stuff, right? You might already use Windows Scheduler or similar software to do these things on your own computer.

But for our purposes, we want to do things on our website, and the best way to do that is by running a scheduler on the webserver. That usually means setting up something in cPanel *, which will continue to run on your hosting company's computer after you've switched off and gone to sleep, and will keep running when your house is flattened in a flood, or you take a few week's holiday. Cron jobs, when they work, are the first step towards making your website autonomous. One obvious but boring use is to do daily backups. Another use, which is a little bit more interesting, is running your own News Aggregator service on your webserver. In practice that means that your website can display the latest news headlines from, say, Yahoo Asia, and can check for updates every 30 minutes so it will always have something new to show to visitors. Drupal has a News Aggregator built in

CPanel users set up Cron jobs from the main screen of CPanel, by scrolling down to the icon named, er, Cron jobs. From there you are offered two options, Standard or Advanced. Frankly, this is a bit of a letdown from cPanel, because they are both as clear as mud unless you are already familiar with the original Linux text version. But go to the Standard anyway. Enter an email address - hint, set up a new mail folder and rule in your mail reader to hold these messages, as you tend to get a lot of them while troubleshooting. Now look inside the box "Entry 1". There are multiple selection menus, and a single line "Command to run:". The selection menus are your timers, and they are all running. If you click the top line of each one, you would be saying "I want you to do this thing every minute of every hour, of every day, on all days of the week, in every month."

Normal people would just say "do it once a minute". I suppose programmers don't like ambiguity; there's no chance to say "do it once a minute, for a while".

So the idea is, you choose options from this menu that combined together, say how frequently your task will happen. Remember, this isn't really a video timer, that was just my metaphor; you can't program a task to happen only once. Once a year, minimum. To do something on every Feb 21st, 5:19pm, you set Minutes to "19", Hours to "17 = 5pm", Days to "21", leave Weekdays at "Every day", and set Months to "February".

Just to clarify some of those selections:
- the Minutes menu lets you choose a frequency, but the second part of the list is the actual minute. If you knew that Yahoo Asia updates its own news headlines exactly on the hour (e.g. 9:00pm), you might want to update yours at 3 minutes past the hour (e.g. 9:03pm) to ensure you don't miss them. So you would select the number 3.
- Days is of course the Day of the month, e.g. 1st of every month would be 1.
- Weekdays menu is strangely titled. Apparently Unix programmers don't know what weekends are (that explains a lot). Anyway, yes, you can choose weekend days as well as weekday.
- Weekdays vs Days. Choose the one that you want to define, and leave the other at "every day". Choosing "Sunday" and "10" will mean "do it on all days that are Sunday, as well as all days that are the 10th.
- On all menus, hold down the CTRL key to select multiple choices (e.g. 9:03, 9:17, 9:31 and 9:46).

Now for the difficult part.... what task is it going to do?

Well, first of all, the answer is going in that single line textbox above the menus. So you only have the chance to tell it one thing. The idea is, you enter your line there, set it to run every 5 minutes, click Save, and wait to see if it works. Once it's working, you can go back and change the timing to something sensible. So what line do you enter to make Drupal's News Aggregator do its stuff?

On the Drupal forum, they talk about Linux commands such as Wget and Curl. Now this is fine for people running their own private webserver, but most of us don't, and at this point you'll probably become familiar with an emailed error message telling you that you don't have permission to do that. (You can comfort yourself with the fact that your web host is not completely careless about security.)

Keep trying.

Drupal has grouped all the necessary tasks into a single file, so that if you just look at that file in your browser, that will trigger those things to happen. Clever. However you don't want to look at a blank page every 30 minutes for the rest of your life. Apparently it's sometimes possible to just enter the address of that page, e.g. "mywebsite.com/cron.php". More likely, you will again be told you don't have permission.

In Drupal's "scripts" folder there are two files called cron-lynx.sh and cron-curl.sh. These scripts can be "executed" directly by your webserver. If you look at them in a text editor, they are just one line; but you can't run that line yourself using Cron; you have to run the script! Confusing? Yes. But I gather it's necessary for security reasons.

One of these is the one you want. Try both. Chances are, you'll be told you don't have permission.

Have you put the correct "path" in front of the script name, on that line in Cron? For a typical cPanel Drupal admin, the correct version looks like this:

/home/username/public_html/scripts/cron-curl.sh
Username is your cPanel login. Yes, you don't normally see the first few folders in your browser, but they are there, hidden, on your webserver.

By this stage, I was tearing my hair out. If I still smoked, I would have gone through several packs at this point. For me, the final answer was this:

In your ftp or cPanel File Manager, select cron-lynx.sh and cron-curl.sh, and change their permissions, to 755. Don't know what that means? The short version is, it says who can or can't use them. In some ftp software, you set permissions by right-clicking on the file. In File Manager, you select the file then click Change Permissions on the right.

I've tried to cover all the mistakes I made today before I got Cron working for the first time. In the end there was nothing particularly difficult, except knowing the correct line, and ignoring all the irrelevant advice about Wget, etc. But it really did take me most of a day to it working. If you've had a similar battle with Cron, and got caught out by something else, please drop a note in the Comments so that others have a chance to avoid it.

* The Cron program is for Unix or Linux webservers only. Remember, your PC is probably Windows and your webserver is probably Linux. Sometimes there are (less standard) alternatives for Windows servers, but I can't help you there.


This post first appeared on Learning To Make A Profitable Website, please read the originial post: here

Share the post

Setting up a Drupal site, day two - Cron jobs

×

Subscribe to Learning To Make A Profitable Website

Get updates delivered right to your inbox!

Thank you for your subscription

×