Skip to content

Instantly share code, notes, and snippets.

@papamoose
Forked from lebedov/lsof_funcs.py
Created May 7, 2018 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papamoose/1534a3ec99881d72d52f91f2d25f1b9a to your computer and use it in GitHub Desktop.
Save papamoose/1534a3ec99881d72d52f91f2d25f1b9a to your computer and use it in GitHub Desktop.
Python functions for finding open files and PIDs that have opened a file.
#!/usr/bin/env python
"""
Python functions for finding open files and PIDs that have opened a file.
"""
import numbers
import subprocess
try:
from subprocess import DEVNULL
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
def get_open_files(*pids):
"""
Find files opened by specified process ID(s).
Parameters
----------
pids : list of int
Process IDs.
Returns
-------
files : list of str
Open file names.
"""
for pid in pids:
if not isinstance(pid, numbers.Integral):
raise ValueError('invalid PID')
files = set()
for pid in pids:
try:
out = subprocess.check_output(['lsof', '-wXFn', '+p', str(pid)],
stderr=DEVNULL)
except:
pass
else:
lines = out.strip().split('\n')
for line in lines:
# Skip sockets, pipes, etc.:
if line.startswith('n') and line[1] == '/':
files.add(line[1:])
return list(files)
def get_pids_open(*files):
"""
Find processes with open handles for the specified file(s).
Parameters
----------
files : list of str
File paths.
Returns
-------
pids : list of int
Process IDs with open handles to the specified files.
"""
for f in files:
if not isinstance(f, basestring):
raise ValueError('invalid file name %s' % f)
pids = set()
try:
out = subprocess.check_output(['lsof', '+wt']+list(files),
stderr=DEVNULL)
except Exception as e:
out = str(e.output)
if not out.strip():
return []
lines = out.strip().split('\n')
for line in lines:
pids.add(int(line))
return list(pids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment