1

I am looking for a batch to make little commands in a terminal (for ubuntu) and save the answers in a data base in mysql in the same computer.

For example, I want to ask the temperature of the computer each hour, and save the answers in a data base already created. I do not if I should use php or just a batch to make a connection to the data base and save the information, but I do not know how to do neither of this posibilities.

Help please.

Bye.

1 Answers1

0

This is easily done in Python. First, learn how to get your CPU's temperature here. Either use the sensors command or the cat /sys/class/thermal/thermal_zone0/temp to write the current temperature to a temporary file curr.temperature. You can do that seperately in a bash script or in the Python program itself, using:

from subprocess import call
call (["/path/to/script/get_temperature"], shell=False)

Then the Python code just needs to read the file curr.temperature and store the value in a MySQL table. I have not tested this code, but it should be something like this:

#!/usr/bin/python
import MySQLdb.cursors
with open('curr.temperature', 'r') as f:
    read_data = f.read() 
db = MySQLdb.connect(db='databasename', host='localhost',
                     port=3306, user='MySQL-username', passwd='password',
                     cursorclass=MySQLdb.cursors.DictCursor)
cur = db.cursor()
cur.execute("INSERT INTO temperatures VALUES (now," + read_data + ")" )
Jos
  • 29,224
  • If you are satisfied with my answer, please click the check mark next to the question to mark the question as "answered". – Jos Sep 14 '15 at 10:16