-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfindprocess.py
More file actions
32 lines (25 loc) · 856 Bytes
/
findprocess.py
File metadata and controls
32 lines (25 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def pids(processname, entireline=True):
"""
Find the pid(s) of a running process, returned as a list.
Code from: http://www.echarcha.com/forum/archive/index.php/t-26378.html
enterline means search the entire 'ps ax' line, not just the command.
"""
import os
import signal
pids = []
for line in os.popen('ps xa'):
try:
fields = line.split()
pid = fields[0]
process = fields[4]
if entireline: searchstr = line
else: searchstr = process
# if searchstr.find(processname) > 0:
if searchstr.find(processname) >= 0:
# print line
pids.append(int(pid))
except Exception, e:
print >> sys.stderr, type(e), e
return pids
if __name__ == "__main__":
print pids("firefox")