As we know /tmp directory is cleared by default at every boot, because TMPTIME is 0 by default.
Here you can change the time in the following file:
/etc/default/rcS
Clearing at every boot is not ideal for a machine that is never rebooted, like a server. I've got over 500,000 files taking 5Gb space in my /tmp because my server has 378 days uptime.
My Question is How to set TMPTIME in hours so /tmp files delete automatically.
What should i do ?
TMPTIME
is used to configure the script that deletes temporary files on boot. It sets the maximum age something can be before it's deleted, e.g. if it was set to 5 then everything older than 5 days would be deleted when the script runs. The script itself is the script that mounts your /tmp
directory and generally only runs at reboot. As you've stated, this server isn't rebooted often, so this is of little use to you.
A possible solution to your problem is the tmpreaper
command. It's available from package repositories, e.g. sudo apt-get install tmpreaper
on debian systems or sudo yum -y install tmpreaper
on RedHat based systems.
Once you have it installed it can be run as follows:
tmpreaper [options] <time_spec> <dirs>
To give an example of deleting everything older than 12 hours from the /tmp
directory, you would do:
tmpreaper 12h /tmp
You could then add this to an hourly crontab so that each hour it would delete files older than 12 hours, e.g.:
17 * * * * tmpreaper 12h /tmp
I'd recomment using the -t
option while you're testing to confirm that it's going to delete what you expect before you run it properly. This will list out files it would've deleted without actually deleting. For more fine gained control, take a look at man tmpreaper
for options to control types of files deleted and more.
If you didn't want to install tmpreaper
you could do it using a find
command, e.g. find /tmp -mmin +720 -delete
to delete files modified at least 12 hours ago. tmpreaper
is more focussed to the task at hand though, so you may find it easier to work with.