I have an application that does a lot of forking, and every fork uses quite a lot of temp disk space. Currently, all of this space is used in /tmp. For various reasons, I want to clean up these tmp files asap after the process dies, preferably instantly.
The problem is that in many cases, the proc are terminated irregularly; hence the process itself should not be responsible for cleaning up its tmp files. What would be a nice way to set this up so that tmp files are removed when the process dies?
I was thinking about writing files to /tmp/myapp/$pid/ and then having a cron job every second or so that searches for directories /tmp/myapp/$pid/ and unlinks the ones for which there is no active process with that ID. But that might create a lot of overhead. Also for fun I tried if I could actually put the dir inside of the proc mkdir /proc/12345/tmp
but that doesn't fly either :-)
cron
look intoinotify
to see if you can monitor for the creation of/tmp/app/pid#
and then register a job to clean it up in a few minutes (hours, days, etc)