Search code examples
pythonlistkillpid

Kill all processes using a list of pid's in python?


I have a list of strings which contains the pid's to certain processes which must be killed at a certain time.

pidList = ['1234', '1235', '1236']

How would one run through the list first casting all elements to int, then killing the process represented by that pid?

essentially doing this:

os.kill(int(pidList[0]), signal.SIGTERM)

os.kill(int(pidList[1]), signal.SIGTERM)

os.kill(int(pidList[2]), signal.SIGTERM)


Solution

  • Simply iterate over the pidlist

    for proc_id in pidlist:
       os.kill(int(proc_id), signal.SIGTERM)