User login

Setting up Drupal Cron Task using cPanel in a Shared Web Host

Upon recently installing Drupal 6.13 on my shared web host - Hostgator - I was faced with the challenge of setting up a new Cron Task in the cPanel controls, and immediately I encountered the big question: What am I supposed to type into the bloody command line field?

cPanel | Cron Task | Command Line

While the official Drupal.org resource might be fine for a seasoned server administrator, the information is presented in a way that is too esoteric for most beginners to grasp. Upon searching the Drupal forums I found many different suggestions for strings to put in that command line field, and alternative solutions as well. Most of the suggestions on various command lines were accompanied by little or no explanation as to why you would want to use it or how it works. Furthermore, nearly every suggestion seemed to work for some people and result in errors for others.

So I have outlined the most common solutions below (based on my own experience), along with an example and description of each. While this is far from an exhaustive guide, and may be lacking in some respects, it should at least be enough for you to get Drupal Cron Tasks working successfully on almost any shared web host, and definitely enough to get it working on Hostgator.

GET Command

Examples:

  • GET http://www.example.com/cron.php > /dev/null

For security reasons, most shared hosts disable using the GET command. Most people will get an error if they use this command with a shared host.

WGET Command

Examples:

  • wget -O /dev/null http://www.yoursitename.com/cron.php 2>/dev/null
  • wget http://mydrupalusername:mydrupalpassword@example.com/cron.php

As with GET, most shared hosts disable using the WGET command. Most people will get an error if they use this command with a shared host.

POST Command

Examples:

  • POST http://www.yourdomain.com/cron.php /dev/null

This works for a lot of people on shared hosts so its worth a try. However, several people have also reported problems so it may not work for all shared hosts. If anyone has any more information on this please comment.

PHP Command

Examples:

  • /usr/local/bin/php -q /home/[username]/public_html/cron.php
  • cd /home/[username]/public_html/ ; /usr/local/bin/php -q cron.php

Based on my own experience, using a command which relies on PHP seems to produce the most reliable results across all shared web hosts - IF you are careful to get the paths right! You should note that the pathing in the examples given above are specific to Hostgator and that those paths will vary depending on the setup of your host. I have seen cases where people simply use "php" and I have also seen cases where they fully qualify the php path like "/usr/local/bin/php" as I have done. The path to the cron.php file will also vary depending on your own Drupal setup - whether or not your Drupal installation is in a subdirectory of your webroot and if whether or not you have a multisite setup. (I have heard of people with multisite setups having problems with this method)

You should also note that while the two examples given above both essentially do the same thing, the first example will result in errors on Hostgator while the second example will not. This is because the first example can tend to screw up the relative pathing, giving you an error like this:

Warning: include_once(./includes/bootstrap.inc): failed to open stream: No such file or directory in /home/username/public_html/content/cron.php on line 9
Warning: include_once(): Failed opening './includes/bootstrap.inc' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/username/public_html/content/cron.php on line 9
Fatal error: Call to undefined function drupal_bootstrap() in /home/username/public_html/content/cron.php on line 10

The second example works because it actually makes a switch to the correct directory before it hits cron.php, which resolves the relative pathing issue evident in the error above. And additionally - while this was not necessary for me - in some cases I have also seen the php path qualified like this: ../../../usr/local/bin/php -q cron.php

Once again, while I suspect that this method can be used for most shared hosts, you may need to experiment with the pathing before you have any success.

Alternative Methods

Poor Man's Cron Module:

This is a Drupal module that can be downloaded here. While it seems to be a good fall-back for those who have tried everything else, it is not ideal because it requires more server resources than doing it natively with cPanel. Also due to complexities with upgrading and keeping your Drupal installation secure, you should always try to minimize the number of third-party modules you have installed. Only use this module if you have exhausted all your other options.

Third Party Service:

I have heard mention of different third party services that will set up cron tasks for you. I do not know of any off-hand but if anyone has suggestions please comment.

cron-curl.sh and cron-lynx.sh:

I do not know much about the use of these scripts for Cron, but there is more information about them on the official Drupal.org resource page.

dido's picture

Very good post, thanks a lot.

Garrett's picture

Wonderful post. Your instructions were to the point and easy to follow.

Pedro's picture

Very useful stuff here regarding cron commands and Drupal.

On my experience with different servers and Drupal, I found that the easiest and most reliable command to launch the cron was:

php -f /home/[username]/public_html/cron.php

With the -f I never got any errors with the command.
Hope it helps!

Guest's picture

For me worked PHP Command, second example

cd /home/[username]/public_html/ ; /usr/local/bin/php -q cron.php

thanks a lot!

stunning boy's picture

awesome this is surely a useful and unique feature of drupal i didn't about this, i must say drupal is the best CMS indeed.
web hosting

Mia's picture

It works! I checked the aggregator area and it shows that the news feeds have been refreshed. Thank you once again for the help and support in this wonderful community.
Torrent site

racking's picture

Tong Li Forklift Truck Co,. Ltd mainly engages in the production and sales of forklift

trucks, wheel loaders,engineering machinery,mining machinery, foundry parts,heat treatment manufacture. Metal materials, chemical raw materials (not

including dangerous goods), electronic products, electrical machinery, rubber products sale; machinery technology consultation, information services and

so on business; real estate, equipment property rent.The leading products forklift truck and

various types of warehouse equipment with "TONGLI" brand are widely used in industrial and mining enterprises,ports and harbors, airports, construction

sites, shopping malls, warehouses, and for short distance transportation.

bb's picture

Very good post, thanks.

Raiz's picture

Very well work done ...good post..thanks..

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <actionscript3>, <css>, <html>, <javascript>, <php>, <xml>.

More information about formatting options

Powered by Drupal, an open source content management system