One of the functions of our managed Pis is to receive GPS information regularly, for which we are using Node’s dgram module.
We are using Pis in an environment which sees them power cycled several times throughout a normal day. When turned back on, device logs show that the ‘main’ service is already running:
Service is already running 'main sha256:de3cf67b9a9e5bcf71c6fae7341b731fd1bacdc3c3418b0d48ff6220995360cd'
From there, it appears that our container application is started. When it attempts to bind a socket with a static IP , we receive the error from main:
Error: bind EADDRNOTAVAIL, most likely as it was already bound by the ‘main’ that was already running.
How can I overcome this? Normally I’d register a socket.close() to occur on process.exit but with power being removed to the Pis that doesn’t get called. On restart it seems through balenaOS that ‘main’ isn’t being restarted so much as reconnected to. Using the balenaCloud interface, a simple restart of the ‘main’ service fixes this problem completely, but this isn’t a practical solution for how many devices we have or how often they will be power cycled.
Is there a way to force a restart of main every time the device turns back on? Or is there another solution completely?