0

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://<URL of Server>/ --path=/home/<userDir>/<hostingDir>/ 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

<br />
<b>Warning</b>:  fwrite() expects parameter 1 to be resource, string given in <b>phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php</b> on line <b>61</b><br />
<br />
<b>Warning</b>:  fwrite() expects parameter 1 to be resource, string given in <b>phar:///usr/local/bin/wp/php/utils-wp.php</b> on line <b>118</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at phar:///usr/local/bin/wp/php/WP_CLI/Loggers/Base.php:61) in <b>/home/<userDir>/<hostingDir>/wp-includes/pluggable.php</b> on line <b>1216</b><br />

========================================

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/<userDir>
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

  • 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... – Norman Cates Mar 7 '18 at 22:57
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.