I have a python script to log data from a USB weather station and present it via a flask front end. The USB on the pi (Pi2 model B) is flaky and occasionally hangs after 24-48 hours. The flask app doesn't seem to cope to well when my home broadband glitches out. To cope with both problems, I reboot the pi every night at 1 am through cron. I've also created an @reboot cron entry to run the python/flask app.
So, the problem I have is this:
- If I run the script manually, it works perfectly every time
- If I manually reboot with "shutdown -r now", it works perfectly every time
- When the reboot runs though crontab, it consistently 100% of the time time fails to initialize the USB interface and usually without an error message. (Any error seen is a "timeout" which is semi-normal so ignored by the app. The flask front end still works fine.)
Is there something different about a cron-initiated script and the interaction with the USB subsystem?
Tried unsuccessfully: usbreset app, writing to "authorized" file for the USB device. They don't break anything but they don't fix the problem either.
pi is running Linux 4.19.27-v7+ #1206 SMP
crontab for the weather station user: (just one line)
@reboot bash /home/pi/wd >/home/pi/wdstartup.log 2>&1
crontab for root: (one line again)
0 1 * * * /sbin/shutdown -r now
the weather station startup script:
#!/bin/bash cd ~pi cd weatherd ./usbreset /dev/bus/usb/001/004 sudo echo 0 > /sys/bus/usb/devices/1-1.4/authorized sudo echo 1 > /sys/bus/usb/devices/1-1.4/authorized sleep 60 cp wd.log wd.log.old nohup python3 weatherd.py >& wd.log & cd ~pi