tags:

views:

43

answers:

2

Hello there,

How to get $base_url to show the correct url for my Drupal site when I'm running a cron job? Do I have to set the global $base_url manually for that to happen? Do I have to run the cron job as a registered user?

When I run mysite.com/cron.php by hand everything seems to work fine: $base_url is set to the correct url. However, when I run a similar command via cron or drush, $base_url is set to a generic "http://default".

The funny thing is that when I run cron manually as a registered user from inside Drupal (using devel, for instance), $base_url aways points to the right url.

Any suggestions?

Thanks in advance,

Leo

A: 

Let's should walk trough several possible causes:

  • wget, curl or lynx don't exist on the server. Try running these commands by hand, your OS will tell you if the programs are not available. Solution: make them available, install them, or ask your sysadmin to make them available or install them.
  • wget, curl and the likes cannot connect to the outside world. Call the entire cron command by hand, but _make sure you leave out the --silent or --quit parameters, you want to get verbose information. Good chance some firewall is blocking your connection from inside to outside. Many well-secured systems do. Soldution: Contact your sysadmin to disable the firewall.
  • No-one can connect or run your cron.php. You already point out, that is not the case, but for future reference: many servers have blocked cron.php to be run by "just anyone". You can find this out by calling cron.php and looking into the watchdog (Drupal » Admin » Logs » Recent Events). A record telling that cron was run should be present there. Solution: Find out how the cron.php is blocked from "just anyone", often this is a record in .htaccess or apache configuration, often it is a firewall. Disable that for your requested IP or client.
berkes
Hello berkes, thanks for your message! Unfortunately, I don't think my question was precise. Cron is running fine. But it's not initializing $base_ulr. Would you have any suggestions?
you are saying that if you fire cron by hand, $base_url is set correct, but if you fire it trough the server (commandline) it is not?
berkes
A: 

Your cron is probably set up wrong.

You can use wget or curl, which is effectively the same as running the cron "by hand". Something like this:

5 * * * * wget http://example.com/cron.php

You are probably using drupal.sh, which claims that you should use "http://default/cron.php as the URI." This will break the $base_url handling. The following might work with drupal.sh.

5 * * * * /path/to/drupal.sh --root /home/site/public_html/ http://example.com/cron.php

When using drush, you might have to supply the --uri argument:

drush --uri=http://example.com cron

You could also just set the $base_url variable in settings.php (which is a perfectly valid way to do it, not a hack).

Nick