views:

2010

answers:

8

I'm putting some files in /tmp on a web server that are being used by a web application for a limited amount of time. If the files get left in the server's /tmp after the user quits using the application and this happens repeatedly, should i be concerned about the directory filling up? I read online that rebooting cleans out the /tmp directory, but this box doesn't get rebooted very much.

Tom

+4  A: 

Yep It will be linked to one of your disks/partitions and can fill up. It gets deleted on a reboot.

When the user quits the application you should clean the files up after them.

Paul

Paul Whelan
This is configurable, and by no means true on all distros.
Adam Jaskiewicz
+10  A: 

Yes, it will fill up. Consider implementing a cron job that will delete old files after a while.

Something like this should do the trick:

/usr/bin/find /tmp/mydata -mtime +1 -exec rm -f {} \;

This will delete files that have a modification time that's more than a day old.

Or as a crontab entry:

   # run five minutes after midnight, every day
   5 0 * * *       /usr/bin/find /tmp/mydata -mtime +1 -exec rm -f {} \;

where /tmp/mydata is a subdirectory where your application stores its temporary files. (Simply deleting old files under /tmp would be a very bad idea, as someone else pointed out here.)

Look at the crontab and find man pages for the details. Don't go running scripts that delete files on your filesystem without understanding all the details - that's how bad things happen to good servers. :)

Of course, if you can just modify your application to delete temporary files when it's done with them, that would be a far better solution, generally.

Ori Pessach
I remember there were a bug where 'find' or 'rm' would follow symlinks...I'm not really sure. But it might be important to try before using it. Because if someone does something like ln -s / /tmp/ItWillEraseEverything . It might not be that funny if your cron job follow symlinks.
Sybiam
When cleaning up /tmp, be careful of security problems. In Debian and derivatives, tmpreaper should be useful.
Lars Wirzenius
+4  A: 

The only thing you can write to without worrying it will fill up is /dev/null. Everything else will eventually run out of space if you keep dumping things in it.

One simple approach would be to have a cron job clean up all your /tmp files that are older than, say, a few days.

Tiberiu Ana
A: 

Some linux distros have a package that will clean up old files in /tmp for you. It isn't hard to implement your own, as mentioned above. One thing to look out for are long running processes, especially "zombies", which are ones that have died but which haven't finished cleaning up after themselves. If a process has a file open, just deleting it from /tmp won't actually reclaim its space - you have to kill the process or somehow coerce it to close the file. Many programs that write log or temporary files are designed to catch a signal (often SIGUSR1) and close and re-open any log or temporary files for that reason.

Paul Tomblin
A: 

Many Linux distributions include something named 'tmpwatch', or similar which runs via cron and deletes things on a pre-defined gradient. Some are smart enough to go by the owner of the file .. stuff that is owned by daemon users gets cleaned out faster than stuff owned by regular users. Check on the mailing lists for your distro of choice to find out.

Still, you should have SNMP or some other kind of monitor watching how much room is available, if it fills up services like Apache aren't going to be happy. For instance, e-accelerator for PHP will need plenty of room, some mail scanners don't clean up properly, etc.

Tim Post
I thing a combination of cron and tmpwatch is a good solution to this problem. Thanks
Thomas Kessler
+1  A: 

In which language is your web-application? A lot of languages propose temp files:

Search in your language if there is such a feature.

Jérôme
Maybe I'm missing something here, but wasn't the question about deleting temp files, not creating them??
Thomas Padron-McCarthy
+1  A: 

You can't just blindly delete everything that hasn't been modified for a certain amount of time. A lot of programs store sockets in there, which never get modified but are still an integral part of the program working. Take for instance mysql from one of my servers:

srwxrwxrwx    1 mysql    mysql           0 Sep 11 04:01 mysql.sock=

That's a valid, working "file" in /tmp. It just looks old because mysql hasn't been restarted in a while. Either limit your find with '-type f' or use one of the distro-provided tools others have mentioned.

jj33
A: 

Just a warning: not all Linux installation clean the /tmp directory after each reboot

Julien