How to cleanup tmp folder safely on Linux

Posted by Syncopated on Super User See other posts from Super User or by Syncopated
Published on 2012-11-03T16:22:18Z Indexed on 2012/11/03 17:07 UTC
Read the original article Hit count: 179

Filed under:
|
|

I use RAM for my tmpfs /tmp, 2GB, to be exact. Normally, this is enough but sometimes, processes create files in there and fail to cleanup after themselves. This can happen if they crash. I need to delete these orphaned tmp files or else future process will run out of space on /tmp.

How can I safely garbage collect /tmp? Some people do it by checking last modification timestamp, but this approach is unsafe because there can be long-running processes that still need those files. A safer approach is to combine the last modification timestamp condition with the condition that no process has a file handle for the file. Is there a program/script/etc that embodies this approach or some other approach that is also safe?

Incidentally, does Linux/Unix allow a mode of file opening with creation wherein the created file is deleted when the creating process terminates, even if it's from a crash?

© Super User or respective owner

Related posts about linux

Related posts about filesystems