Answers.Body: <p>After much research and getting expert ideas...</p>
  
  <p>(I'm sure this is known to many people here, but I'm posting here to hopefully help others who don't know this.)</p>
  
  <p>TLDR; in Cron jobs, you should fully specify the path to the PHP binary as well as wp-cli.</p>
  
  <p>=======================================</p>
  
  <p>Cron runs in its own minimal environment. It does run as the relevant user, but it does not load most of the user environment info. This post was helpful for me to understand some of this:</p>
  
  <p><a href="https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user">https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user</a></p>
  
  <p>I am running cPanel which does some of its own management of php. The cPanel support people were very helpful in helping me understand more.</p>
  
  <p>What is not obvious is that when you login to a shell, various paths are added, even ones that are NOT in the user's .bash_profile. Cron does not necessarily do the same thing.</p>
  
  <p>One that is added with a shell login is /usr/local/bin/</p>
  
  <p>So typing "which php" in a shell gives /usr/local/bin/php, but in cron it gives /usr/bin/php</p>
  
  <p>In cPanel, or indeed other tools I expect, this contains references to specific php binaries that can change depending on what user you are. </p>
  
  <p>I don't pretend to understand the underlying mechanism.</p>
  
  <p>But the upshot is that the php command points to a different binary in the shell or via cron.</p>
  
  <p>So in the cron job, you ALSO have to point at the specific php binary with a command like:</p>
  
  <pre><code>/usr/local/bin/php /usr/local/bin/wp --info (etc)
  </code></pre>
  
  <p>Or even a FULL path to the desired php binary:</p>
  
  <pre><code>/opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/wp --info (etc)
  </code></pre>
  
  <p>In cPanel there is also a known problem when using PHP-FPM for a site where the automatic PHP detection doesn't work, so it falls back to the cPanel PHP default. They are working on fixing that. If this is a problem, then setting the full path to the desired PHP binary will work.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-08T19:44:56.540Z
Answers.LastActivityDate: 2018-03-08T19:44:56.540Z
Answers.UserDisplayName: Norman Cates
Body: <p>I'm trying to set up the CiviCRM cron job.</p>
  
  <p>I've downloaded and installed wp-cli according to the instructions on</p>
  
  <p><a href="http://wp-cli.org/" rel="nofollow noreferrer">http://wp-cli.org/</a></p>
  
  <p>I have logged into the server as the user running the cron job.</p>
  
  <p>I can successfully run this command in the shell:</p>
  
  <pre><code> /usr/local/bin/wp  --url=https://&lt;URL of Server&gt;/ --path=/home/&lt;userDir&gt;/&lt;hostingDir&gt;/ civicrm api job.execute auth=0 
  </code></pre>
  
  <p>But if I run the same command in cron it returns this error in the Cron email:</p>
  
  <p>===============================</p>
  
  <pre><code>Set-Cookie: PHPSESSID=73sl011599lmjbg5038j5n06q4; path=/
  Expires: Thu, 19 Nov 1981 08:52:00 GMT
  Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  Pragma: no-cache
  Content-type: text/html; charset=UTF-8
  
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  fwrite() expects parameter 1 to be resource, string given in &lt;b&gt;phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php&lt;/b&gt; on line &lt;b&gt;61&lt;/b&gt;&lt;br /&gt;
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  fwrite() expects parameter 1 to be resource, string given in &lt;b&gt;phar:///usr/local/bin/wp/php/utils-wp.php&lt;/b&gt; on line &lt;b&gt;118&lt;/b&gt;&lt;br /&gt;
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  Cannot modify header information - headers already sent by (output started at phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php:61) in &lt;b&gt;/home/&lt;userDir&gt;/&lt;hostingDir&gt;/wp-includes/pluggable.php&lt;/b&gt; on line &lt;b&gt;1216&lt;/b&gt;&lt;br /&gt;
  </code></pre>
  
  <p>========================================</p>
  
  <p>I've also run the whoami command via cron and it confirms that it's the same user running in the shell and the cron job.</p>
  
  <p>Running wp --info in the shell gives this:</p>
  
  <pre><code>OS:     Linux 3.10.0-514.21.2.el7.centos.plus.x86_64 #1 SMP Wed Jun 21 17:20:54 UTC 2017 x86_64
  Shell:  /bin/bash
  PHP binary:     /opt/cpanel/ea-php56/root/usr/bin/php
  PHP version:    5.6.33
  php.ini used:   /opt/cpanel/ea-php56/root/etc/php.ini
  WP-CLI root dir:        phar://wp-cli.phar
  WP-CLI vendor dir:      phar://wp-cli.phar/vendor
  WP_CLI phar path:       /home/&lt;userDir&gt;
  WP-CLI packages dir:
  WP-CLI global config:
  WP-CLI project config:
  WP-CLI version: 1.5.0
  </code></pre>
  
  <p>=================================</p>
  
  <p>Running wp --info in the cron job gives this:</p>
  
  <pre><code>OS:     Linux 3.10.0-514.21.2.el7.centos.plus.x86_64 #1 SMP Wed Jun 21 17:20:54 UTC 2017 x86_64
  Shell:  /bin/bash
  PHP binary:     /opt/cpanel/ea-php56/root/usr/bin/php-cgi
  PHP version:    5.6.33
  php.ini used:   /opt/cpanel/ea-php56/root/etc/php.ini
  WP-CLI root dir:        phar://wp-cli.phar
  WP-CLI vendor dir:      phar://wp-cli.phar/vendor
  WP_CLI phar path:       /usr/local/bin
  WP-CLI packages dir:
  WP-CLI global config:
  WP-CLI project config:
  WP-CLI version: 1.5.0
  </code></pre>
  
  <p>================</p>
  
  <p>Now, the PHP binary paths are different. As is the WP_CLI phar path.</p>
  
  <p>I'm assuming this may be the problem?</p>
  
  <p>But I don't know why they are different, or how to reconcile them...</p>
  
  <p>Any insights gratefully received.</p>
  
  <p>Thanks,
  Norman</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-07T22:57:40.843Z
Comments.Text: To add to this. If I explicitly specify the path to the PHP binary, ala: /opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/wp --info
  Then it works and I get identical outputs. So that's a workaround, but hardly ideal. The core problem is why cron is running in a different environment...
Comments.UserDisplayName: Norman Cates
Tags: <cron>
Title: Problem with wp-cli and setting up cron in CiviCRM
ThreadText: # Problem with wp-cli and setting up cron in CiviCRM
  
  (Asked by: username_0 on 2018-03-07)
  
  I'm trying to set up the CiviCRM cron job.
  I've downloaded and installed wp-cli according to the instructions on
  http://wp-cli.org/
  I have logged into the server as the user running the cron job.
  I can successfully run this command in the shell:
   /usr/local/bin/wp  --url=https:/// --path=/home/// civicrm api job.execute auth=0 
  
  But if I run the same command in cron it returns this error in the Cron email:
  ===============================
  Set-Cookie: PHPSESSID=73sl011599lmjbg5038j5n06q4; path=/
  Expires: Thu, 19 Nov 1981 08:52:00 GMT
  Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  Pragma: no-cache
  Content-type: text/html; charset=UTF-8
  
  
  Warning:  fwrite() expects parameter 1 to be resource, string given in phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php on line 61
  
  Warning:  fwrite() expects parameter 1 to be resource, string given in phar:///usr/local/bin/wp/php/utils-wp.php on line 118
  
  Warning:  Cannot modify header information - headers already sent by (output started at phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php:61) in /home///wp-includes/pluggable.php on line 1216
  
  ========================================
  I've also run the whoami command via cron and it confirms that it's the same user running in the shell and the cron job.
  Running wp --info in the shell gives this:
  OS:     Linux 3.10.0-514.21.2.el7.centos.plus.x86_64 #1 SMP Wed Jun 21 17:20:54 UTC 2017 x86_64
  Shell:  /bin/bash
  PHP binary:     /opt/cpanel/ea-php56/root/usr/bin/php
  PHP version:    5.6.33
  php.ini used:   /opt/cpanel/ea-php56/root/etc/php.ini
  WP-CLI root dir:        phar://wp-cli.phar
  WP-CLI vendor dir:      phar://wp-cli.phar/vendor
  WP_CLI phar path:       /home/
  WP-CLI packages dir:
  WP-CLI global config:
  WP-CLI project config:
  WP-CLI version: 1.5.0
  
  =================================
  Running wp --info in the cron job gives this:
  OS:     Linux 3.10.0-514.21.2.el7.centos.plus.x86_64 #1 SMP Wed Jun 21 17:20:54 UTC 2017 x86_64
  Shell:  /bin/bash
  PHP binary:     /opt/cpanel/ea-php56/root/usr/bin/php-cgi
  PHP version:    5.6.33
  php.ini used:   /opt/cpanel/ea-php56/root/etc/php.ini
  WP-CLI root dir:        phar://wp-cli.phar
  WP-CLI vendor dir:      phar://wp-cli.phar/vendor
  WP_CLI phar path:       /usr/local/bin
  WP-CLI packages dir:
  WP-CLI global config:
  WP-CLI project config:
  WP-CLI version: 1.5.0
  
  ================
  Now, the PHP binary paths are different. As is the WP_CLI phar path.
  I'm assuming this may be the problem?
  But I don't know why they are different, or how to reconcile them...
  Any insights gratefully received.
  Thanks,
  Norman
  
  **Tags:** cron
  
  > **Comments:**
  > **username_0**: To add to this. If I explicitly specify the path to the PHP binary, ala: /opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/wp --info Then it works and I get identical outputs. So that's a workaround, but hardly ideal. The core problem is why cron is running in a different environment...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-03-08. Score: 2 | Accepted answer
  
  After much research and getting expert ideas...
  (I'm sure this is known to many people here, but I'm posting here to hopefully help others who don't know this.)
  TLDR; in Cron jobs, you should fully specify the path to the PHP binary as well as wp-cli.
  =======================================
  Cron runs in its own minimal environment. It does run as the relevant user, but it does not load most of the user environment info. This post was helpful for me to understand some of this:
  https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user
  I am running cPanel which does some of its own management of php. The cPanel support people were very helpful in helping me understand more.
  What is not obvious is that when you login to a shell, various paths are added, even ones that are NOT in the user's .bash_profile. Cron does not necessarily do the same thing.
  One that is added with a shell login is /usr/local/bin/
  So typing "which php" in a shell gives /usr/local/bin/php, but in cron it gives /usr/bin/php
  In cPanel, or indeed other tools I expect, this contains references to specific php binaries that can change depending on what user you are. 
  I don't pretend to understand the underlying mechanism.
  But the upshot is that the php command points to a different binary in the shell or via cron.
  So in the cron job, you ALSO have to point at the specific php binary with a command like:
  /usr/local/bin/php /usr/local/bin/wp --info (etc)
  
  Or even a FULL path to the desired php binary:
  /opt/cpanel/ea-php56/root/usr/bin/php /usr/local/bin/wp --info (etc)
  
  In cPanel there is also a known problem when using PHP-FPM for a site where the automatic PHP detection doesn't work, so it falls back to the cPanel PHP default. They are working on fixing that. If this is a problem, then setting the full path to the desired PHP binary will work.
  
  
  ---
  
  