0

Is it possible to kill a process of another user with python by using:

import subprocess

def killProcess(pid):
    p = subprocess.Popen(['sudo','kill','-9',str(pid)], stdout=subprocess.PIPE)

Because if I execute this, nothing happens. If I execute sudo kill -9 pid in terminal no matter which user Iam logged in it workds. So I think there is something wrong with my Popen execution. I try to kill subprocesses spawned with pythons multiprocessing module. Each of those subprocesses creates tensorflow instances. When the main process has killed the subprocesses still blocking the GPUs memory and therefore has to be killed.

I also tried the psutil.Process(pid).terminate() approach. But then I get the error message:

AccessDenied: psutil.AccessDenied (pid=326080)

Anyone has an idea?

Best regards!

Varlor
  • 1,421
  • 3
  • 22
  • 46

1 Answers1

0

try using psutil,

for i in psutil.process_iter():
   if 'tensorflow' in i.name():
       i.kill()

or

[i.kill() for i in psutil.process_iter() if 'tensorflow' in i.name()]

Each process iter having their own .kill() attribute.

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • Nice approach but the problem still is that the processes are spawned by another user and I get AcessDenied Error – Varlor Jun 26 '19 at 13:44