2

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?

1 Answer 1

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.

2
  • 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? May 29, 2017 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, 2017 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .