Skip to main content

Drush Scripts: Running Drush Commands from Inside a Drush Shell Script

We have a website that updates numerous nodes nightly with an uploaded XML file. This XML file is then processed with Drupal's batch api, and when the XML file is uploaded manually, the script works perfectly. However, in real life, this XML file is uploaded programmatically by a script running on another server. Programmatically, the file gets uploaded and the batch begins properly, but on the successive redirects, the batch fails. This is because, the pseudo browser window from our publishing script closes.

Drush to the rescue

Luckily we have Drush installed on the server. After doing some digging we found that the command drush batch-process {bid} will run the batch of whatever batch id you tell it. Perfect, now we can use a cron job on the server to process our batch after the XML file is published to the server.

Drush as a shell script

Before invoking Drush from cron, we need to created an executable Drush script that we can have cron run. Our script looks like this:

  1. #!/usr/bin/env drush
  2.  
  3. // Find the newest batch job to run.
  4. $x = db_fetch_object(db_query("SELECT bid FROM {batch} ORDER BY bid DESC LIMIT 1"));
  5.  
  6. if ($x->bid) {
  7.   // Some output for us to make sure the script is running.
  8.   drush_print('The batch ID is: ' . $x->bid);
  9.   $i = 1;
  10.  
  11.   /*
  12.     Why a do while loop? Because we want the script to run continuously being sure to process the entire batch. we added a database query at the end to check for the ID we are processing periodically. We found that calling drush_invoke_process just once resulted in stopping before all results were processed.
  13.   */
  14.   do {
  15.     // Call drush_invoke_process to mimic drush batch-process
  16.     drush_invoke_process("@self", 'batch-process', array($x->bid));
  17.     // Some more output for us humans.
  18.     drush_print('Running process #' . $i);  
  19.  
  20.     // Only query the database every 10 tries.
  21.     if($i % 10 == 0) {
  22.       drush_print('Check the database for the batch ID.');
  23.       // Query the database and see if the batch ID still exists.
  24.       $x = db_fetch_object(db_query("SELECT bid FROM {batch} WHERE bid = %d", $x->bid));
  25.     }
  26.  
  27.     $i++;
  28.   } while ($x->bid);
  29.  
  30. }
  31. else {
  32.   drush_print('No batch ID was found');
  33. }

UPDATE: I had initially set this script to run under a cron job and it was not running correctly. After setting my crontab MAILTO: parameter, I quickly found out why. I was receiving the following error message:

  1. PHP Fatal error:  Call to undefined function db_fetch_object() in /usr/share/drush/commands/core/core.drush.inc(666) : eval()'d code on line 2
  2. Drush command terminated abnormally due to an unrecoverable error.       [error]
  3. Error: Call to undefined function db_fetch_object() in
  4. /usr/share/drush/commands/core/core.drush.inc(666) : eval()'d code,
  5. line 2

As it turns out, my script was trying to run, but it didn't know what site I was referring to. Drush wasn't bootstrapping the site, and I was missing most of Drupal's API functions. The solution: Drush Site Aliases.


Comments