function _drush_backend_generate_command

8.0.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
6.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
7.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
3.x _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
4.x _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
5.x _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
master _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())

Generate a command to execute.


command: A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.

args: An array of arguments for the command.

data: Optional. An array containing options to pass to the remote script. Array items with a numeric key are treated as optional arguments to the command. This parameter is a reference, as any options that have been represented as either an option, or an argument will be removed. This allows you to pass the left over options as a JSON encoded string, without duplicating data.

method: Optional. Defaults to 'GET'. If this parameter is set to 'POST', the $data array will be passed to the script being called as a JSON encoded string over the STDIN pipe of that process. This is preferable if you have to pass sensitive data such as passwords and the like. For any other value, the $data array will be collapsed down into a set of command line options to the script.

integrate: Optional. Defaults to TRUE. If TRUE, any error statuses or log messages will be integrated into the current process. This might not be what you want, if you are writing a command that operates on multiple sites.

drush_path: Optional. Defaults to the current drush.php file on the local machine, and to simply 'drush' (the drush script in the current PATH) on remote servers. You may also specify a different drush.php script explicitly. You will need to set this when calling drush on a remote server if 'drush' is not in the PATH on that machine.

hostname: Optional. A remote host to execute the drush command on.

username: Optional. Defaults to the current user. If you specify this, you can choose which module to send.

Return value

A text string representing a fully escaped command.

2 calls to _drush_backend_generate_command()
drush_backend_fork in includes/
A small utility function to call a drush command in the background.
drush_backend_invoke_args in includes/


includes/, line 338
Drush backend API


function _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL) {
  if (drush_is_local_host($hostname)) {
    $hostname = null;

  $drush_path = !is_null($drush_path) ? $drush_path : (is_null($hostname) ? DRUSH_COMMAND : 'drush'); // Call own drush.php file on local machines, or 'drush' on remote machines.
  $data['root'] = array_key_exists('root', $data) ? $data['root'] : drush_get_context('DRUSH_DRUPAL_ROOT');
  $data['uri'] = array_key_exists('uri', $data) ? $data['uri'] : drush_get_context('DRUSH_URI');

  $option_str = _drush_backend_argument_string($data, $method);
  foreach ($data as $key => $arg) {
    if (is_numeric($key)) {
      $args[] = $arg;
  foreach ($args as $arg) {
    $command .= ' ' . escapeshellarg($arg);
  // @TODO: Implement proper multi platform / multi server support.
  $cmd = escapeshellcmd($drush_path) . " " . $option_str . " " . $command . " --backend";

  if (!is_null($hostname)) {
    $username = (!is_null($username)) ? $username : get_current_user();
    $ssh_options = (!is_null($ssh_options)) ? $ssh_options : drush_get_option('ssh-options', "-o PasswordAuthentication=no");
    $cmd = "ssh " . $ssh_options . " " . escapeshellarg($username) . "@" . escapeshellarg($hostname) . " " . escapeshellarg($cmd);

  return $cmd;