Problem: spawning drush command from a shell script that is running in background mode by form submit custom module's function caused errors described in acquia_drush_error.log
If you try to run drush command from shell script in background mode via exec() from form submit (php-fpm) you'll recieve lot's of include_once warnings and fatal errors for service dependencies.
[warning] include_once(/mnt/www/html/proj/docroot/modules/contrib/acquia_connector/acquia_connector.module)
TypeError: Argument 3 passed to ConfigImportCommands::__construct() must implement interface CacheBackendInterface ...
An example command: exec('/path/to/custom/script.sh > /tmp/process.log 2>&1 &');
An example script (script.sh):
#!/bin/bash
cd /drupal/root
drush st
The problem is when exec
spawns shell to execute there are no environment acquia variables AH_SITE_GROUP, AH_SITE_GROUP that are used to include correctly acquia settings.php file.
And also you need to specify full path to drupal, drush, site root, and HOME env var for drush.
Minimal drupal code and script contents:
<php
$acquiaSiteGroup = getenv('AH_SITE_GROUP');
$acquiaEnv = getenv('AH_SITE_ENVIRONMENT');
if (!empty($acquiaEnv) && !empty($acquiaSiteGroup)) {
$projRoot = "/var/www/html/$acquiaSiteGroup.$acquiaEnv";
$docRoot = $projRoot . '/docroot';
}
else {
$docRoot = \Drupal::root();
$projRoot = trim(shell_exec("cd $root; cd ..; pwd"));
}
$drush = $projRoot . '/vendor/bin/drush';
$cmd = "/path/to/script.sh $projRoot $docRoot $acquiaSiteGroup $acquiaEnv > /tmp/process.log 2>&1 &";
exec($cmd);
script.sh:
#!/bin/bash
proj_root=$1
docroot=$2
ah_site_group=$3
ah_site_env=$4
export HOME=$proj_root
if [[ -z "${AH_SITE_GROUP}" ]]; then
export AH_SITE_GROUP=$ah_site_group
fi
if [[ -z "${AH_SITE_ENVIRONMENT}" ]]; then
export AH_SITE_ENVIRONMENT=$ah_site_env
fi
dr="$proj_root/vendor/bin/drush -vv"
$dr --root=$docroot st
Related links: