Run a drush script from php system()

I have a Drupal module with a function that runs a system(“myscript.drush”);

function _deploy_monitor_stream($submission_id) {   header("Content-type: text/plain");   disable_ob(); // Outputs to browser    $command = 'whoami'; // Debug : Returns my local user since i'm using MAMP    system($command, $return);    $command = 'pwd'; // Debug : Returns my drupal root so it's fine   system($command, $return);    $PATH = getenv('PATH'); // Debug : I tried to force my path to have all bins i need   putenv('PATH=' . $PATH . ':[FULL_PATH_TO/drush:/Applications/MAMP/Library/bin/mysql');    $command = 'drush myscript.drush --option1 --option 2 | tee mytest.log;   system($command); } 

myscript.drush runs different drush_shell_exec() including a site-install function.

$si = DRUSH_PATH . " -y si my_profile --db-url=mysql://root:root@localhost/$DATABASE_NAME "; $si .= "-l $SITE_URL --sites-subdir=$SITE_URL --account-name=$DRUPAL_ADMIN_NAME --account-pass=$DRUPAL_ADMIN_PWD "; $si .= "--site-mail=$SITE_CONTACT_MAIL"; drush_print("Installing site ... " . $site_code); // ---- INSTALL INSTALLATION PROFILE if (drush_shell_exec($si)) {   drush_log("Site succesfully installed", 'ok'); } else {   //If install failed, we abort   foreach (drush_shell_exec_output() as $m) {     drush_print($m);   }   drush_die("Command // $si //  failed"); }     

While it works fine if I run it from the command line, it doesn’t work when my PHP calls the script.

Here is the script output :

Installing site ... /Users/me/.composer/vendor/drush/drush/drush --root=/Users/me/Documents/workspace/proto -y si my_profile --db-url=mysql://root:root@localhost/test_site.test.com -l test_site.test.com --sites-subdir=test_site.test.com --account-name=admin --account-pass=admin --site-mail=system_contact@example.com Directory /Users/me/.drush/cache/default exists, but is not       [error] writable. Please check directory permissions. You are about to CREATE  the 'test_site.test.com' database. Do you want to continue? (y/n): y sh: mysql: command not found Starting Drupal installation. This takes a few seconds ...                  [ok] WD php: Exception: To start over, you must empty your existing           [error] database.To install to a different database, edit the appropriate settings.php file in the sites folder.To upgrade an existing installation, proceed to the update script.View your existing site. in install_begin_request() (line 301 of /Users/me/Documents/workspace/proto/includes/install.core.inc). WD php: Warning: Cannot modify header information - headers already    [warning] sent by (output started at /Users/me/.composer/vendor/drush/drush/includes/output.inc:38) in drupal_send_headers() (line 1233 of /Users/me/Documents/workspace/proto/includes/bootstrap.inc). Exception: <ul><li>To start over, you must empty your existing database.</li><li>To install to a different database, edit the appropriate <em>settings.php</em> file in the <em>sites</em> folder.</li><li>To upgrade an existing installation, proceed to the <a href="http://test_site.test.com/update.php">update script</a>.</li><li>View your <a href="http://test_site.test.com">existing site</a>.</li></ul> in install_begin_request() (line 301 of /Users/me/Documents/workspace/proto/includes/install.core.inc). Drush command terminated abnormally due to an unrecoverable error.       [error] drush: Command // /Users/me/.composer/vendor/drush/drush/drush --root=/Users/me/Documents/workspace/proto -y si my_profile --db-url=mysql://root:root@localhost/test_site.test.com -l test_site.test.com --sites-subdir=test_site.test.com --account-name=admin --account-pass=admin --site-mail=system_contact@example.com //  failed 

As you can see, it cannot find the mysql command sh: mysql: command not found I tried to add the path to mysql to all and even force it to the PATH var before running the script in case it would only live for the current run – but it doesn’t solve the problem.

If i do an

drush_shell_exec('echo $PATH'); foreach (drush_shell_exec_output() as $m) {     drush_print($m); } 

At the beginning of my drush script, it confirms mysql is in the PATH var. So Drush should be able to find it as well !

Anybody can help ?

Thanks !

UPDATE 1

Here is a log with a si --debug option. It doesn’t give more information – We still see that the “script” failes to call mysql Installing site ... /Users/me/.composer/vendor/drush/drush/drush --root=/Users/me/Documents/workspace/proto -y si --debug my_profile --db-url=mysql://root:root@localhost/test_site.test.com -l test_site.test.com --sites-subdir=test_site.test.com --account-name=admin --account-pass=admin --site-mail=system_contact@example.fr Bootstrap to phase 0. [0 sec, 1.88 MB] [bootstrap] Drush bootstrap phase : _drush_bootstrap_drush() [0.01 sec, 2 MB] [bootstrap] Directory /Users/me/.drush/cache/default exists, but is not [error] writable. Please check directory permissions. [0.01 sec, 2.01 MB] Cache MISS cid: 6.5.0-alias-path--e51a01eaadbedd8992f93f4b1e37a340 [debug] [0.01 sec, 2.01 MB] Cache MISS cid: 6.5.0-commandfiles-0-9f983fb1e5cd5a06f3cad18503b4c204 [debug] [0.01 sec, 2.02 MB] Cache SET cid: 6.5.0-commandfiles-0-9f983fb1e5cd5a06f3cad18503b4c204 [debug] [0.02 sec, 2.03 MB] Bootstrap to phase 0. [0.06 sec, 5.59 MB] [bootstrap] Bootstrap to phase 1. [0.08 sec, 5.6 MB] [bootstrap] Drush bootstrap phase : _drush_bootstrap_drupal_root() [0.09 sec, [bootstrap] 6.49 MB] Initialized Drupal 7.35 root directory at [notice] /Users/me/Documents/workspace/proto [0.09 sec, 6.49 MB] Found command: site-install (commandfile=core) [0.09 sec, 6.49 MB] [bootstrap] Calling hook drush_core_site_install_validate [0.15 sec, 6.52 MB] [debug] Returned from hook drush_core_site_install_validate [0.15 sec, 6.52 [debug] MB] Calling hook drush_core_pre_site_install [0.15 sec, 6.53 MB] [debug] Executing: mysql --database=test_site.test.com --host=localhost --user=root --password=root -e "SELECT 1;" 2> /dev/null > /dev/null You are about to CREATE the 'test_site.test.com' database. Do you want to continue? (y/n): y Sites directory sites/test_site.test.com already exists - [notice] proceeding. [0.15 sec, 6.53 MB] Drush bootstrap phase : _drush_bootstrap_drupal_site() [0.15 sec, [bootstrap] 6.54 MB] Initialized Drupal site test_site.test.com at [notice] sites/test_site.test.com [0.15 sec, 6.54 MB] Cache MISS cid: [debug] 6.5.0-install_profile-fbfb0256099ded6086bc55133257a1bc [0.16 sec, 6.55 MB] Cache MISS cid: 6.5.0-commandfiles-2-5ea45eaeffdc711df5631f275d26edd1 [debug] [0.16 sec, 6.55 MB] Cache SET cid: 6.5.0-commandfiles-2-5ea45eaeffdc711df5631f275d26edd1 [debug] [0.23 sec, 6.57 MB] Executing: mysql --database=test_site.test.com --host=localhost --user=root --password=root -e "SELECT 1;" 2> /dev/null > /dev/null Calling system(mysql --database=information_schema --host=localhost --user=root --password=root < /private/tmp/drush_QDZllv); sh: mysql: command not found Returned from hook drush_core_pre_site_install [0.26 sec, 7.74 MB] [debug] Calling hook drush_core_site_install [0.26 sec, 7.75 MB] [debug] Starting Drupal installation. This takes a few seconds ... [0.26 sec, [ok] 8.03 MB] Calling install_drupal(Array) [0.26 sec, 8.03 MB] [debug] WD php: Exception: To start over, you must empty your existing [error] database.To install to a different database, edit the appropriate settings.php file in the sites folder.To upgrade an existing installation, proceed to the update script.View your existing site. in install_begin_request() (line 301 of /Users/me/Documents/workspace/proto/includes/install.core.inc). [0.32 sec, 12.26 MB] WD php: Warning: Cannot modify header information - headers already [warning] sent by (output started at /Users/me/.composer/vendor/drush/drush/includes/output.inc:38) in drupal_send_headers() (line 1233 of /Users/me/Documents/workspace/proto/includes/bootstrap.inc). [0.32 sec, 12.29 MB] Exception: <ul><li>To start over, you must empty your existing database.</li><li>To install to a different database, edit the appropriate <em>settings.php</em> file in the <em>sites</em> folder.</li><li>To upgrade an existing installation, proceed to the <a href="http://test_site.test.com/update.php">update script</a>.</li><li>View your <a href="http://test_site.test.com">existing site</a>.</li></ul> in install_begin_request() (line 301 of /Users/me/Documents/workspace/proto/includes/install.core.inc). Drush command terminated abnormally due to an unrecoverable error. [error] [0.32 sec, 12.27 MB] drush: Command // /Users/me/.composer/vendor/drush/drush/drush --root=/Users/me/Documents/workspace/proto -y si --debug my_profile --db-url=mysql://root:root@localhost/test_site.test.com -l test_site.test.com --sites-subdir=test_site.test.com --account-name=admin --account-pass=admin --site-mail=system_contact@example.fr // failed

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Run a drush script from php system()

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.