linux - 如何将 CTRL+C 从 Node-RED 传递到 Raspberry Pi 终端?

标签 linux raspberry-pi node-red

我正在创建一个访问高中生的教程。

使用 Node-RED 我有一个运行用 python 编写的脚本的 Raspberry Pi。我希望通过传递 keyboardInterrupt (CTRL+C) 从 node-RED 结束 python 脚本,以便脚本正常结束并为下一次运行清理 GPIO。

通过使用 node-RED 中的 EXEC block 并将“sudo python Documents/python/blinker.py”传递到终端,我可以很好地运行脚本。问题是因为我不是直接在终端工作,我不能只使用 CTRL+C 退出 while 循环。有没有办法将 CTRL+C 从 node-RED 传递到终端?

我已经为那些感兴趣的人附上了 python 代码:

import RPi.GPIO as GPIO
import time

pwmPin = 18
ledPin = 23
butPin = 17

dc = 95

GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(pwmPin, GPIO.OUT)
pwm = GPIO.PWM(pwmPin, 50)
GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(ledPin, GPIO.LOW)
pwm.start(dc)

print("Press CTRL+C to exit")
try:
        while 1:
                if GPIO.input(butPin):
                        pwm.ChangeDutyCycle(dc)
                        GPIO.output(ledPin, GPIO.LOW)
                else:
                        pwm.ChangeDutyCycle(100-dc)
                        GPIO.output(ledPin, GPIO.HIGH)
                        time.sleep(0.075)
                        GPIO.output(ledPin, GPIO.LOW)
                        time.sleep(0.075)
except keyboardInterrupt:
        pwm.stop()
        GPIO.cleanup()

我已尝试使用 kill 和 killall 命令并终止应用程序,但 GPIO.cleanup() 没有运行,任何打开的输出都将保持打开状态。

我也曾尝试传递“echo $'\cc' | ./blinker.py”,但遇到了一个前提错误。

/bin/sh: 1: Documents/python/blinker.py: Permission denied echo: write error: Broken pipe

感谢任何帮助

最佳答案

对于长时间运行的脚本,您可能应该使用 daemon节点而不是执行节点。

而且,当部署新版本的流时,这两个节点都会尝试终止任何正在运行的脚本实例(导致重建节点)。

您还可以通过向它发送带有要发送信号名称的 msg.kill 来显式终止守护进程节点。来自守护进程节点文档:

Setting msg.kill to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the restart flag is set it will then auto restart.

您使用 sudo 运行脚本的事实是您不能直接终止脚本的原因,因为您试图终止 root 拥有的进程。如果您将信号发送到正在运行该进程的 sudo 实例,则应该终止子进程。

关于linux - 如何将 CTRL+C 从 Node-RED 传递到 Raspberry Pi 终端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377356/

相关文章:

javascript - 如何将一条消息中包含的 JSON 对象数组转换为多个 JSON 对象消息?

docker - 使用resin.io部署时如何更新node-red/node-red-docker镜像中的settings.js?

linux - 仅在挂载 SAN 时才允许写入目录

python - 导入错误/lib/ld-linux-armhf.so.3 : version `gLIB _2.4' not found

c++ - Raspberry Pi 优化的 backtrace()

linux - 将现有的 rootfs 与自定义工具链相结合

Node-Red 和 MongoDB 使用 msg.collection 动态查询集合

linux - 从ftp获取同名文件

c - 使用 pthreads 我只得到大值的 "Segmentation fault (core dumped)"

linux - NOOBS 已安装,但无法正常启动,出现内核紧急错误代码 0x00000004”