1

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)

user2497792
  • 505
  • 2
  • 9
  • 18

2 Answers2

3

Simply iterate over the pidlist

for proc_id in pidlist:
   os.kill(int(proc_id), signal.SIGTERM)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • worked like a charm, I am still new to python so being able to declare a variable on the fly `proc_id` isn't second nature to me yet – user2497792 Jul 01 '13 at 19:45
0

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)
Ansuman Bebarta
  • 7,011
  • 6
  • 27
  • 44