Auto restart newrelic-daemon if crashed

On a web server with Newrelic APM installed, graph stopped showing new data. On checking the newrelic log file, i get following error

root@new-website-2:~# tail -f /var/log/newrelic/php_agent.log
2021-02-08 17:21:27.453 +0000 (680439 680439) warning: daemon connect(fd=13 uds=@newrelic) returned -1 errno=ECONNREFUSED. 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

This is because newrelic-daemon stopped working. To auto restart newrelic-daemon if it crash, do the following

mkdir -p /usr/serverok
vi /usr/serverok/newrelic

Add following to the file

#!/bin/bash
# Author: ServerOK
# Web: https://serverok.in/auto-restart-newrelic-daemon

CURRENT_STATUS="$(/etc/init.d/newrelic-daemon status)"

TIME_STAMP="$(date "+%Y-%m-%d %H:%M:%S")"

if [[ ! "${CURRENT_STATUS}" =~ "newrelic-daemon is running" ]]
then
    /etc/init.d/newrelic-daemon start
    echo -e "${TIME_STAMP} newrelic-daemon down\n"
fi

Make the script executable

chmod 755 /usr/serverok/newrelic 

Create a cronjob

crontab -e

Add following cronjob

*/5 * * * * /usr/serverok/newrelic > /var/log/newrelic/monitor.log 2>&1

See Server Monitoring, Application Performance Monitor (APM)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *