【问题标题】:Permission Denied when trying to run Shell Script (containing Python sub-script)尝试运行 Shell 脚本(包含 Python 子脚本)时权限被拒绝
【发布时间】:2015-04-08 21:44:44
【问题描述】:

我目前正在创建一个基于 Raspberry Pi(B 型)的带显示屏的温度传感器。我正在尝试在 LX 终端中运行 shell 启动脚本,但在 Python 子脚本旁边不断收到“权限被拒绝”错误,如下所示:

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)

我是新手,如果有任何有用的建议,我们将不胜感激。

【问题讨论】:

  • 该python脚本是否设置了可执行位?
  • 我试过 chmod 777 如果这有助于@EtanReisner
  • /usr/local/lib/python2.7/dist-packages 真的是 python 二进制文件的路径吗?
  • 不是包含二进制文件的目录,而是实际上二进制文件本身?
  • 对不起,我是新手;我将如何检查那个伴侣? @EtanReisner

标签: python linux shell permissions raspberry-pi


【解决方案1】:

要使用 GPIO 引脚,Python 脚本需要以管理员权限运行。要么以 root 身份执行 shell 脚本,要么在 shell 脚本中使用 sudo 调用你的 Python 脚本。

【讨论】:

    【解决方案2】:

    要么以root身份登录,要么使用sudo运行程序,因为通常权限被拒绝意味着您需要root权限,如果您不想以root身份登录,sudo以root权限运行该特定程序。

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 2015-12-27
      • 2013-07-08
      相关资源
      最近更新 更多