Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Drupal's $base_url to work on a cron job?

Tags:

php

cron

drupal

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

like image 273
Leo Burd Avatar asked Oct 22 '10 04:10

Leo Burd


1 Answers

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).

like image 76
Nick Zalutskiy Avatar answered Sep 21 '22 06:09

Nick Zalutskiy