A python script on my RPi reads the temperature from a DS18B20 probe located at /sys/bus/w1/devices/<probe id>/w1_slave.

Right now the program is hanging at the line to read the file:

with open(file_name) as f:
    return f.readlines()

If I try to cat the file on the command line, it also hangs until I hit ctrl+c.

I can "solve" the problem by rebooting the pi. However, what I want to do is have Python break out of the hang and then send me an email to do a manual reboot. Is this possible?

  • Have you tried setting non-blocking mode on the file and using read rather than readline? Presumably the hang is because the sensor is no longer working. – joan Jun 7 '15 at 19:30
  • If cat fails something is wrong. Since this is obviously a kernel mode driver, have you checked /var/log/syslog or dmesg? – goldilocks Jun 7 '15 at 20:55
  • can you start that in a seperate thread in Python? Then monitor the thread for response, if nothing kill it off. In C# that is what I would do if its an intermittent issue. – Piotr Kula Oct 22 '15 at 22:01

Instead of email, how about using watchdog? That way when you have an issue, if it hands the pi, it should reboot automatically. You can actually have it test the file directly.

See my post here regarding how to set up watchdog. Do not let it launch right after boot, put in some sleep time as explained in the post.

Not the answer you're looking for? Browse other questions tagged or ask your own question.