According to How is the /tmp directory cleaned up?, the /tmp directory is cleaned up using tmpreaper, which uses cron to schedule cleanups at fixed time intervals. However, I would instead like to enforce a certain maximum size of the tmp directory. Is this possible?
Asked
Active
Viewed 141 times
1 Answers
1
You could write a little script:
#
# your maximum size of /tmp in kbytes
#
maxsize=1000
#
# now get the actual size of /tmp in kbytes
#
tmpsize=$(du -ks /tmp|cut -f 1)
#
# when maximum reached, clean up
#
if [ $tmpsize -ge $maxsize ]; then
rm -r /tmp/*
fi
This should be run as root, in order to clean up files owned by other users (including root) as well.
-
What if my program just generated the temporary file a few seconds ago and is about to open it for reading? Shouldn't there be a time test in your script? – WinEunuuchs2Unix May 29 '17 at 11:40
-
@WinEunuuchs2Unix You can remove only files older than x days using `find /tmp -mtime +x -delete`. See `man find` for more options. Of course, if there is a risk of deleting valuable data, you shouldn't run this script at all. – Jos May 29 '17 at 11:44