-1

I want to terminate a subprocess, but it doesn't work. Here's my code:

import os
import subprocess

p = subprocess.Popen(['gnome-terminal','-e','tshark -i eth0 -w /root/Desktop/test.pcap'])
q = subprocess.Popen(['python','avtp2.py'])

if q.wait() == 0:
  p.terminate()

Do you know why?

Greetz

crappidy
  • 377
  • 1
  • 5
  • 16

2 Answers2

2

From the documentation:

Popen.wait()

Wait for child process to terminate. Set and return returncode attribute.

So for the body of the if to run, the process has to first terminate, you can use q.poll() instead, since it doesn't blocks.

Community
  • 1
  • 1
Francisco
  • 10,918
  • 6
  • 34
  • 45
0

This works:

import os
import subprocess
from subprocess import check_output
import signal

p = subprocess.Popen(['gnome-terminal','-e','tshark -i eth0 -w /root/Desktop/test.pcap'],shell=False)
q = subprocess.Popen(['python','avtp2.py'])

if q.wait() == 0:
  p_pid = check_output(["pidof","tshark"]).split()
  os.kill(int(p_pid[0]), signal.SIGINT)
crappidy
  • 377
  • 1
  • 5
  • 16