How do I get a custom script to run when using hook_cron in my custom module? I am brand new to module development, fresh off the D7 tutorial boat. What I want to do cannot be that hard.

I have created my first module called get_weather, with get_weather.info and get_weather.module properly installed and drupal recognized.

I created another simple script that has one function, which I want to run when cron runs. 'update_weather' the ONLY thing this does is return the temperature for that time and store it in the database.

There is nothing to display, there are no blocks to render. However, this does not work:

function get_weather_cron() { update_weather(); }


What's going on here? I get no errors, but nothing happens at all? How do I get my module to run my custom function when cron runs?

Note that my custom function works fine if I run it from a script outside of a Drupal module environment.

Here is code based on comments feedback:

include('weather.php');
function get_weather_cron(){
    watchdog('get_weather_debug', 'Cron function triggered');
    update_weather();
}


Also here is update_weather, which is included in the weather.php include file:

function update_weather() {
    $page['weather'] = scrape_weather();
    //insert into db code here;
}


Again, when I simply run this php normally outside of the drupal enviroment, everything works fine. All I want to do is run this same script when cron runs so this is done automatically. No function in my entire library for this functionality has anything to do with Drupal. I am simply trying to activate it by using cron.
Welcome to drupal! One of the first things that will learn is that Drupal keeps an internal cache of all hooks implemented on the site. Just because you implement hook_cron() in your custom module, and then run cron, does not mean it will work.

Steps I would suggest to help fix


Install drush (this is Drupal's Swiss Army knife)
run drush cc all
run drush cron


Hopefully it will all be resolved

To check if your module is installed and enabled correctly you can


drush pml | grep get_weather (or alternatively look at the modules list in the UI)


Or to check if there is something wrong with your function update_weather();, you can run it in the context of drupal (i.e. alter a full drupal bootstrap with all modules loaded)


drush php-eval "print update_weather();" - you can also place additional logging or printing in your function to see what is going on.


Of course, don't forget to define all you functions that you want to execute in the .module file, as every enabled module, has it's .module file included on every request. On a side note, why don't you do away with calling another function from hook cron?

function get_weather_cron() { update_weather(); }


Simply use something similar to the following one.

function get_weather_cron() {
  // more actual weather getting code here
  variable_set('get_weather_temp', 23);
}


It should be easier to read for the next person too.