user2497792
user2497792

Reputation: 525

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)

Upvotes: 1

Views: 1828

Answers (2)

Ansuman Bebarta
Ansuman Bebarta

Reputation: 7256

you can use list comprehension

[os.kill(int(pid), signal.SIGTERM) for pid in pidlist]

also map will work

map(lambda x: os.kill(int(x), signal.SIGTERM), pidlist)

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250941

Simply iterate over the pidlist

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

Upvotes: 3

Related Questions