View Single Post
turtle
Lord of the Rant.
Formerly turtle2472
 
Join Date: Mar 2005
Location: Upstate South Carolina
 
2021-05-28, 17:18

I need to daemonize a script rather than run it in a screen session. It works now, but once it crashes or whatever I need it to run again without me having to realize it stopped.

The background is I'm using a Raspberry Pi to serve as a temperature and humidity data logger. The original guide I followed use an older Adafruit_Python_DHT that has been deprecated. The newer script version is called CircuitPython-DHT. I've actually modified my copy so it doesn't send the alerts right to my Zabbix server but rather writes the temp and humidity readings to a txt file so it can be pulled by Zabbix.

This is my current script with my modifications:
Code:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import adafruit_dht # Initial the dht device, with data pin connected to: #dhtDevice = adafruit_dht.DHT22(board.D2) # you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. # This may be necessary on a Linux single board computer like the Raspberry Pi, # but it will not work in CircuitPython. dhtDevice = adafruit_dht.DHT22(board.D2, use_pulseio=False) while True: try: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity print( "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( temperature_f, temperature_c, humidity ) ) with open('/tmp/temp', 'w') as f: f.write("{:.1f}".format( temperature_f ) ) with open('/tmp/humidity', 'w') as f: f.write("{}".format( humidity ) ) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) time.sleep(2.0) continue except Exception as error: dhtDevice.exit() raise error time.sleep(5.0)
I know I can remove the "print" but I'm using that since I'm running it in screen. If I can demonize it that would go away. I looked at this article that talks about how to daemonize a script but I can't get it to work like I'm expecting and I know it is from my lack of knowledge of Python. Heck, it barked at me because I mixed spaces and tabs to indentation.

This is what I came up with but it doesn't do anything and no news is not good news in this case. There is no process running and I would expect nor a PID file ever actually created:
Code:
#!/usr/bin/python3 # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT from daemonize import Daemonize import time import board import adafruit_dht pid = "/tmp/dataDHT.pid" # Initial the dht device, with data pin connected to: dhtDevice = adafruit_dht.DHT22(board.D2) def main(): while True: try: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity #print( # "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( # temperature_f, temperature_c, humidity # ) #) with open('/tmp/temp', 'w') as f: f.write("{:.1f}".format( temperature_f ) ) with open('/tmp/humidity', 'w') as f: f.write("{}".format( humidity ) ) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) time.sleep(2.0) continue except Exception as error: dhtDevice.exit() raise error time.sleep(5.0) daemon = Daemonize(app="dataDHT", pid=pid, action=main) daemon.start()

Louis L'Amour, “To make democracy work, we must be a nation of participants, not simply observers. One who does not vote has no right to complain.”
Visit our archived Minecraft world! | Maybe someday I'll proof read, until then deal with it.
  quote