mirror of https://github.com/flaggo/pydu.git
add process.get_processes_by_path
This commit is contained in:
parent
1d11f2aadc
commit
b1d27fd122
|
@ -0,0 +1,28 @@
|
|||
try:
|
||||
import psutil
|
||||
except ImportError:
|
||||
raise ImportError('Need to pip install psutil if you use pydu.process')
|
||||
|
||||
|
||||
def get_processes_by_path(path):
|
||||
pinfos = []
|
||||
for proc in psutil.process_iter():
|
||||
pinfo = proc.as_dict(attrs=['pid', 'name', 'exe', 'cwd', 'open_files'])
|
||||
|
||||
using_paths = []
|
||||
if pinfo['exe']:
|
||||
using_paths.append(pinfo['exe'])
|
||||
if pinfo['cwd']:
|
||||
using_paths.append(pinfo['cwd'])
|
||||
if pinfo['open_files']:
|
||||
using_paths.extend(pinfo['open_files'])
|
||||
|
||||
for using_path in using_paths:
|
||||
if path not in using_path:
|
||||
continue
|
||||
pinfos.append({
|
||||
'pid': pinfo['pid'],
|
||||
'name': pinfo['name'],
|
||||
'cmdline': pinfo['exe']
|
||||
})
|
||||
return pinfos
|
Loading…
Reference in New Issue