1

To give context, I'm building an IoT project that requires me to monitor some sensor inputs. These include Temperature, Fluid Flow and Momentary Button switching. The program has to monitor, report and control other output functions based on those inputs but is also managed by a web-based front-end. What I have been trying to do is have a program that runs in the background but can be controlled via shell commands.

My goal is to be able to do the following on a command line (bash):

pi@localhost> monitor start
sensors are now being monitored!
pi@localhost> monitor status
Temp: 43C
Flow: 12L/min

My current solution has been to create two separate programs, one that sits in the background, and the other is just a light-weight CLI. The background process listens to a bi-directional Linux Socket File which the CLI uses to send it commands. It then sends responses back through said socket file for the CLI to then process/display. This has given me many headaches but seemed the better option compared to using network sockets or mapped memory. I just have occasional problems with the socket file access when my program is improperly terminated which then requires me to "clean" the directory by manually deleting the socket file.

I'm also hoping to have the program insure there is only ever one instance of the monitor program running at any given time. I currently achieve this by capturing my pid and saving it to a file which I can look for when my program is starting. If the file exists, I self terminate with error. I really don't like this approach as it just feels too hacky for me.

So my question: Is there a better way to build a background process that can be easily controlled via command line? or is my current solution likely the best available?

Thanks in advance for any suggestions!

flamewave000
  • 547
  • 5
  • 12
  • 1
    YMMV, but I really like Redis for this. You can use Python, Perl, PHP, C/C++ or command line to store and query the current values of your monitored devices and a queue with blocking POPs to pass commands between modules. https://stackoverflow.com/a/45507702/2836621 – Mark Setchell Jan 15 '18 at 19:41
  • See basic data structures Redis can serve here... https://redis.io/topics/data-types-intro – Mark Setchell Jan 15 '18 at 19:43
  • For single instances... https://stackoverflow.com/a/37303133/2836621 – Mark Setchell Jan 15 '18 at 19:45
  • Of course, all the Redis interfaces including command line, allow you to specify an IP address of your Pi, so you can control things easily from remote machines too. – Mark Setchell Jan 15 '18 at 19:55
  • So from what I've been able to figure based on your helpful suggestions. Redis is a standalone DB-like server that offers global data storage and message queues, and it's data/queues are controlled directly from its CLI. So I would need to make a lot of `system()` calls. As for the singleton instancing, I can use `sem` to wrap my program launch to insure only one is running at a time. Do I have a good understanding of those suggestions? It's not perfect since both can be circumvented/exploited since those programs leave things a little exposed. – flamewave000 Jan 15 '18 at 20:49
  • 1
    No, you don't have to use the CLI at all, if you don't want to. There is a C/C++ API, a PHP API, a Python API and so on. – Mark Setchell Jan 15 '18 at 20:59
  • Ok, it wasn't very apparent on the redis.io website. I'm seeing some community made clients for it now. So I'm starting to think it might be a good idea to use redis as a communication bridge between mini-programs. I could have a mini-program for each sensor that could then report themselves to the redis server and the monitor program could simply act as a coordinator between them. Might be very interesting. I could then send direct commands to individual sensors/outputs and perform better unit testing (which I really like). – flamewave000 Jan 15 '18 at 21:11

0 Answers0