Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This code is meant to retrieve ascii from a temperature sensor in the fridge, check that it is not the error code (85 degrees) and append it to a csv file.
If the reading is 85 degrees, then either my fridge is on fire or the sensor cocked up, in which case it is meant to try again.
It's the last bit that I can't get to work.
Code:
#!/bin/bash
COUNT=4
while [ $COUNT -gt 0 ]; do
/usr/bin/digitemp_DS9097 -a -q -o"%d/%b/%g %H:%M%t%.2C" -c /etc/digitemp.conf > /home/brafferton/Downloads/check.txt
CHECK=$(tail -c 6 /home/brafferton/Downloads/check.txt)
if [ "${CHECK}" = 85.00 ];then
# echo bad result
COUNT=$((COUNT - 1))
else
COUNT=0
cat /home/brafferton/Downloads/check.txt >> /home/brafferton/Downloads/$(date +%G-%b).csv
# echo good result
fi
done
Check.txt is empty if the reading is bad, and nothing is appended to the log.
This check is done every 4 hours, day and night. But as you can see from below, a few results are missing.
If "Check.txt is empty if the reading is bad" then the "bad" test will never be passed ...
If /home/brafferton/Downloads/check.txt is empty when the reading is bad then CHECK=$(tail -c 6 /home/brafferton/Downloads/check.txt) sets CHECK to an empty string and if [ "${CHECK}" = 85.00 ] fails, COUNT is set to zero, an empty string is appended to /home/brafferton/Downloads/$(date +%G-%b).csv and the while loop ends.
Would it be more robust to check that /home/brafferton/Downloads/check.txt contains a valid value rather than sentinel value of 85.00? Or to test for it being empty as you say it is when the reading is bad?
I am eager to put in a check for a blank check.txt, but my knowledge of the code doesn't quite stretch that far. (this is my very first attempt at scripting)
I am now getting endless "good results", which is great, but doesn't really tell me if I've fixed it...
So the null bit worked.
Thing is....
If I type it on the command line, I get perfect results every time.
It's when cron does it that the errors come.
My crontab:
MAILTO=""
# m h dom mon dow command
# Check fridge door every 3 minutes during the day
*/3 8-22 * * * fridgedoor >/dev/null 2>&1
# Get temperature readings from fridge and save to monthly file in Downloads
# 30 */4 * * * fridge >/dev/null 2>&1
30 */4 * * * sh -x /usr/bin/fridge -l 2>&1 | tee /home/brafferton/Downloads/log.txt
# Send report via email on the last day of the month
0 1 1 * * /usr/bin/sendfridge
The logs don't show any errors for cron.
The log of sh -x /usr/bin/fridge -l 2>&1 | tee -a /home/brafferton/Downloads/log.txt
does show a crc error on the reading from digitemp - which, funnily enough, gives a null output.
My theory (whistling in the dark now) is that cron calls digitemp faster than I can do it on the keyboard, and digitemp sometimes can't handle it.
Now however, the new checks allow digitemp to re-read, and hey presto! it gets it right second time around. Good enough!
Thanks Jlinklels and catkin for your help. Much appreciated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.