Friday 27 May 2016

Making a python daemon

I have arduino (https://github.com/mika-koivusaari/read_kwh_pulse) that counts pulses, and you can then query those counts through usb serial. Currently it measures electricity consumption of our house and geothermal heat pump. I needed a way to store those values in a database.

I quickly cobbled a script that just read values every minute and inserted them to MySql. It worked pretty fine, except you had to start it manually if something went wrong and when the server was rebooted. So I started to rewrite it to a proper unix daemon, and it's hard:)

For the serial part I used pyserial. http://www.varesano.net/blog/fabio/serial%20rs232%20connections%20python has a good example.

Python has a couple of implementations for daemonizing a process, first I tried with https://pypi.python.org/pypi/python-daemon/ but it didn't seem to have good documentation, so I switched to http://pep3143daemon.readthedocs.io/en/latest/.

Problems I encountered.

Security, when you start you can have root privileges, but when you are running you really shouldn't be root. So when you reload your configuration when the user ask and try to open files it's very well possible you can't.

Signals, you should close all your resources nicely. So you should catch signals like SIGTERM. But I just couldn't get that to work. But it turns out all I had to do was read the documentation. https://docs.python.org/2/library/signal.html Signal handlers need to take two parameters, signal number and current stack frame.

Debugging, still haven't figured this one out. Daemon processes need to close all filehandles before becoming a daemon, and I'm guessing that (atleast PyCharm) opens some socket to the process it's debugging and when the daemonize process closes that you can't debug anymore. And it's really hard to find errors when you also don't have stdout or stderr and you haven't yet opened your log file, or there is something wrong with your logging.

Current code for my daemon is in github https://github.com/mika-koivusaari/read_pulse_mysql

No comments:

Post a Comment