Fixing out of memory problem for cron job
Drupal is known to be resource heavy system that heavily depends on database storage for content generation. While minimum recommendation for PHP memory limit is 32MB, it is not uncommon to allow as much as 128MB of RAM to PHP in bigger sites.
With tons of modules installed, Drupal cron job can fail due to lack of memory even with 128MB. Quick solution is to increase PHP memory limit. There are several ways to do it and they are well explained at http://drupal.org/node/207036.
However, it would not be ideal to increase memory for all page loads just because of cron job, when that much memory may not be needed for most of the time. I'd like to share couple of ways to give more memory to cron jobs only and not to other pages.
First method is to include a conditional in your settings.php. Cron job is either invoked by accessing /cron.php (usually by system's cron tab) or by manually visiting the page, admin/reports/status/run-cron. Hence,
<?php
if ($_SERVER['PHP_SELF'] == '/cron.php' or
(isset($_GET['q']) and $_GET['q'] == 'admin/reports/status/run-cron')) {
ini_set('memory_limit', '256M'); // Set higher value if needed
}
?>There are bunch of calls to ini_set() function in settings.php and below them would be a good place to put the code. If you use Poorman's cron module instead of system cron tab to trigger Drupal cron periodically, it directly calls function to run cron hooks, drupal_cron_run(), and the method above will not work. Next suggested method will be useful for this case.
Second method is to write a custom module to implement hook_cron.
Drupal allows modules to assign themselves a weight to insure that their hooks are called before others. hook_install() can take care of assigning the weight. Attached is a little module that does the job. It is bare minimum and you will need to change hard coded value since it lacks UI admin setting to change memory limit.
4 comments
Another option: run cron as script using Drush
There's a better option IMHO, which is to run cron as a script instead of triggering via url. Drush makes this easy: http://drush.ws/docs/cron.html
Benefits of running cron as a script are:
- there is a *separate* php.ini file for tuning script vs apache execution (e.g. /etc/php5/cli/php.ini versus /etc/php5/apache5/php.ini) .. or you can specify on the cron command line ('-d memory_limit 128M')
- memory limits and timeouts can be appropriately configured without affecting the web experience
- ungraceful apache restarts won't interrupt a cron in progress
- Drush is a gateway to more command line goodness :)
Cheers,
Stephen
This looks like a good idea
This looks like a good idea too.
In fact, I also use drush to run cron on other sites that I developed. The initial motivation was to avoid time out for running cron.php because a nightly cron job could run for hours on special occasions.
Approach through settings.php can still be useful to admins who may not want hassle with command line or simply don't have access to it.
Thanks for the tip!
Had the same problem - if you
Had the same problem - if you didn't run cron for say 2-3 months, you have to set the memory_limit to a very high value (856M did it for me) and run it once (drush is the way to go!). After that, you can set it back to a more usual limit to 128M or 256M and it runs smoothly.
Thanks lot for solving this
Thanks lot for solving this problem, First I asked to my hosting provider but they did not give me any more response on the topic but now prob has been solved.
Post new comment