User Tools

Site Tools


adding_custom_php_to_drupal_chron_directory

Adding Custom PHP to Drupal chron directory

From Stackoverflow:
If you need to run code just once, you can do it without installing any module, including the Devel module.

Write a PHP file (e.g. tasks.php), put it where the cron.php file that comes with Drupal is, and add the following code.

  /**
   * Root directory of Drupal installation.
   */
  define('DRUPAL_ROOT', getcwd());
  
  include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  drupal_set_time_limit(240); 
  
  // Your code here.

If you are already using Drush for maintaining/deploying your site, then you could make a Drush script. In this way, it could be executed by CLI, and it could receive any arguments, making it re-usable for many occasions.

  #!/usr/bin/env drush
  
  // Your code here.
  // Access the options with drush_get_option(), or any argument with drush_shift().
  

You could also execute a PHP script with drush php-eval, for example:

drush php-eval 'print time();'

I would create a Drush script, but if you cannot install Drush, or you need to execute PHP code from the browser, the first method works fine.

adding_custom_php_to_drupal_chron_directory.txt · Last modified: 2017/08/18 09:09 by tomgle