Raspberry Pi background program runs but doesnt work
Question: SOLVED: For some reason making CRON run a bash script that runs the python
script solved the problem.

I have a python script "temperature.py" which is an infinite loop that checks
values from a GPIO pin, adds the values to a file which it uploads to google
drive with "gdrive" and sometimes sends a mail using smtp. The script works
perfectly if i run it from the SSH terminal ($ sudo python temperature.py) but
it doesn't work at startup like i would want it to. I'm using raspbian wheezy.

What I've done:

in /etc/rc.local:

    
    
    #...
    #...
    
    sleep 10
    python /home/pi/temperature.py&
    exit 0
    

the pi boots normally and after i login using SSH and write:

    
    
    ...$ps aux
    

i get:

    
    
    ...
    root   2357   1.4   1.9   10556   8836 ?    S 21:11   0:12 python /home/pi/temperature.py
    ...
    

so I'm guessing it is running and it uses 1.4% CPU which is very little but
almost all other processes use 0.0%. Since the program probably It doesnt do
anything however... my google drive is empty...

**So it works if i run it from terminal as background but not if i run it from
rc.local...**

What I'm guessing :

  1. it lacks some permission?

  2. it must be something with rc.local... since it works perfectly from terminal The result of 

...$ls -l temperature.py -rwxr-xr-x 1 root root 1927 Dec 12 21:10
temperature.py ...$ls -l /etc/rc.local -rwxr-xr-x 1 root root 373 Dec 12 20:54
/etc/rc.local

I have tried staring it using cron ($sudo crontab -e) but it didn't work
either.

Any ideas? I feel like I'm missing something obvious but since I'm very new to
raspberry pi and linux stuff I can't find it on google.

The script temperature.py

    
    
    #Made by Matthew Kirk
    # Licensed under MIT License, see
    # http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/LICENSE
    #Adapted by me
    
    import RPi.GPIO as GPIO
    import time
    import subprocess
    import os
    import commands
    import sys
    import smtplib
    from email.mime.text import MIMEText
    
    print 'TEMPERATURE LOGGER - M'
    print ' '
    #MAILER SETUP
    to = '****@gmail.com'
    gmail_user = '****@gmail.com'
    gmail_password = '*****'
    smtpserver = smtplib.SMTP('smtp.gmail.com',587)
    
    #TEMP LOGGER GPIO
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(7,GPIO.IN)
    
    while True:
        print 'fail'
        if GPIO.input(7):
                break
    
    while GPIO.input(7):
        pass
    
    waitTime = 60
    
    tempTreshold = 50
    logFile  = "/home/pi/tDat.csv"
    while True:
        dataFile = open(logFile,"a")
        time_1 = time.time()
        tFile = open("/sys/bus/w1/devices/28-011582ac5dff/w1_slave")
        text = tFile.read();
        tFile.close();
    
        tData = text.split("\n")[1].split(" ")[9]
        temp = float(tData[2:])
        temp = temp/1000
        timeStamp = time.strftime("%d/%m/%Y %H:%M:%S")
        dataFile.write(str(temp)+","+ timeStamp+ "\n")
        dataFile.close()
        file_ID = commands.getoutput('drive list | tail -n +2 | head -1 | awk \'{print $1;}\' ')
        cmd = 'drive delete --id '+file_ID
        os.system( cmd )
        cmd = 'drive upload --file '+logFile
        os.system( cmd )
     # MAIL IF TEMP TOO LOW
        if temp < tempTreshold:
                smtpserver.ehlo()
                smtpserver.starttls()
                smtpserver.ehlo()
                smtpserver.login(gmail_user,gmail_password)
                msg = MIMEText('The temperature in Branten, {}C, is below {} degrees C!!!'.format(temp,tempTreshold)+'\n'+'Recorded$
                msg['Subject'] = 'Branten Temperature Warning'
                msg['From'] = gmail_user
                msg['To'] = to
                smtpserver.sendmail(gmail_user,[to],msg.as_string())
                smtpserver.quit()
    
        sys.exit()
    

and the CRON:

    
    
    * * * * * python /home/pi/temperature.py
    


Answer: Consider revising your code to not use an infinate loop.

Read about Linux CRON jobs. CRON is a service that will execute your program
or script on a schedule (properly). EDIT: it is installed by default on most
linux distros including Rasbian.

[Some good examples](http://www.thegeekstuff.com/2009/06/15-practical-crontab-
examples/)

