Are we supposed to manually delete the contents of /tmp?
To answer the questions:
- Is
/tmp
supposed to be emptied automatically: Yes - Are we supposed to delete Files in
/tmp
regularly and manually: No, your system will take care of them.
If you may ask yourself:
- Can I delete files from
/tmp
for whatever reason (need space, want to remove traces, etc.): It depends, read on.
Filesystem Hierarchy Standard (FHS) states:
The /tmp directory must be made available for programs that require temporary files.
Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.
/var/tmp/
has a similar purpose, but must not be deleted during reboot.
It is not guaranteed that /tmp/
or /var/tmp/
are cleaned up on a regular basis. This may depend on your distribution and settings, although most systems do some cleanup from time to time. See comment by mike
.
If you need to to delete a file in /tmp, see first if the file is in use. You can do this easily with:
lsof /tmp/file_to_delete
If you have rights to do so, this will show the process holding the handle to that file, like process name, PID and type of the file. To show really all processes, prepend sudo
or run as user root.
lsof +D /tmp
will show you all files in /tmp
and directories below (+D
) that are currently open.
Of course you should not delete these files.
In fact when you delete a file that is still opened - if you have the rights to do so - it becomes inaccessible from the filesystem namespace, but it still exists for the processes that have an open file handle for it. After closing that handle, the file is not accessible for that process any more, and if no process has the file opened any more it is finally deleted. A process should not suppose that the file survives between subsequent open
calls, but programmers are sloppy, and you never know. For that reason it's not that clever to delete files that are still in use by some programs.
I think this is OS varient dependent. I'd imagine that /tmp is typically cleared on reboot, and indeed it would not be safe for the system to clean itself up mid session as it won't know what files are active.
If you are brave you might want to throw a command into crontab which deletes files older then a certain age, but this might cause some issues if it deletes files still used. You might try a command (I have not tried it) like
find /tmp -type f -ctime +10 -exec rm {} +
Which will theoretically remove all files under /tmp older then 10 days.
The /tmp and /var/tmp directories are cleaned on a normal schedule. This may depend on your distro. On my CentOS system (a clone of RedHat) there is a cron job scheduled to run tmpwatch, a tmp dir cleaner, on a daily schedule. Files in /var/tmp are allowed to stick around a little longer than files in /tmp/. I've also seen scripts that prune /tmp (but explicitly not /var/tmp) on a reboot, knowing that there can be nothing holding that file open since all the processes are new.
So, yes, /tmp has maintenance from basic scripts. It still can fill up outside of those maintenance times. If you chose to clean things manually, best sysadmin practice is to be careful. Sysadmin lore talks about symlinks in /tmp pointing to necessary system files that were deleted when n00b sysadmins ran a simple find
script.