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.

irfan's picture

hey

this is great post helpful and informative for cpanel and cron job thanks for saving time.

irfan

sarah's picture

Hi,

This is wonderful place for setting a cron job. But I have one issue with running a cron job. Say in between while running a cron job, due to any reason, it gets stop or say cron job is not finished successfully. Can we roll back the entire process. In a situation where large data deletion or insert is done this may create problem. Or any other way to overcome this problem.

Thanks
sarah

dido's picture

Very good post, thanks a lot.

donn's picture

In my experience Drupal is not exactly the easiest content management system to handle, specially on shared hosting. There are a lot of technical things to know before engaging into it so I think posts like these really come in handy and are a great deal of help for thousands of newbies on web. Thanks for sharing that!

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