0

Greeting's,

I am running an apache2 server on my raspberry pi. I wanted to keep a tab on it's temperature while I'm travelling. I know that following two commands will give me the GPU and CPU temperature:

GPU =  vcgencmd measure_temp
CPU = cat /sys/class/thermal/thermal_zone0/temp

I also know that I can send an email using SSMTP. I will be using Gmail for this. I don't need help with sending email.

What I need help with is creating a file or a variable that will store the data every hour when the cron job runs and email the output at the end of the day. I would also like to flush this variable at the end of the day. I am using a variable as opposed to sending a file for the sake of easy reading on mobile client (instead of opening an attachment).

#!/bin/bash
# Script: temp_email.sh
# -------------------------------------------------------
cpu=$(</sys/class/thermal/thermal_zone0/temp)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $(/opt/vc/bin/vcgencmd measure_temp)"
echo "CPU => $((cpu/1000))'C"

How do I tie all of this together to send me a single email once every 24 hours with temperature of the Pi measured and stored once per hour?

PS: I'm running RASPBIAN STRETCH LITE in headless mode.

  • In order to send an email you will need access to an actual SMTP server, or find a mail API to work with whatever your provider provides along these lines. While it is not impossible to send mail via an SMTP server on the pi, you may have problems getting that mail relayed. – goldilocks Oct 27 '18 at 14:24
  • I'm going to use Gmail to send the emails. ;) – Parth Maniar Oct 27 '18 at 14:38
  • 1
    You should clarify in the question that you already know how to send the email then, since a lot of it seems to be about asking how to do that. – goldilocks Oct 27 '18 at 14:43
2

Here are two approaches. I don't claim that either is the best or perfect solution, but both should work just fine for your needs.

1. Make your script do all the work

Loop your entire script.

Add a sleep 3600 line somewhere in your .sh script to make it wait 1 hour between readings. Store these readings in a file by > from echo to the file of your choice.

Add some sort of count that increments every time the temperature is read. When this count mod24 = 0, have your script execute the command that sends the email. After it sends the email, delete or wipe the file.

With crontab -e add the line @reboot sleep 60 && /path/to/my_script.sh that way your script will automatically start 1 minute after you boot your pi.

2. Make cron do all the work.

Leave your script exactly as it is.

With crontab -e add the lines:

0 * * * * sh /path/to/my_script.sh > /path/to/temperatures.txt
0 12 * * * <the commands you use to email the file> && rm temperatures.txt

Explaination:

The first command will run your script at the 0th minute of every hour, and save the results to temperatures.txt.

The second command (make sure to replace <the commands you use to email the file> including the < and > with your command) will email you that file at the 0th minute on the 12th hour of every day (change 12 to whatever hour you would prefer to receive the email. Acceptable values range from 0 to 23). Then the && rm temperatures.txt clears the file so the new day of data can begin.

Hope this helps.

  • 1
    This is perfect. I truly appreciate it. I have tweaked it a bit to fit my needs. First task is running the script which I've tweaked to append output to a file. /path/script file >> /path/file.txt Second task sends the email and here is the tweak. I want to keep the footprint of installed software low. So instead of installing additinal software to attach a file. I've used cat command and pipe the output as part of the email body. hence: cat /path/file.txt | mail -s "Test Subject" email@id.com && rm /path.output_file.txt I truly appreciate your assistance. – Parth Maniar Oct 28 '18 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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