loes7815
Posts: 38
Joined: Thu Jan 28, 2016 3:15 pm

Cronjob doesn't work

Mon Feb 01, 2016 12:46 pm

Hi!

I got two sensors connected to my pi and I like to log their status to a csv file.
I made the following code:

Code: Select all

#!/usr/bin/python
import csv, os, glob, time, datetime


tfile1 = open("/sys/bus/w1/devices/28-00000742260e/w1_slave")
text1 =tfile1.read()
tfile1.close()
secondline1 = text1.split("\n")[1]
temperaturedata1 = secondline1.split(" ")[9]
temperature1 = float(temperaturedata1[2:])
temperature1 = temperature1 / 1000

tfile2 = open("/sys/bus/w1/devices/28-0000074270de/w1_slave")
text2 =tfile2.read()
tfile2.close()
secondline2 = text2.split("\n")[1]
temperaturedata2 = secondline2.split(" ")[9]
temperature2 = float(temperaturedata2[2:])
temperature2 = temperature2 / 1000

with open("/home/pi/temp.csv", "a") as csvfile:
    out = csv.writer(csvfile, delimiter=",",
                    quotechar='|',quoting=csv.QUOTE_MINIMAL)
    date = datetime.datetime.now()
    out.writerow([date.strftime("%Y"),date.strftime("%m"),date.strftime("%d"),date.strftime("%H"),date.strftime("%M"), temperature1, temperature2])
    time.sleep(5) #wait 5seconds


I made a cronjob.
using:

Code: Select all

sudo crontab -e
I choose the second option (sudo)

In the file I wanted to add

Code: Select all

***** /usr/bin/python /home/pi/temperature_csv.py
but I get an error saying bad command.

Code: Select all

***** sudo /usr/bin/python /home/pi/temperature.csv.py
doesnt work either

in addition, how can i stop the loop to view the data using the terminal?


thank you!
Last edited by loes7815 on Mon Feb 01, 2016 3:15 pm, edited 2 times in total.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Making a infinite loop

Mon Feb 01, 2016 12:53 pm

loes7815 wrote:how can i stop the loop to view the data using the terminal?
CTRL + Z only pauses the process.
CTRL + C

loes7815
Posts: 38
Joined: Thu Jan 28, 2016 3:15 pm

Re: Cronjob doesn't work

Mon Feb 01, 2016 3:16 pm

Thx!
Do you also have a solution for my other problem?

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Cronjob doesn't work

Mon Feb 01, 2016 3:21 pm

loes7815 wrote:Thx!
Do you also have a solution for my other problem?
yes
don't edit your original post to make it completely different to what it was

sudo crontab -e
will edit the root crontab

Code: Select all

***** /usr/bin/python /home/pi/temperature_csv.py
will need to be

Code: Select all

* * * * * /usr/bin/python /home/pi/temperature_csv.py
but this is a good way of trashing your SDcard
if you are running permanently loop inside your program and collate data more sensibly
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

loes7815
Posts: 38
Joined: Thu Jan 28, 2016 3:15 pm

Re: Cronjob doesn't work

Mon Feb 01, 2016 4:46 pm

I googled some more, and found this solution to my problem.
But it didn't work either, that's why I changed my original post.

But do you suggest to loop my program?
Because

Code: Select all

tfile1 = open("/sys/bus/w1/devices/28-00000742260e/w1_slave")
text1 =tfile1.read()
tfile1.close()
secondline1 = text1.split("\n")[1]
temperaturedata1 = secondline1.split(" ")[9]
temperature1 = float(temperaturedata1[2:])
temperature1 = temperature1 / 1000
Won't work if I make it a 'normal' loop, with 'While True' or 'For a in 1:'

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Cronjob doesn't work

Mon Feb 01, 2016 4:51 pm

loes7815 wrote:But do you suggest to loop my program?
Because

Code: Select all

tfile1 = open("/sys/bus/w1/devices/28-00000742260e/w1_slave")
text1 =tfile1.read()
tfile1.close()
secondline1 = text1.split("\n")[1]
temperaturedata1 = secondline1.split(" ")[9]
temperature1 = float(temperaturedata1[2:])
temperature1 = temperature1 / 1000
Won't work if I make it a 'normal' loop, with 'While True' or 'For a in 1:'
What exactly do you mean by that? Error messages?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cronjob doesn't work

Mon Feb 01, 2016 5:27 pm

It works when I try it

Code: Select all

import time

while True:
    tfile1 = open("/sys/bus/w1/devices/28-00000742260e/w1_slave")
    text1 =tfile1.read()
    tfile1.close()
    secondline1 = text1.split("\n")[1]
    temperaturedata1 = secondline1.split(" ")[9]
    temperature1 = float(temperaturedata1[2:])
    temperature1 = temperature1 / 1000
    print temperature1
    sleep( 60 )

loes7815
Posts: 38
Joined: Thu Jan 28, 2016 3:15 pm

Re: Cronjob doesn't work

Mon Feb 08, 2016 6:31 pm

thank you!
I think I made a typo, it's working now

Return to “Troubleshooting”