Drupal - How to distinguish between drush and HTTP request

From drush.php:

function_exists('drush_main') may be used by modules to detect whether they are being called from drush. See http://drupal.org/node/1181308 and http://drupal.org/node/827478


In Drupal 7:

if (drupal_is_cli()) {
  // Cli only code here
}

if you want to specifically check for drush use the above along with a check for drush_main.

if (drupal_is_cli() && function_exists('drush_main')) {}

In Drupal 8

if (PHP_SAPI == 'cli') {
  // CLI only code here
}

Changelog for D8: https://www.drupal.org/node/2295037


Drush is PHP executed over the CLI (Command Line Interface), so perhaps this question can help.

Tags:

Drush

7