Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Execute a drush command programmatically

By editor , 29 September, 2016
Question

How to execute a drush command programmatically ?

Methodes:

drush_invoke (For the current site)
drush_invoke($command, $arguments = array());

drush_invoke_process
This creates a new process in a new Drupal instance. There for you must specify the site alias.
drush_invoke_process($site_alias_record, $command_name, $commandline_args = array(), $commandline_options = array(), $backend_options = TRUE);

Example 1. Enable a Module/Feature and Revert all features.

$site = drush_sitealias_get_record('@mysite');
drush_invoke_process($site, 'pm-enable', array("features_dependencies","-y"));
drush_invoke_process($site, 'features-revert-all', array("-y","--force"));

//OR for the current site:
drush_invoke_process('@self', 'features-revert-all', array("-y","--force"));

 

 

Example 2. Run a drush command at  the installation of a module. 

function mymodule_install() {
    // clears the 'all' cache for current web site
    //Methode 1.
    drush_invoke('cache-clear', 'all');
    
    Or

    //Methode2.
    $site = drush_sitealias_get_record('@mysite');
    drush_invoke_process($site, 'cache-clear', array('all'));
}

Tags

  • Drupal 8
  • Code
  • Drush
  • Drupal 7
  • Add new comment
Useful Drush commands
Create a custom DRUSH command on drupal 8

Comments3

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Insasse (not verified)

8 years 8 months ago

Drush 9

The same for drush 9 please.

  • Reply

Anonymous (not verified)

8 years 8 months ago

In reply to Drush 9 by Insasse (not verified)

You have to us drush_invoke…

You have to us drush_invoke_process() becaus drush_invoke() doesn't exist anymore -> https://github.com/drush-ops/drush/blob/master/includes/command.inc

  • Reply

Rifas (not verified)

8 years 2 months ago

Error

Saying drush_invoke function is not defined

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer