0

I'm running some processes from Python3 but they are not being terminated. This is a MCVE of the problem, running on Linux 16.04 with Python 3.5. I've tried this with QJoypad and with Sublime3.

import subprocess
import time

p_joypad = subprocess.Popen(['qjoypad'])   # also happens with sublime3
time.sleep(2)                              # wait until process starts

p_joypad.terminate()                       # try and kill it

p_joypad.kill()                            # try and kill it again
del p_joypad

os.kill(p_joypad.pid, signal.SIGTERM)      # ask the OS to kill it
os.kill(p_joypad.pid, signal.SIGKILL)

# another attempt at calling and killing the process
p_joypad = subprocess.Popen(['qjoypad'], preexec_fn=os.setsid)
os.killpg(os.getpgid(p_joypad.pid), signal.SIGTERM)

input("Process is still open, baby")       # hang up to verify this

Initially I had only the terminate() call, but then I tried the kill() as well as clearing the object from memory. However, both processes are still running after this. Qjoypad doesn't allow other instances to be called (so I need to close and restart my Python3 application), which is exceedingly problematic.

BlueMoon93
  • 2,910
  • 22
  • 39

1 Answers1

0

Okay, I found the issue. I had to set shell to True and kill the process group. From here.

p_joypad = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(p_joypad.pid), signal.SIGTERM)
BlueMoon93
  • 2,910
  • 22
  • 39