【问题标题】:Subprocess Popen problem with terminate shell script终止shell脚本的子进程Popen问题
【发布时间】: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


【解决方案1】:

如我所愿,此解决方案可以正常工作。在 flirone 驱动程序中,我运行了 ./flirone 并从 subprocess.Popen 打开了 1 个进程,并在 flirone 代码中打开了 2 个附加进程。我检查了这个运行命令 sudo ./flirone ~/github/flirone-v4l2/palettes/Iron2.rawps aux | grep flirone 。我使用pidof flirone 命令检查主ID。当我杀死这个进程时,一切都会正常运行,其他进程也会结束。

import sys
import os
import signal
import time
import subprocess

def subprocess_kill():
      
    pidof = subprocess.check_output("pidof flirone", shell=True)
    time.sleep(1)

    subprocess.check_output("sudo kill -9 %s " % (pidof), shell=True)
    
     
try:

    x = 1
    
    while True:
        if x == 0:
        
            sys.exit()            
            
        elif x == 1:

            print("x=1")
            time.sleep(2)
            os.chdir("/home/pi/github/flirone-v4l2")
            shellscript = subprocess.Popen("sudo modprobe v4l2loopback exclusive_caps=0,0 video_nr=1,2,3", shell=True)
            shellscript2 = subprocess.Popen("sudo ./flirone ~/github/flirone-v4l2/palettes/Iron2.raw", shell=True)
            time.sleep(5)
            print("Driver test is in background mode")
            time.sleep(2)
            print("Close process")

            subprocess_kill()

            x=0

except KeyboardInterrupt:

    print("hey")

    

当我从except KeyboardInterrupt 中删除杀戮方法时,进程也结束了。

如果你想隐藏子进程的输出,你可以使用:

FNULL = open(os.devnull, "w")
shellscript2 = subprocess.Popen("sudo ./flirone ~/github/flirone-v4l2/palettes/Iron2.raw", shell=True, stdout=FNULL)

更多:How to hide output of subprocess in Python 2.7

Subprocess python 2.7 lib

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 2010-11-01
    • 1970-01-01
    • 2011-12-10
    • 2022-01-19
    • 2013-11-22
    • 1970-01-01
    • 2011-07-14
    • 2017-12-27
    相关资源
    最近更新 更多