Question

Any idea on how to kill every python interpreter running on a GNU/Linux machine but the one which's launching the kill command (which is a python script itself)?

Was it helpful?

Solution

The first thing you need to do is find your current PID (Process ID):

import os
mypid = os.getpid()

Now using psutil, find every process running a python interpreter

import psutil
for process in psutil.process_iter():
    if process.name == 'python.exe' and process.pid != mypid:
        process.kill() # or send_signal(signal)

OTHER TIPS

in the killer script use os.getpid() to find the current pid

Then filter this out of the list of pids to be destroyed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top