Raspberry Pi 后台程序运行但不工作

Raspberry Pi background program runs but doesnt work

已解决:出于某种原因,使 CRON 运行 成为 bash 脚本 运行 的 python 脚本解决了问题。

我有一个 python 脚本 "temperature.py" 这是一个无限循环,它检查来自 GPIO 引脚的值,将值添加到一个文件,该文件使用 google 驱动器 "gdrive" 有时使用 smtp 发送邮件。如果我从 SSH 终端($ sudo python temperature.py)运行 脚本完美运行,但它在启动时无法像我希望的那样运行。 我正在使用 raspbian 喘息。

我做了什么:

在/etc/rc.local:

#...
#...

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

pi 正常启动,在我使用 SSH 登录并写入后:

...$ps aux

我得到:

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

所以我猜它是 运行ning 并且它使用 1.4% CPU 这非常少,但几乎所有其他进程都使用 0.0%。由于程序可能 但是它什么也没做...我的 google 驱动器是空的...

所以如果我 运行 它来自终端作为背景但如果我 运行 它来自 rc.local...

我的猜测:

  1. 它缺少一些权限?

  2. 它必须是 rc.local... 因为它在终端上完美运行

    的结果

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

我试过使用 cron ($sudo crontab -e) 启动它,但也没有用。

有什么想法吗?我觉得我遗漏了一些明显的东西,但由于我对 raspberry pi 和 linux 非常陌生,所以我无法在 google.

上找到它

脚本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 ;}\' ')
    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()

和 CRON:

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

考虑修改您的代码以不使用无限循环。

了解 Linux 个 CRON 作业。 CRON 是一项将按计划(正确)执行您的程序或脚本的服务。编辑:它默认安装在大多数 linux 发行版上,包括 Rasbian。

Some good examples

也许 nohup 有帮助?

nohup python /home/pi/temperature.py&