简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM