Answers.Body: <p>You should run CV commands from the root of the website (especially on Drupal 8/9 - where support isn't as well tested for "pathing" - i.e finding the correct files to bootstrap CiviCRM).</p>
  
  <p>If your website lives in <code>/var/www/civicrm</code> that's the directory you should be in when running CV commands (ideally!). You may find that being in <code>/var/www/civicrm/web</code> or <code>/var/www/civicrm/web/modules</code> works but chances are it'll confuse the pathfinding logic!</p>
  
  <p>To run commands as a specific user you should append <code>--user=USERNAME</code> to the end of the command string.</p>
  
  <p>To run commands via cron you should use the absolute path to CV, your site and PHP. </p>
  
  <p><strong>For example:</strong></p>
  
  <p><code>/usr/bin/php /usr/local/bin/cv api job.execute --cwd=/var/www/civicrm --user=admin</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-01T15:07:07.240Z
Answers.Text: Thank you for the --user and --cwd switches.  Had missed those.  
  Now using this invocation:  
  
  `/usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php /opt/local/my_client/drupal/vendor/civicrm/cv/bin/cv api job.execute --cwd=/opt/local/my_client/drupal --user=mailprocess` 
  
  and getting this error message:  
  
  `PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106`.
Answers.UserDisplayName: Hugh Esco
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-01T15:47:19.943Z
Answers.Text: I have also tested, with no joy, but the exact same error message, several variations on this:  `-d includepath=/opt/local/my_client/drupal/web/core/lib`
Answers.UserDisplayName: Hugh Esco
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-01T15:52:48.780Z
Answers.Text: May have just discovered the secret sauce!  Further testing required, but I got not error messages and `echo $?` returns 0.  I used this on the cli:  `-d require ./vendor/autoload.php`.
Answers.UserDisplayName: Hugh Esco
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-01T08:24:48.253Z
Answers.LastActivityDate: 2020-06-01T08:24:48.253Z
Answers.UserDisplayName: HomoTechsual
Body: <p>My next issue is this:  </p>
  
  <p>attempts to push out a civilmailing result, as reported by /civicrm/admin/joblog, with this error: 
  'API permission check failed for Group/get call', . . . 'insufficient permission: require access CiviCRM'</p>
  
  <p>From my $drush_context path, I had run 
  sudo -u www-data ../../../vendor/civicrm/cv/bin/cv api Job.execute
  Same result without the sudo as www-data.  </p>
  
  <p>Full log entry:</p>
  
  <pre><code>Details
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  
  &gt; Finished execution of Mailings scheduler with result: Failure, Error
  &gt; message:  API permission check failed for Group/get call; insufficient
  &gt; permission: require access CiviCRM
  
  </code></pre>
  
  <p>My build put the following in place:</p>
  
  <pre><code># grep -v ^# /var/spool/cron/crontabs/www-data 
  */5 * * * * /home/drupal/lib/sh/civimail_cron_job.sh process_mailing
  */15 * * * * /home/drupal/lib/sh/civimail_cron_job.sh fetch_bounces
  </code></pre>
  
  <p>Running that manually results in:</p>
  
  <pre><code># sudo -u www-data /bin/bash /home/drupal/lib/sh/civimail_cron_job.sh process_mailing 
  PHP Warning:  require_once(civicrm.config.php): 
  failed to open stream: No such file or directory 
  in /opt/local/my_client/drupal/vendor/civicrm/civicrm-core/bin/cli.class.php on line 245
  
  PHP Fatal error:  require_once(): 
  Failed opening required 'civicrm.config.php' (include_path='.:/usr/share/php') 
  in /opt/local/my_client/drupal/vendor/civicrm/civicrm-core/bin/cli.class.php on line 245
  
  </code></pre>
  
  <p>closest I come to finding that file on this container is:</p>
  
  <pre><code># locate civicrm.config.php 
  /opt/local/my_client/drupal/vendor/civicrm/civicrm-drupal-8/civicrm.config.php.drupal
  /opt/local/my_client/drupal/web/modules/contrib/civicrm/civicrm.config.php.drupal
  </code></pre>
  
  <pre><code># drush user:information mailprocess 
   User ID       :  23                                       
   User name     :  mailprocess                              
   User mail     :  mailprocess@uat.db.georgiagreenparty.net 
   User roles    :  authenticated                            
                    civimail_role                            
   User status   :  1                                        
  </code></pre>
  
  <pre><code># drush role:list civimail_role
   Permission        
   access CiviCRM    
   access CiviMail   
   view all contacts
  </code></pre>
  
  <p><strong>UPDATING this script's listing</strong></p>
  
  <pre><code># cat /home/drupal/lib/sh/civimail_cron_job.sh 
  
  #!/bin/bash
  
  CMD=$1
  CWD=/opt/local/my_client/drupal/vendor/civicrm/civicrm-core
  requirements='-d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
  PHP="/usr/bin/nice -n19 /usr/bin/php ${requirements}"
  PARAMS='-j -s default -u mailprocess -p asecretpassword -e Job -a '
  
  cd $CWD; $PHP bin/cli.php $PARAMS $CMD
  
  exit 0
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>l# CIVICRM_SETTINGS=`pwd`/web/sites/default/civicrm.settings.php /bin/bash ./civimail_cron_job.sh 
   + CMD=
   + CWD=/opt/local/my_client/drupal
   + requirements='-d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
   + PHP='/usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
   + PARAMS='-j -s default -u mailprocess -p verysecret -e Job -a '
   + cd /opt/local/my_client/drupal
   + /usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc  -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php vendor/civicrm/civicrm-core/bin/cli.php -j -s default -u mailprocess -p verysecret -e Job -a
  PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106
  Stack trace:
  #0 {main}
    thrown in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc on line 106
   + exit 0
  </code></pre>
  
  <p>or to highlight the issue, here:<br>
  <strong>Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found</strong></p>
  
  <p>line 106 of bootstrap.inc, reads:</p>
  
  <pre><code>const DRUPAL_ANONYMOUS_RID = AccountInterface::ANONYMOUS_ROLE;
  </code></pre>
  
  <p>2,738 mentions of this function in the code base, 
  not sure what more I must do to get my code to find it.</p>
  
  <pre><code>/opt/local/my_client/drupal# grep -R 'AccountInterface' . | wc -l
  2738
  
  /opt/local/my_client/drupal# grep -R 'AccountInterface' web/ | wc -l
  1365
  </code></pre>
  
  <p><strong>Further UPDATE</strong></p>
  
  <p>As for testing the use of cv here, running this:</p>
  
  <pre><code># vendor/civicrm/cv/bin/cv api job.execute 
  {
      "is_error": 0,
      "version": 3,
      "count": 1,
      "values": 1
  }
  </code></pre>
  
  <p>is yielding the following in the logs at /civicrm/admin/joblog:</p>
  
  <pre><code>Date    Job Name    Command/Job Status/Additional Information
  2020-06-01 01:32:26     
  Summary
  Finishing scheduled jobs execution.
  2020-06-01 01:32:26 Mailings scheduler  
  Entity: job Action: process_mailing
  Summary
  Finished execution of Mailings scheduler with result: Failure, Error message: API permission check failed for Group/get call; insufficient permission: require access CiviCRM
  Details
  
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  Finished execution of Mailings scheduler with result: Failure, Error message: API permission check failed for Group/get call; insufficient permission: require access CiviCRM
  
  2020-06-01 01:32:26 Mailings scheduler  
  Entity: job Action: process_mailing
  Summary
  Starting execution of Mailings scheduler
  Details
  
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  Starting execution of Mailings scheduler
  
  2020-06-01 01:32:26     
  Summary
  Starting scheduled jobs execution
  </code></pre>
  
  <p><em>leading me to wonder how it is I get cv to run with my mailprocess user, which has these missing permissions?</em></p>
  
  <p><strong>UPDATE #3</strong></p>
  
  <p>Thank you, HomoTechsual, for the --user and --cwd switches.  <br>
  Had missed those.  <br>
  Now using this invocation:  </p>
  
  <pre><code>/usr/bin/nice -n19 /usr/bin/php \
      -d require ./web/core/includes/bootstrap.inc \
      -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php \
      /opt/local/my_client/drupal/vendor/civicrm/cv/bin/cv api job.execute \
      --cwd=/opt/local/my_client/drupal \
      --user=mailprocess
  </code></pre>
  
  <p>and getting this error message:  </p>
  
  <pre><code>PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106
  </code></pre>
  
  <p><strong>UPDATE #4</strong></p>
  
  <p>This invocation now returns a result code of 0.<br>
  And still I see no evidence that it ran my civimail,<br>
  in the browser UI, at /civicrm/mailing/browse/unscheduled,<br>
  nor in the logs at /civicrm/admin/joblog. </p>
  
  <pre><code>/usr/bin/nice -n19 /usr/bin/php \
      -d require /opt/local/my_client/drupal/vendor/autoload.php \
      /opt/local/ggp_crm/drupal/vendor/civicrm/cv/bin/cv api job.execute \
      --cwd=/opt/local/ggp_crm/drupal --user=mailprocess
  
  </code></pre>
  
  <p>My script now reads:</p>
  
  <pre><code>#!/bin/bash
  set -x
  
  CMD=$1
  CWD='/opt/local/my_client/drupal'
  CV="${CWD}/vendor/civicrm/cv/bin/cv"
  CIVIMAIL_USER='mailprocess'
  requirements="-d require ${CWD}/vendor/autoload.php"
  PHP="/usr/bin/nice -n19 /usr/bin/php"
  ARGS="--cwd=${CWD} --user=${CIVIMAIL_USER}"
  
  cd ${CWD}; ${PHP} ${requirements} ${CV} api job.execute ${ARGS}
  exit "$?"
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-30T21:19:49.807Z
Comments.Text: Can you try having that script run from `/opt/local/my_client/drupal/` as the CWD rather than `/opt/local/my_client/drupal/vendor/civicrm/civicrm-core`.
  
  Also. for what it's worth - you'll find [CV](https://github.com/civicrm/cv) much more useful for cron-jobs on Drupal 8/9 than cli.php which hasn't really been updated to work with D8 or D9 which are not yet officially supported.
Comments.UserDisplayName: HomoTechsual
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-01T08:19:03.030Z
Comments.Text: To get CV to run as a specific user add `--user=USERNAME` to the end (as covered in the docs).
Comments.UserDisplayName: HomoTechsual
Tags: <civimail><cron>
Title: CiviMail cron job unable to find: civicrm.config.php, built with: roundearth/drupal-civicrm-project
ThreadText: # CiviMail cron job unable to find: civicrm.config.php, built with: roundearth/drupal-civicrm-project
  
  (Asked by: username_0 on 2020-05-30)
  
  My next issue is this:  
  attempts to push out a civilmailing result, as reported by /civicrm/admin/joblog, with this error: 
  'API permission check failed for Group/get call', . . . 'insufficient permission: require access CiviCRM'
  From my $drush_context path, I had run 
  sudo -u www-data ../../../vendor/civicrm/cv/bin/cv api Job.execute
  Same result without the sudo as www-data.  
  Full log entry:
  Details
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  
  > Finished execution of Mailings scheduler with result: Failure, Error
  > message:  API permission check failed for Group/get call; insufficient
  > permission: require access CiviCRM
  
  
  My build put the following in place:
  # grep -v ^# /var/spool/cron/crontabs/www-data 
  */5 * * * * /home/drupal/lib/sh/civimail_cron_job.sh process_mailing
  */15 * * * * /home/drupal/lib/sh/civimail_cron_job.sh fetch_bounces
  
  Running that manually results in:
  # sudo -u www-data /bin/bash /home/drupal/lib/sh/civimail_cron_job.sh process_mailing 
  PHP Warning:  require_once(civicrm.config.php): 
  failed to open stream: No such file or directory 
  in /opt/local/my_client/drupal/vendor/civicrm/civicrm-core/bin/cli.class.php on line 245
  
  PHP Fatal error:  require_once(): 
  Failed opening required 'civicrm.config.php' (include_path='.:/usr/share/php') 
  in /opt/local/my_client/drupal/vendor/civicrm/civicrm-core/bin/cli.class.php on line 245
  
  
  closest I come to finding that file on this container is:
  # locate civicrm.config.php 
  /opt/local/my_client/drupal/vendor/civicrm/civicrm-drupal-8/civicrm.config.php.drupal
  /opt/local/my_client/drupal/web/modules/contrib/civicrm/civicrm.config.php.drupal
  
  # drush user:information mailprocess 
   User ID       :  23                                       
   User name     :  mailprocess                              
   User mail     :  mailprocess@uat.db.georgiagreenparty.net 
   User roles    :  authenticated                            
                    civimail_role                            
   User status   :  1                                        
  
  # drush role:list civimail_role
   Permission        
   access CiviCRM    
   access CiviMail   
   view all contacts
  
  UPDATING this script's listing
  # cat /home/drupal/lib/sh/civimail_cron_job.sh 
  
  #!/bin/bash
  
  CMD=$1
  CWD=/opt/local/my_client/drupal/vendor/civicrm/civicrm-core
  requirements='-d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
  PHP="/usr/bin/nice -n19 /usr/bin/php ${requirements}"
  PARAMS='-j -s default -u mailprocess -p asecretpassword -e Job -a '
  
  cd $CWD; $PHP bin/cli.php $PARAMS $CMD
  
  exit 0
  
  UPDATE
  l# CIVICRM_SETTINGS=`pwd`/web/sites/default/civicrm.settings.php /bin/bash ./civimail_cron_job.sh 
   + CMD=
   + CWD=/opt/local/my_client/drupal
   + requirements='-d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
   + PHP='/usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php'
   + PARAMS='-j -s default -u mailprocess -p verysecret -e Job -a '
   + cd /opt/local/my_client/drupal
   + /usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc  -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php vendor/civicrm/civicrm-core/bin/cli.php -j -s default -u mailprocess -p verysecret -e Job -a
  PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106
  Stack trace:
  #0 {main}
    thrown in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc on line 106
   + exit 0
  
  or to highlight the issue, here:
  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found
  line 106 of bootstrap.inc, reads:
  const DRUPAL_ANONYMOUS_RID = AccountInterface::ANONYMOUS_ROLE;
  
  2,738 mentions of this function in the code base, 
  not sure what more I must do to get my code to find it.
  /opt/local/my_client/drupal# grep -R 'AccountInterface' . | wc -l
  2738
  
  /opt/local/my_client/drupal# grep -R 'AccountInterface' web/ | wc -l
  1365
  
  Further UPDATE
  As for testing the use of cv here, running this:
  # vendor/civicrm/cv/bin/cv api job.execute 
  {
      "is_error": 0,
      "version": 3,
      "count": 1,
      "values": 1
  }
  
  is yielding the following in the logs at /civicrm/admin/joblog:
  Date    Job Name    Command/Job Status/Additional Information
  2020-06-01 01:32:26     
  Summary
  Finishing scheduled jobs execution.
  2020-06-01 01:32:26 Mailings scheduler  
  Entity: job Action: process_mailing
  Summary
  Finished execution of Mailings scheduler with result: Failure, Error message: API permission check failed for Group/get call; insufficient permission: require access CiviCRM
  Details
  
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  Finished execution of Mailings scheduler with result: Failure, Error message: API permission check failed for Group/get call; insufficient permission: require access CiviCRM
  
  2020-06-01 01:32:26 Mailings scheduler  
  Entity: job Action: process_mailing
  Summary
  Starting execution of Mailings scheduler
  Details
  
  
  Parameters parsed (and passed to API method): 
  a:1:{s:7:"version";i:3;}
  
  Full message: 
  Starting execution of Mailings scheduler
  
  2020-06-01 01:32:26     
  Summary
  Starting scheduled jobs execution
  
  leading me to wonder how it is I get cv to run with my mailprocess user, which has these missing permissions?
  UPDATE #3
  Thank you, username_1, for the --user and --cwd switches.  
  Had missed those.  
  Now using this invocation:  
  /usr/bin/nice -n19 /usr/bin/php \
      -d require ./web/core/includes/bootstrap.inc \
      -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php \
      /opt/local/my_client/drupal/vendor/civicrm/cv/bin/cv api job.execute \
      --cwd=/opt/local/my_client/drupal \
      --user=mailprocess
  
  and getting this error message:  
  PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106
  
  UPDATE #4
  This invocation now returns a result code of 0.
  And still I see no evidence that it ran my civimail,
  in the browser UI, at /civicrm/mailing/browse/unscheduled,
  nor in the logs at /civicrm/admin/joblog. 
  /usr/bin/nice -n19 /usr/bin/php \
      -d require /opt/local/my_client/drupal/vendor/autoload.php \
      /opt/local/ggp_crm/drupal/vendor/civicrm/cv/bin/cv api job.execute \
      --cwd=/opt/local/ggp_crm/drupal --user=mailprocess
  
  
  My script now reads:
  #!/bin/bash
  set -x
  
  CMD=$1
  CWD='/opt/local/my_client/drupal'
  CV="${CWD}/vendor/civicrm/cv/bin/cv"
  CIVIMAIL_USER='mailprocess'
  requirements="-d require ${CWD}/vendor/autoload.php"
  PHP="/usr/bin/nice -n19 /usr/bin/php"
  ARGS="--cwd=${CWD} --user=${CIVIMAIL_USER}"
  
  cd ${CWD}; ${PHP} ${requirements} ${CV} api job.execute ${ARGS}
  exit "$?"
  
  **Tags:** civimail, cron
  
  > **Comments:**
  > **username_1**: Can you try having that script run from `/opt/local/my_client/drupal/` as the CWD rather than `/opt/local/my_client/drupal/vendor/civicrm/civicrm-core`.  Also. for what it's worth - you'll find [CV](https://github.com/civicrm/cv) much more useful for cron-jobs on Drupal 8/9 than cli.php which hasn't really been updated to work with D8 or D9 which are not yet officially supported.  
  > **username_1**: To get CV to run as a specific user add `--user=USERNAME` to the end (as covered in the docs).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-06-01. Score: 3
  
  You should run CV commands from the root of the website (especially on Drupal 8/9 - where support isn't as well tested for "pathing" - i.e finding the correct files to bootstrap CiviCRM).
  
  If your website lives in `/var/www/civicrm` that's the directory you should be in when running CV commands (ideally!). You may find that being in `/var/www/civicrm/web` or `/var/www/civicrm/web/modules` works but chances are it'll confuse the pathfinding logic!
  
  To run commands as a specific user you should append `--user=USERNAME` to the end of the command string.
  
  To run commands via cron you should use the absolute path to CV, your site and PHP.
  
  **For example:**
  
  `/usr/bin/php /usr/local/bin/cv api job.execute --cwd=/var/www/civicrm --user=admin`
  
  > **Comments:**
  > **username_0**: Thank you for the --user and --cwd switches.  Had missed those.   Now using this invocation:    `/usr/bin/nice -n19 /usr/bin/php -d require ./web/core/includes/bootstrap.inc -d require ./web/core/lib/Drupal/Core/Session/AccountInterface.php /opt/local/my_client/drupal/vendor/civicrm/cv/bin/cv api job.execute --cwd=/opt/local/my_client/drupal --user=mailprocess`   and getting this error message:    `PHP Fatal error:  Uncaught Error: Class 'Drupal\Core\Session\AccountInterface' not found in /opt/local/my_client/drupal/web/core/includes/bootstrap.inc:106`.  
  > **username_0**: I have also tested, with no joy, but the exact same error message, several variations on this:  `-d includepath=/opt/local/my_client/drupal/web/core/lib`  
  > **username_0**: May have just discovered the secret sauce!  Further testing required, but I got not error messages and `echo $?` returns 0.  I used this on the cli:  `-d require ./vendor/autoload.php`.  
  
  ---
  
  