I currently have Homebridge set up on my raspberry pi. When the pi boots, it starts a script which attempts to keep homebridge alive. I originally took the script from this answer which walks you through the rather trivial process of creating such a script. However, I have slightly adapted the script and it now looks like this:

until "homebridge" -s /bin/sh pi; do
    echo "Server homebridge crashed with exit code $?.  Respawning.." >&2
    echo "Looks like Homebridge just crashed, restarting it now..." | mail -s "Homebridge Crash" pi
    rm -r /home/pi/.homebridge/accessories/cachedAccessories
    sleep 1

It is virtually the same as the original script with the exception that it deletes a folder and waits a second before re-spawning. Furthermore, it sends some mail to my user (pi) to let me know that the process has died and that it is re-spawning. This has been working perfectly for me with the simple omission of any sort of de-bugging. By that I mean that whilst I do get notified that the process has died, I am not presented with an output of the process when it died. It would be perfect if the mail could include, for example, the last 300 lines before the process exited in order to aid with debugging following a crash

What exactly would I need to add to the above script in order to receive a 'log' of what the homebridge output was just before it crashed in order to debug it?

  • 1
    Which information exactly do you need? The output on stdout and/or stderr? Does Homebridge write a logfile like /var/log/homebridge.log or does it use syslog? maybe you can configure it to do this. Then it would be easy to filter the log and use tail. – Bodo Jan 15 '19 at 9:39
  • @Bodo All output would be great, (both stdout and stderr). I know that homebridge does supposedly write a log either to /var/log/homebridge.log or /var/log/homebridge.err as per here and I understand that tail would be a simple solution. However, neither of these files exist for me and that is why I am wondering whether it would be able to integrate it into the script shown above itself. – Rocco Jan 15 '19 at 16:36
  • I suggest to find out how to enable the log output. Of course you can use something like until "homebridge" -s /bin/sh pi > outputfile 2>&1; do and later use something like tail -100 outputfile, but the file may permanently grow limited only by the available space. The regular log mechanism probably already has a mechanism for log file switching and deleting of old log files. – Bodo Jan 15 '19 at 17:00
  • Wouldn't it be better to let systemd (re)start homebridge? – Bodo Jan 15 '19 at 17:03

Your Answer

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

Browse other questions tagged or ask your own question.