How does one find large files that have been deleted but are still open in an application? How can one remove such a file, even though a process has it open?
The situation is that we are running a process that is filling up a log file at a terrific rate. I know the reason, and I can fix it. Until then, I would like to rm or empty the log file without shutting down the process.
Simply doing rm output.log
removes only references to the file, but it continues to occupy space on disk until the process is terminated. Worse: after rm
ing I now have no way to find where the file is or how big it is! Is there any way to find the file, and possibly empty it, even though it is still open in another process?
I specifically refer to Linux-based operating systems such as Debian or RHEL.
lsof -p <pid>
to list its open files and their sizes. The deleted file will have a(deleted)
next to it. The deleted file will be linked at/proc/<pid>/fd/1
probably. I don't know how to make a process stop writing to its file descriptor without terminating it. I would think that would depend on the process.rm
ed files that are still open?lsof | grep "(deleted)"
. When there are no more processes holding a deleted file open, the kernel will free up the inode and disk blocks. Processes do not have "handlers" by which they can be notified that an open, essentially locked file, have been removed from disk.lsof | grep '(deleted)'
works on Linux as well. On Linux, you can be notified of file deletion (even files that already don't have any entry in any directory other than /proc/some-pid/fd anymore) with the inotify mechanism (IN_DELETE_SELF event)somefile
and opened it in VIM, thenrm
ed it in another bash process. I then runlsof | grep somefile
and it is not in there, even though the file is open in VIM.