[Drupal] Drush Digging Deeper

Any package manager or system tool can be difficult to use. We’ve looked at and used Drush a bit in the past. Here are some more tips to help better use Drush.

drush help, or drush help [command]

The help command in Drush by default gives a good overview of available Drush commands. If you’re clueless how to use Drush, or want to check if a 3rd party module has added commands to Drush. Then this command is your friend.

If you want to check what commands a particular package provides you can provide a –filter argument to Drush:

$ drush help --filter=migrate
All commands in migrate: (migrate)
 migrate-analyze       Analyze the source fields for a migration.               
 migrate-audit (ma)    View information on problems in a migration.             
 migrate-deregister    Remove all tracking of a migration                       
 migrate-fields-desti  List the fields available for mapping in a destination.  
 nation (mfd)                                                                   
 migrate-fields-sourc  List the fields available for mapping from a source.     
 e (mfs)                                                                        
 migrate-import (mi)   Perform one or more migration processes                  
 migrate-mappings      View information on all field mappings in a migration.   
 migrate-messages      View any messages associated with a migration.           
 migrate-reset-status  Reset a active migration's status to idle                
 migrate-rollback      Roll back the destination objects from a given migration 
 migrate-status (ms)   List all migrations with current status.                 
 migrate-stop (mst)    Stop an active migration operation                       
 migrate-wipe (mw)     Delete all nodes from specified content types.

Once you know what commands are available it’s useful to know what Options are available to most commands, drush core-topic, is a command reference that lists a lot of additional examples in using Drush.

$ drush topic
Choose a topic
 [0]    :  Cancel                                                                                                                             
 [1]    :  All global options (core-global-options)                                                                                           
 [2]    :  Bashrc customization examples for Drush. (docs-bashrc)                                                                             
 [3]    :  Bastion server configuration: remotely operate on a Drupal sites behind a firewall. (docs-bastion)                                 
 [4]    :  Bootstrap explanation: how Drush starts up and prepared the Drupal envionment for use with the command. (docs-bootstrap)           
 [5]    :  Configuration overview with examples from example.drushrc.php. (docs-configuration)                                                
 [6]    :  Contexts overview explaining how Drush manages command line options and configuration file settings. (docs-context)                
 [7]    :  Crontab instructions for running your Drupal cron tasks via `drush cron`. (docs-cron)                                              
 [8]    :  Drush API (docs-api)                                                                                                               
 [9]    :  Drush command instructions on creating your own Drush commands. (docs-commands)                                                    
 [10]   :  Drush Make example makefile (docs-make-example)                                                                                    
 [11]   :  Drush Make overview with examples (docs-make)                                                                                      
 [12]   :  Error code list containing all identifiers used with drush_set_error. (docs-errorcodes)                                            
 [13]   :  Example Drush command file. (docs-examplecommand)                                                                                  
 [14]   :  Example Drush commandfile that extends sql-sync to allow transfer of the sql dump file via http rather than ssh and rsync.         
 [15]   :  Example Drush commandfile that extends sql-sync to enable development modules in the post-sync hook. (docs-example-sync-extension) 
 [16]   :  Example Drush script. (docs-examplescript)                                                                                         
 [17]   :  Example policy file. (docs-policy)                                                                                                 
 [18]   :  php.ini or drush.ini configuration to set PHP values for use with Drush. (docs-ini-files)                                          
 [19]   :  README.txt (docs-readme)                                                                                                           
 [20]   :  Shell alias overview on creating your own aliases for commonly used Drush commands. (docs-shell-aliases)                           
 [21]   :  Shell script overview on writing simple sequences of Drush statements. (docs-scripts)                                              
 [22]   :  Site aliases overview on creating your own aliases for commonly used Drupal sites with examples from example.aliases.drushrc.php.  
 [23]   :  Strict option handling, and how commands that use it differ from regular Drush commands. (docs-strict-options)

If you want to check the status of Drush and your website, you can issue drush core-requirements and Drush will output status information of Drupal and its dependencies for both the site, and the configured server.

I often write and test simple PHP scripts using the core Drupal API. You can test such scripts by invoking them from Drush via drush php-script FILENAME.php to debug my own scripts or 3rd party Drush commands it can be helpful to pass a verbose flag to Drush or a debug flag. This is simply drush command -v or drush command –debug.

Many of these settings can be configured globally using a drushrc.php file. This file is a configuration file for Drush and it may be system-wide, user-wide or Drupal site wide. Drush scans various locations for it. In order to make Drush always output commands verbosely you could add the following line to your drushrc file:

// Always supply the following option to all Drush commands.
$options['v'] = TRUE;

Typically the drushrc file is used to store aliases to remote, development and staging sites so that Drush commands that accept sitenames can perform tasks either locally or remotely (or both) using a convenient shorthand notation.

Lastly, you can create your own Drush commands to perform tasks. To make a command you simply create a file named as MYMODULE.drush.inc within a custom module. You then must/should implement the following Drush hooks:

  • COMMANDFILE_drush_help()
    provide help text from the command line about your commands when drush help COMMANDNAME is called.
  • COMMANDFILE_drush_command()
    Provide an $items array keyed on COMMANDNAME that describes the command and valid options to be passed to the command.
    This function you implement actually performs your command business logic. It is called by Drush after the command invocation is validated (see below).
  • drush_COMMANDFILE_COMMANDNAME_validate()
    This function allows you to validate if options passed to commands are valid. This lets you avoid having an erroneous process start.

For more detailed directions on creating a custom Drush Command see the official Drush website documentation.

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *