【发布时间】:2021-11-08 18:47:33
【问题描述】:
我在理解我的代码发生了什么方面有点问题。 我的代码中有很多行,所以我决定用较短的版本来模拟我的问题。 我在使用 raspberry pi 4,在 rapsbian 上使用 flirone。
import sys
import os
import time
import subprocess
global shellscript
#global pid
def subprocess_settings():
shellscript = subprocess.Popen(["/home/pi/Desktop/init_flir.sh"], close_fds=True)
#shellscript = subprocess.Popen(["/home/pi/Desktop/init_flir.sh", "&> /dev/null"], stdin=None, stdout=None, close_fds=True)
#pid = shellscript.pid
try:
x = 1
while True:
if x == 0:
sys.exit()
elif x == 1:
print("Yas, x=1")
time.sleep(2)
subprocess_settings()
time.sleep(5)
print("Driver test is in background mode")
time.sleep(2)
print("Close process")
subprocess.Popen.kill(shellscript)
x=0
except KeyboardInterrupt:
subprocess.Popen.kill(shellscript)
还有我的 .sh 文件:
#!/bin/bash
cd /home/pi/github/flirone-v4l2
sudo modprobe v4l2loopback exclusive_caps=0,0 video_nr=1,2,3
sudo ./flirone ~/github/flirone-v4l2/palettes/Iron2.raw
我想运行我的 .sh 文件。它必须在后台工作。我需要用两种方式终止它,第一种来自键盘,第二种在我的 elif 中。
最后我需要隐藏 .sh 文件的终端输出。
我做错了什么?? 这是全局变量的问题吗?
如何解决这个问题才能正常工作? 感谢您的帮助。
【问题讨论】:
-
你永远不会定义
shellscript,你只是称之为global shellscript -
是的,我在从 vnc 查看器中复制时遇到了问题。这是一个短暂的错误,所以我决定截屏。
标签: python shell subprocess popen kill