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?
2>&1not2&>1. You found the solution so please answer your question.