-1

I have a little problem with understand what is going on with my code. I have a lot of lines in my code so i decided to simulate my problem with shorter version. Im using raspberry pi 4, flirone on rapsbian.

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)

and my .sh file:

#!/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

I want to run my .sh file. It has to work in background. I need to terminate this with 2 ways, first one from keyboard and second inside my elif.

At the end i need to hide terminal output from .sh file.

What im doing wrong?? Is this is a problem with global variables?

enter image description here

How to fix this to work properly? Thanks for any help.

Selenee
  • 9
  • 1
  • 1
    you never define `shellscript` you just call it `global shellscript` – It_is_Chris Sep 13 '21 at 13:14
  • Please [don’t post images of code or error messages.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Sep 13 '21 at 15:55
  • yes right, i had problems to copy from vnc viewer. It was short error so i decided to make a screen shot. – Selenee Sep 13 '21 at 17:48

1 Answers1

0

This solution works properly, as i wanted. In flirone driver i runned ./flirone and opened 1 process from subprocess.Popen and 2 additional processes in flirone code. I checked this running command sudo ./flirone ~/github/flirone-v4l2/palettes/Iron2.raw and ps aux | grep flirone . I used pidof flirone command to check main ID. When im killing this process everything works as should be and other processes ends too.

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")

    

When i deleted killing method from except KeyboardInterrupt processes ends too.

If you want to hide output of subprocess u can use:

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

More: How to hide output of subprocess in Python 2.7

Subprocess python 2.7 lib

Selenee
  • 9
  • 1