3

I'm trying to get a substring from a string, it has to be specific though,
if you use this function and print it you'd get a long string with all the currently unresponsive processes and their infos, I need the PID from a specific process.

r = os.popen('tasklist /FI "STATUS eq Not Responding"').read().strip()  
print r 

example if chrome.exe is not responding it would be in the list and I would like to get the PID associated with it. I tried split() and pop() to single out what I needed without success.

Edit :
I have 10+ processes that all share the same application name. It's necessary for me to use PID and that it belongs to the correct application. I don't want my script to kill everything either.

So in short I need to find the PID that is on the same row as the specified 'process_name' and then only keep that 'PID'

Hope that makes sense.

  • Please post the "long string with all the currently unresponsive processes and their infos" and show us what you need from that string – inspectorG4dget Feb 20 '14 at 05:36
  • Does this help? http://stackoverflow.com/questions/13507902/how-to-extract-a-specific-field-from-output-of-tasklist-on-the-windows-command-l – Jayanth Koushik Feb 20 '14 at 05:36

3 Answers3

5

You can simplify your life by having tasklist return to you the list as a CSV:

C:\>tasklist /FI "STATUS eq Not Responding" /FO CSV /NH
"jusched.exe","3596","Console","1","13,352 K"
"chrome.exe","4760","Console","1","181,088 K"
"chrome.exe","3456","Console","1","119,044 K"
"chrome.exe","2432","Console","1","24,236 K"
"chrome.exe","440","Console","1","36,420 K"
"chrome.exe","4964","Console","1","60,596 K"
"chrome.exe","3608","Console","1","21,924 K"
"chrome.exe","4996","Console","1","22,348 K"
"chrome.exe","2580","Console","1","38,432 K"
"chrome.exe","3312","Console","1","32,756 K"
"chrome.exe","4600","Console","1","36,072 K"
"chrome.exe","4180","Console","1","24,436 K"
"chrome.exe","4320","Console","1","31,152 K"
"chrome.exe","4120","Console","1","22,632 K"

Exploiting this with the csv module, you now have:

>>> r = os.popen('tasklist /FI "STATUS eq Not Responding" /FO CSV')
>>> import csv
>>> reader = csv.DictReader(r, delimiter=',')
>>> rows = list(reader)
>>> rows[0]
{'Session Name': 'Console', 'Mem Usage': '13,352 K', 'PID': '3596', 'Image Name'
: 'jusched.exe', 'Session#': '1'}
>>> rows[0]['PID']
'3596'

I got rid of the /NH switch (No Header) to get dictionaries from the csv module.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Works well, however, I can't seem to make it automatically get me the PID from the process name I want it to. I have to specify the # of the row. Lets say process on row 7 is named fun.exe and I want only the PID associated with 'fun.exe' how could that be done? – user3331130 Feb 20 '14 at 22:22
0

I reckon there is a method to get the process pid only, but if you want to know how to extract a specific substring from a string use regex

The regex pattern to match the pid : \w+\.\w+\s+(\d+)\s

import os
import re

r = os.popen('tasklist /FI "STATUS eq Not Responding"').read().strip()
print re.findall('\w+\.\w+\s+(\d+)\s',r)

Output:

['4024']
K DawG
  • 13,287
  • 9
  • 35
  • 66
0

Based on How to extract a specific field from output of tasklist on the windows command line:

print os.popen('for /f "tokens=2 delims=," %F in (\'tasklist /nh /fi "STATUS eq Not Responding" /fo csv\') do @echo %~F').read().strip()
Community
  • 1
  • 1
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52