I've just created this module: http://drupal.org/project/holding
and I've found that running drush on my site that runs it shows me the holding page...
So I'd like to add an exception to my module.

What's the simplest way to detect whether Drupal is being run by Drush?

Comments

dman’s picture

Among other things, the $_SERVER and $_REQUEST is going to look totally different. Is that a start?
print_r() it and you'll see something you can grab.

moshe weitzman’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

traviscarden’s picture

Component: Interoperability » Miscellaneous

The Drupal 7 solution to this would be drupal_is_cli(), which detects whether the current script is running in a command-line environment. (Nice.) Ex:

if (drupal_is_cli()) {
   return; // Good-bye Drush!
}

drupal_goto('http://www.google.com/');
cameron tod’s picture

To make sure that your code is being run from the cli and drush, you can check for the existence of drush functions.

Drupal 7:

if (drupal_is_cli() && function_exists('drush_main')) {
    // Code is being run from drush.
}

Drupal 6 (and probably 5):

if (PHP_SAPI == 'cli' && function_exists('drush_main')) {
    // Code is being run from drush.
}
traviscarden’s picture

Title: how to detect drush is being run? » How to programmatically detect a CLI (Drush) context
Version: All-Versions-2.0 »
Component: Miscellaneous » Documentation
dpovshed’s picture

Version: » 8.x-6.x-dev
Issue summary: View changes

Here is an info for D8 for completeness: https://www.drupal.org/node/2295037

diamondsea’s picture

D8:

// Just check the PHP_SAPI constant, directly
if (PHP_SAPI === 'cli') {
  // Do stuff
}

(saved you a click)