[PHP] agent not working in CLI mode

I executing some long-running php scripts in CLI mode and I tried to use function newrelic_custom_metric to report some metrics. But when checking metrics explorer panel I don’t see any of reported metrics.

I checked php agent log and saw these lines:

2021-06-10 06:50:44.987 +0000 (23939 23939) info: attempt daemon connection via '/tmp/.newrelic.sock'
2021-06-10 06:50:44.987 +0000 (23939 23939) info: New Relic 9.4.1.250 ("kiwi" - "16cf35195903") [daemon='/tmp/.newrelic.sock'  php='7.3.18-1+ubuntu18.04.1+deb.sury.org+1' zts=no sapi='cli'  pid=23939 ppid=23031 uid=0 euid=0 gid=0 egid=0 backtrace=yes startup=agent os='Linux' rel='5.4.0-1049-aws' mach='x86_64' ver='#51~18.04.1-Ubuntu SMP Fri May 14 18' node='ec2-34-253-140-248.eu-west-1.compute.amazonaws.com']
2021-06-10 06:50:44.988 +0000 (23939 23939) info: spawned daemon child pid=23940
2021-06-10 06:50:45.054 +0000 (23939 23939) warning: daemon connect(fd=5 uds=/tmp/.newrelic.sock) returned -1 errno=ENOENT. Failed to connect to the newrelic-daemon. Please make sure that there is a properly configured newrelic-daemon running. For additional assistance, please see: https://newrelic.com/docs/php/newrelic-daemon-startup-modes

Why php agent can’t connect the daemon? When calling the script via web server (Apache 2 in my case) everything works fine. I read https://newrelic.com/docs/php/newrelic-daemon-startup-modes but that doesn’t answered my questions.

Any suggestions where to search for the problem?

PHP: 7.3.18
New Relic: 9.4.1.250
PHP have same configuration both for CLI and Apache modes.

Hey @stas4,

The PHP Agent will record CLI data whether it’s running in External or Agent mode. I’d suggest running the PHP Agent in agent mode, which will allow for it to handle all of the daemon process management.

Here are some steps to try, which should help fix this:

  1. Edit the newrelic.ini file and change the line newrelic.daemon.port="/tmp/.newrelic.sock" to newrelic.daemon.port="@newrelic-daemon".
  2. If you are running a system with SELinux, check the current status of it with the command sestatus. If this is currently set to anything other than Passive please mention it in this ticket.
  3. Verify all of the daemon processes are stopped with ps aux | grep newr and stop any that are still running.
  4. Remove the file /tmp/.newrelic.sock if it still exists.
  5. Remove or rename the file /etc/newrelic/newrelic.cfg if it exists. This file should only be used for advanced startups.
  6. Briefly stop your web server, sudo kill -9 newrelic-daemon to make sure all processes have exited, restart and allow up to 5 minutes for data to start reporting.

Let us know if this helps.

Cheers!

1 Like

Thanks @sudo, that helped!