這是腳本(/shutdown.py)。它監視按鈕按下,如果超過3秒按下按鈕,它會運行poweroff命令。Python循環():raw_input()EOFError:讀取一行時的EOF
#!/usr/bin/python
# Import the modules to send commands to the system and access GPIO pins
from subprocess import call
from time import sleep
import RPi.GPIO as gpio
import time
# Define a function to keep script running
def loop():
raw_input()
# Define a function to run when an interrupt is called
def shutdown(pin):
button_press_timer = 0
while True:
if (gpio.input(17) == False) : # while button is still pressed down
button_press_timer += 1 # keep counting until button is released
if button_press_timer == 3:
#print "powering off"
call('poweroff', shell=True)
sleep(1)
else: # button is released, figure out for how long
#print "Poga atlaista. nospiesta bija " + str(button_press_timer) + " sekundes"
#button_press_timer = 0
return
# sleep(1) # 1 sec delay so we can count seconds
# print "powering off"
gpio.setmode(gpio.BCM) # Use BCM GPIO numbers
gpio.setup(17, gpio.IN, pull_up_down=gpio.PUD_UP) # Set up GPIO 17 as an input
gpio.add_event_detect(17, gpio.FALLING, callback=shutdown, bouncetime=200) # Set up an interrupt to look for button presses
loop() # Run the loop function to keep script running
如果我從控制檯運行腳本,如/shutdown.py
一切都很好。檢測到按鈕被按下並且系統關機被初始化。但是,如果我是腳本添加到/etc/rc.local
(/shutdown.py &),那麼它在失敗與此錯誤啓動:
Traceback (most recent call last):
File "/shutdown.py", line 35, in <module>
loop() # Run the loop function to keep script running
File "/shutdown.py", line 11, in loop
raw_input()
EOFError: EOF when reading a line
如果我註釋掉loop()
線,那麼有沒有錯誤,腳本不能運行背景。我剛剛開始並退出,按下按鈕後未檢測到。那麼,我如何在啓動時運行該腳本並繼續在後臺運行?
編輯
我不是蟒蛇大師,我認爲這是loop()
蟒蛇內部功能。現在我看到它是定義的函數,它調用raw_input()
。我找到並修改了該腳本以適合我的需求。 謝謝。
剛剛用代碼編輯答案 – baxeico 2014-10-01 07:55:32