Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 827
user3848207 Avatar asked Jan 01 '26 11:01

user3848207


1 Answers

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

like image 67
Rakesh B E Avatar answered Jan 03 '26 01:01

Rakesh B E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!