我目前正在创建一个带有显示屏的Raspberry Pi(Model B)温度传感器。我试图在LX终端中运行一个shell启动脚本,但是在Python子脚本旁边继续出现“Permission Denied”错误,如下所示:
pi@raspberrypi ~ $ sudo /home/pi/tempsense/etc/init.d/envmon start
Starting envmon
pi@raspberrypi ~ $ /home/pi/tempsense/etc/init.d/envmon: 15:/home/pi/tempsense/etc/init.d/envmon: home/pi/tempsense/opt/envmon/displayenvmon.py: Permission denied
shell脚本是:
#!/bin/sh
### BEGIN INIT INFO
# Provides: envmon
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop envmon
### END INIT INFO
case "$1" in
start)
/home/pi/tempsense/opt/envmon/dht11 &
echo "Starting envmon"
/home/pi/tempsense/opt/envmon/displayenvmon.py &
;;
stop)
pkill dht11
pkill displayenvmon
echo "envmon stopped"
;;
*)
echo "Usage: /home/pi/tempsense/etc/init.d/envmon {start|stop}"
exit 1
;;
esac
exit 0
导致Permission denied错误的Python脚本是:
#!/usr/local/lib/python2.7/dist-packages
from RPLCD import CharLCD
import RPi.GPIO as GPIO
import subprocess, re
import time
# Data file - current reading
datfile = "/var/envmon.data"
# Rate at which the LCD is updated
UPDATE_RATE = 5
lcd = CharLCD(cols=16, rows=2,
pin_rw=None,
pin_rs=7,
pin_e=8,
pins_data=[25,24,23,18],
numbering_mode=GPIO.BCM)
lcd.cursor_pos = (0, 0)
lcd.write_string('Visit @UWS_Pi')
lcd.cursor_pos = (1, 0)
lcd.write_string('TEMP & HUMID')
time.sleep(5);
# Get IP address - looks for first IP address which is not 127.0.0.1
address_string = subprocess.getoutput("ip addr")
ipaddr = "Unknown"
# Reg exp to extract IP addresss
search_inet = re.compile('inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
for line in address_string.split('\n') :
match = search_inet.search(line)
if (match != None) :
if (match.group(1) != '127.0.0.1') :
ipaddr = match.group(1)
break
# Print the IP address to the LCD
lcd.cursor_pos = (1,0)
lcd.write_string (ipaddr)
# Sleep to give chance to read IP address
time.sleep(5)
# Loop get most recent reading and display on screen
while (True):
fh = open (datfile, "r")
entry = fh.read()
# split into separate entries
[currtime, currtemp, currhumid] = entry.split()
# Change currtime to a formatted time ready for displaying
formattime = time.strftime ("%d/%m/%Y %H:%M", time.localtime(int(currtime)))
# print time to display
lcd.cursor_pos = (0,0)
lcd.write_string (formattime)
# Print temp and humidity values
lcd.cursor_pos = (1,0)
lcd.write_string ("T " + currtemp + "C RH " + currhumid + "% ")
time.sleep(UPDATE_RATE)
我是新手,我们非常感谢任何有用的建议。
答案 0 :(得分:1)
要使用GPIO引脚,需要使用管理员权限运行Python脚本。以root身份执行shell脚本或使用shell脚本中的sudo调用Python脚本。