[Drupal] How to execute the function via Drush

[Drupal] How to execute the function via Drush

Posted byUttam Kotekar,1st May 2016

Drush is a command line shell and Unix scripting interface for Drupal. There are a lot of drush commands that comes handy when we are developing in drupal platform. In this article we will look forward to learn one of the drush commands that developers use commonly. Most oftenly we come across a need where we need to execute the function only once. There are several ways to execute a one time function in drupal sytem like creating a menu and executing, batch process execution or using Drupal queues. But there is yet another cool way to execute the function via command line using Drush command.

  ~$ drush php-eval "_function_name()"


   * One time function to print hello world.
  function _function_print_hello_world() {
    // Set the php execution to zero if this function
    // contains large number of insertion and deletions.
    // Do your stuff here
    print_r('hello world');

Open the terminal. Switch to the project root folder and execute below command. Thats all.

  ~$ drush php-eval "_function_print_hello_world()"