I am working on some code which is the back-end for a tool. This tool creates all its output in a fixed directory. Now suppose at any time a client wants to kill all its applications, then all files should be deleted. So I use the system command (rm -rf
) to remove the directory, but files which are open are not deleted, and hence the directory is also not deleted. How can I do this smartly?
One option seems to be to use a table maintaining all the open files and closing them all before firing rm -rf
on the directory. However, this may slow down the whole application.
Another option I explored is using lsof
to find a list of open files and close them all before rm -rf
.
An additional constraint on my issue is that I have open several different files in a process, and one of them is in use by another process. Using the above methods, if I close all the file descriptors and try to delete the directory, logically it should not be deleted because there are still files opened by another process.
Any suggestions would be appreciated.