1

I want to truncate the table 'watchdog' once a day.

So I heave:

  1. to set the maximum rows number from the 'Logging and Alerts' (of Site Configuration) to 1000

  2. create a crontab on my system to run the root/cron.php once a day

But I can't understand how the process goes. The crontab calls the cron.php . What is the process next?

I see the file cron.php includes the bootstrap.inc. Why is that?

Where is the actual code with the mysql queries (truncate) inside?

I need to understand the process to make some changes to a site.

I have googled it enough and have not find any information.

  • There are waaay too many questions in there for a single post :) Please consider splitting up your posts so that you're only asking one question at a time, it makes things easier for people to answer you and makes the question more useful for future visitors. It might also be wise to re-phrase your question to ask how to accomplish what you actually need to, not to ask how a single file in the Drupal install works. That way you might be offered alternative (easier) methods – Clive Oct 29 '12 at 12:32
  • @Clive you are right but i have not a clue of how cron.pnp works and thus i don't form a clear question. To make it simpler what i want is to make sure that when i run cron.php nothing else is done apart from truncating the watchdog table. The sole purpose of cron.php is to clear the Watchdog table? – tioschi Oct 29 '12 at 13:07
  • 1
    No definitely not, the cron run does an awful lot more than just clear up the watchdog table This is a list of all the functions that are called in Drupal core when the cron is invoked, and there may be many more contributed modules that do the same. It sounds like you want something like Elysia cron, it gives you fine grained control over which cron jobs run and when – Clive Oct 29 '12 at 13:14
  • Oh I see, you want to stop everything except the watchdog table clear on cron? Elysia cron will help some way towards that but bear in mind stopping the default cron jobs from running may leave your site in an inconsistent state – Clive Oct 29 '12 at 13:16
  • @Clive you should write that as an answer because it is very close to make things clear for me. – tioschi Oct 29 '12 at 13:51
4

cron.php initiates a call to function drupal_cron_run() (in common.inc) which will finally make all calls to all cron tasks.

/**
 * Executes a cron run when called
 * @return
 * Returns TRUE if ran successfully
 */
function drupal_cron_run() {
.
.
// Iterate through the modules calling their cron handlers (if any):
module_invoke_all('cron'); // This will call all cron functions defined in modules(enabled)
.
.
}

dblog_cron() will get invoked by module_invoke_all('cron') This is responsible for clearing old entries in your watchdog table. If this function does not suit your requirements you can use Ultimate Cron to disable it alone. This is a good admin tool to manage your cron jobs. Then you can write your own cron task in another module and enable it.

function dblog_cron() {
  // Cleanup the watchdog table
  $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
  db_query('DELETE FROM {watchdog} WHERE wid <= %d', $max - variable_get('dblog_row_limit', 1000));
}

Note : This is wrt Drupal 6. in D7 module invoke is slightly different

  • would it be wise to make a cron job on my server where i delete the unwanted rows from the table with my custom code instead of invoking cron.php? I have direct access to the server. – tioschi Oct 29 '12 at 14:03
  • @miako No, you can write your custom code in a new module like module_name_hook_cron(). Other contributed modules will depend on cron.php so it wouldnt be correct to get rid of cron.php altogether. – GoodSp33d Oct 29 '12 at 14:08
  • but if i make my_module_hook then it is the same as having the dblog_cron. When i want the dblog_cron to be executed all the other hooks will have to be executed as well. But if i create a shell script in my server mysql queries and cron it work once a day isn' t that safe? – tioschi Oct 29 '12 at 16:18
  • yes creating my_module_cron() is same as dblog_cron() but Using Ultimate Cron you can choose dblog_cron not to be executed. It will give you control over each cron hook. You can disable/enable it from admin settings page. – GoodSp33d Oct 29 '12 at 17:33
2

crontab calls cron.php. This file makes a complete Drupal bootsrap and then Drupal starts to call all modules that implements the hook_cron().

So, if you want to do something when cron is run, write a module that implements hook_cron().

watchdog table is not truncated, when you set maximum number of entries Drupal deletes old entries.

Anyway, I'm not sure if it's a good practice to truncate watchdog table. You may lose valuable information about errores produced before truncate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.