I can't get drush to bootstrap my script. I'm using version 5.9.

Below is my test script (test.drush):

#!/usr/bin/env drush

$site = drush_sitealias_get_record('@site1');


while ($arg = drush_shift()) {

And I have the aliases defined in ~/.drush/aliases.drushrc.php (see end of post).

When I run test.drush @site1 it doesn't bootstrap the script with site1. It has access to the alias because it can output the site record. Also it outputs @site1 as an argument. It should remove @site1 from the argument list and bootstrap it but it doesn't.

Below is the output:

$aliases['site1.local'] = array (
  'root' => '/Users/mark/Documents/drupal6',
  'uri' => 'http://site1.local',
  '#file' => '/Users/mark/.drush/aliases.drushrc.php',

 PHP configuration     :  /private/etc/php.ini                   
 Drush version         :  5.9                                    
 Drush configuration   :                                         
 Drush alias files     :  /Users/mark/.drush/aliases.drushrc.php 


BTW running drush @site1 status on the command line works fine

Also, is there any way to change the current site in a script. When I use:

drush_invoke('use', '@site2')

it doesn't apply to the subsequent drush commands but it does the next time you run the script.


$aliases['site1'] = array(
  'root' => '/Users/mark/Documents/drupal6',
  'uri' => 'http://site1.local',
  'db-url' => 'mysql://user:[email protected]/site1'

  'db-url'=>'mysql://user:[email protected]/site2'

2 Answers 2


Drush shell scripts such as the one you show above do not automatically bootstrap to any argument passed to them. (EDIT: This is due to a bug.) You have a few options.

1: Force the bootstrap: $site = drush_shift(); drush_bootstrap_max_to_sitealias($site); drush_invoke('core-status');

2: Do all of your work in a subshell: $values = drush_invoke_process($site, 'core-status');

Those two should get you started. Note that option number two will re-bootstrap Drupal in an isolated subshell every time you call drush_invoke_process. drush_invoke always works on the currently-bootstrapped site, and you can only bootstrap once. Therefore, that drush_invoke('use', ...) trick won't work. 'use' is only for use in the shell, not in scripts.

There's another error above. This code isn't working as you expect it is:


What's really happening here is that drush_invoke is printing the output of drush status to stdout, and drush_print is printing nothing. You'll get $result['output'] if you use drush_invoke_process.

In my somewhat biased opinion, the Drush chapter of the Definitive Guide To Drupal 7 covers these topics pretty well. It is available for download as a free sample chapter at http://definitivedrupal.org/book/26-drush

  • Thanks for that. I'm using your book, one of the few places with info on Drush. I picked up the presumption of bootstrapping from the Drush scripting docs (drush.ws/docs/shellscripts.html), maybe they're referring to a different version of Drush. Jun 20, 2013 at 8:33
  • drush_bootstrap_max_to_sitealias(drush_sitealias_get_record('@site1')) doesn't seem to bootstrap the site for me even though the site alias record is valid and the function returns TRUE. Jun 20, 2013 at 10:14
  • Could you move this to the issue queue? That looks like a documentation bug -- I think that feature was removed, but I'll need to look into it. No time right now. Jun 20, 2013 at 14:56
  • The bootstrap-to-first-arg feature is still in the code, but not working; it's a bug. Jun 20, 2013 at 17:46

The approach to force the bootstrap sounds great but doesn't work as proposed in the previous answer. But a little tweak makes it working pretty nicely:

$site = drush_sitealias_get_record(drush_shift());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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