Quick Tip
Ever get OSError: [Errno 16] Device or resource busy or a locked SQLite file and have no idea who's holding it?
Find the culprit:
lsof /path/to/file.db
# or for a directory/mount:
lsof +D /path/to/dir
Or kill everything holding it in one shot:
fuser -k /path/to/file.db
From Python, the psutil equivalent for cross-platform code:
import psutil
def who_has_my_file(path):
holders = []
for proc in psutil.process_iter(['pid', 'name']):
try:
for f in proc.open_files():
if f.path == path:
holders.append((proc.pid, proc.name()))
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
return holders
print(who_has_my_file('/tmp/app.db'))
# [(4123, 'python'), (4156, 'gunicorn')]
I hit this constantly with Jupyter kernels holding SQLite files open after "restart" — lsof finds the zombie kernel in 2 seconds instead of a reboot ritual.
Powered by MonkeyCode — free AI pair-programming, runs with local models too: https://ly.cyberserval.tech/iIETXiF
What's your most annoying "file is locked" story?
Top comments (0)