2

Edits: added comments to code.

I am running a small .py script on my Raspberry Pi with a DHT11 sensor and 3 LEDs. I am starting the program on boot with crontab, and everything is running fine apart from the logging of the data.

First of all, the temp.py script:

#The needed imports
import RPi.GPIO as GPIO
import Adafruit_DHT
import time
import logging

#setting up the GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)


print("Running temp")

#my original logging config for when i execute the .py file my self
#logging.basicConfig(filename='temp.log', level=logging.INFO, 
      #format='%(asctime)s: %(message)s')

#the logging config i have tried, so i am not forcing where its logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s: %(message)s')


while True:
        #records the humidity and temperature and sets them as hum and temp 
        hum, temp = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
        print(hum, temp)
        GPIO.output(16, False)
        GPIO.output(20, False)
        GPIO.output(21, False)

        #turns on LED to show the temp within range
        if int(temp) > 23:
                GPIO.output(16, True)
        if int(temp) < 24 and int(temp) > 19:
                GPIO.output(20, True)
        if int(temp) < 20:
                GPIO.output(21, True)

        #and here i do the actual logging
        logging.info('temp: ' + str(temp) + '  |||  hum: ' + str(hum))
        time.sleep(60)

So, when I run the temp.py manually (sudo python temp.py) and use the ORIGINAL logging config, it all works fine and I get an output in temp.log

Now, when I try and run my script with crontab:

@reboot sudo python share/temp.py >> share/temp.log 2>&1

the script runs fine, I get the visual output as expected, the temp.log file is created, but I get NO output in the temp.log, nor any errors.

As I stated above, I have tried to use filename='' and leave it out, no difference.

I have tried to, in the crontab to add:

SHELL=/bin/bash

but that did nothing (it's not really a bash file anyway)

I have tried to just write:

@reboot sudo python share/temp.py > share/temp.log still nothing.

As I have said, the logfile temp.log is created everytime, I just get no output.

As a last resort, I tried to remove the temp.log (sudo rm temp.log) while the script was running, after it had created the initial temp.log, and it never recreated another temp.log, it just kept running.

Does anyone have any idea to what I am doing wrong?

  • @reboot sudo python share/temp.py >> share/temp.log & – Andy Anderson Feb 18 '18 at 15:55
  • @AndyAnderson don't work sadly :/ – Hudlommen2 Feb 18 '18 at 17:07
  • 1
    Okay, i have found that if i change my crontab to: @reboot sudo python share/temp.py > share/temp.txt it works like a charm. – Hudlommen2 Feb 18 '18 at 17:25
  • If you want to redirect stderr to stdout then your redirection is wrong. It must be 2>&1 not 2&>1. You found the solution so please answer your question. – Ingo Feb 19 '18 at 0:01
  • @Ingo - yes you are right. It was a typo and i have now corrected it. – Hudlommen2 Feb 23 '18 at 6:10
0

tl;dr: just use a .txt :)

Through a bit of trial and error, a whole lot of google searches and some help in here i found the following:

I never managed to be able to write what i wanted to a .log file when using crontab's @reboot option.

What i found was the fact that you could just write to a .txt file instead and you would get all the output you wanted.

@reboot sudo python yourPath/fileName.py >> yourPath/fileName.txt 2>&1

why this is so, i have no idea. But if you don't especially want a .log file, just a log over your prints or whatever, just use a .txt :)

Edit: i am just adding this part just in case someone new as me comes across this post. To see your prints/logs in real time while the program is running use tail FileName.txt it shows the last few lines in the document. To get a constant update use tail -f FileName.txt it will update as soon as the txt is updated.

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.