Here is one crontab solution to garbage-collect those orphaned files:
0 * * * * find /tmp -maxdepth 1 -user username -atime +1 -and -not -exec /usr/sbin/fuser -s {} ';' -and -exec rm {} ';'
Every hour, at the hour, the command runs. find finds files that have not been used for at least an hour owned by the specified user (username), and checks using fuser whether each found file is being used by any process. If not, it is removed.
Why can't the user just use "-atime +1" without the "maxdepth 1 -user username"? Simply put, there are files and directories that don't belong to use and the user doesn't have permission to delete or even read/execute.
- The reason for "-user username" is to avoid deleting files belong to other users.
- If there is a directory doesn't belong to the user, but is executable by the user, find may explore it and later complain when trying to traverse subdirectories in it that are not executable, or delete files in there for which permissions are lacking. In any case, it's probably not a good idea for a user to delete someone else's files. The "-maxdepth 1" prevents this, though it also prevents the traversal of directories belonging to the user. If we only care about orphaned files directory contained in /tmp, this is an ok workaround.
NB: For more technical discussion perhaps leading to a more robust system-wide solution perhaps see this:
http://linuxgazette.net/18/tmp.html
No comments:
Post a Comment