user3848207
user3848207

Reputation: 4987

How to kill this python process opened using subprocess.pOpen() in Windows?

The code below to kill a process opened using subprocess.Popen() works on Linux.

import os
import signal
import subprocess

# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before  exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                       shell=True, preexec_fn=os.setsid) 

os.killpg(os.getpgid(pro.pid), signal.SIGTERM)  # Send the signal to all the process groups

Unfortunately, it does not work on Windows. The process refused to be killed. How to modify the code such that the process can be killed on Windows?

I am using Windows 10, Python 3.8 on Anaconda.

Upvotes: 3

Views: 759

Answers (1)

Rakesh B E
Rakesh B E

Reputation: 892

USE subprocess.Popen.terminate() TO TERMINATE A SUBPROCESS

Upvotes: 1

Related Questions