8

Since Drush 9 the @sites alias to run a command on all available multi-site instances has been removed from Drush core.

$ drush @sites cr

  The alias @sites could not be found.

Is there a way to get the same behaviour that does not require me typing my 300 multi-site instance names in a config file?

0

3 Answers 3

4

Waiting something similar, I wrote this small script I called "drall" you may adapt:

#!/usr/bin/php
<?php

// Path to sites.php file.
include('web/sites/sites.php');

$done = [];
$argv = $_SERVER['argv'];

array_shift($_SERVER['argv']);

$parameters     = implode(' ', $_SERVER['argv']);
$commandPattern = "drush --uri=:key $parameters";

foreach ($sites as $key => $site) {

  if (count($argv) > 1 && !in_array($site, $done)) {

    $command = str_replace(':key', $key, $commandPattern);
    echo "Executing command `$command` on $site\n";
    `$command`;
    $done[] = $site;
  }
}

Use example:

$ ./drall cc render
Executing command `drush --uri=xxxxxxx cc render` on default
 [success] 'render' cache was cleared.

Hope there will be some @sites behaviors later, that was helpfull.

Although from this issue it doesn't look like it:

Drush 9 no longer supports executing a single command on multiple sites with alias lists (@A,@b,@c) or @sites. Neither of those syntaxes are available in any context. You may still use drush sa @group to see all aliases in a group; that is the only context in which Drush recognizes alias lists.

5
  • Thank you, the script works just fine. One thing which doestn work, are the promts. you cannot see them but can interact with them. Any ideas how to enhance your script? Jun 5, 2018 at 20:23
  • tried echo `$command`; but it shows me only the output not the prompts Jun 5, 2018 at 20:33
  • use -y at the end of your command, it will not show the y/n prompt this way. Jun 11, 2018 at 6:28
  • What if I do want to see them? E.g. config-import. Jun 11, 2018 at 6:45
  • maybe run it in a single site before Jun 11, 2018 at 6:53
2

I'd simply place a lightweight loop into your ~/.bashrc or ~/.bash_profile or where ever you prefer. Just update the SITES=( foo bar ) array to your needs.

Source your bash file, cd into your multi-site root and call ddrush cim -y for example and it will iterate through all sites provided in the SITES array.

ddrush () {
  # Provide an array of sites you want to loop.
  SITES=( foo bar )

  # Validate and hint if no argument provided.
  if [ "${#}" -eq 0 ]; then
    echo "- ddrush: missing argument(s)"
    echo "EXAMPLE: ddrush cex -y"
  else
    # Loop:
    for SITE in "${SITES[@]}"; do
      echo "drush -l ${SITE} ${@}"
      drush -l "${SITE}" "${@}"
    done
  fi
}

You can use nearly the same for running a deployment script. Or you could easily provide multiple aliases like that. adrush, bdrush, cdrush – one for every multi-site environment you need to maintain.


I only now read you have 300 instances running. Respect! Well, then it might be easier to only provide an absolute path to the sites folder and have that scanned and then looped through automatically.

xdrush () {
  # Provide the absolute path to the sites directory.
  SITES="/var/www/MYMULTISITE/sites"

  # Validate and hint if no argument provided.
  if [ "${#}" -eq 0 ]; then
    echo "- xdrush: missing argument(s)"
    echo "EXAMPLE: xdrush cex -y"
  else
    PWD=${PWD}
    cd "${SITES}"
    # Loop:
    for SITE in $(ls -d */ | cut -f1 -d'/'); do
      # Skip default site.
      if [ ! "${SITE}" == "default" ]; then
        cd "${PWD}"
        echo "drush -l ${SITE} ${@}"
        drush -l "${SITE}" "${@}"
      fi
    done
  fi
}
2

An out-of-the-box solution is provided by a package named jigarius/drall.

I wrote that package and I've used it on a number of multi site Drupal installations.

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.