如何打开带有GPIO输入的终端并运行bash脚本并实时查看输出?

时间:2018-03-25 10:56:03

标签: python bash terminal raspberry-pi3 gpio

感谢您的阅读。我有这个pi tft屏幕,触摸按钮连接到它。当一个按钮保持两秒钟时,我想打开一个终端并运行一个bash脚本。问题是我需要实时查看脚本的输出/进度。

这个监视GPIO的python脚本,如果一个终端已经打开,那么它可以正常工作,然后我可以看到输出。

from gpiozero import Button
from signal import pause
import subprocess
def runboth():

subprocess.call(["/opt/script/test.sh"])

runboth_btn = Button(17, hold_time=2)
runboth_btn.when_held = runboth


pause()

但是如果python脚本是从启动运行的,那么没有终端打开,因为我猜它在后台运行?我怎样才能实现脚本也会打开一个终端?

我尝试过以下但没有运气。

from gpiozero import Button
from signal import pause
import subprocess

def runboth():

    subprocess.call(["lxterminal", "--command=/opt/script/test.sh"])

    runboth_btn = Button(17, hold_time=2)
    runboth_btn.when_held = runboth


    pause()

我遇到了这个错误

root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/bin/lxterminal --command=/opt/script/test.sh
Mar 24 23:24:42 raspberrypi sudo[1315]: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 24 23:24:42 raspberrypi lxterminal[1319]: cannot open display: 0:0

或者如果从终端

运行
lxterminal:1404): Gtk-WARNING **: cannot open display: :0.0

这是我目前用于测试的bash脚本。

#!/bin/bash
while true; do
echo "hello"
done

我使用root权限运行Raspberry PI 3,Raspian OS。

再次感谢您阅读并花时间!

0 个答案:

没有答案