Does /tmp clear itself out?

dory36

Well-Known Member
Aug 30, 2003
179
0
166
A user's php script creates a 10k file in /tmp using tempnam(). His script fails if he issues the unlink command at the end of his script, as the process using the temp file seems to still be executing when the unlink command is executed.

My question . . . do I need to clean these files out periodically, or is /tmp purges automatically?
 

chirpy

Well-Known Member
Verifed Vendor
Jun 15, 2002
13,437
31
473
Go on, have a guess
Well, in the first instance, that's a programming problem that should be easily fixed by unlocking the resource before trying to delete it. You could also insist the user create temporary files within their account instead.

/tmp should be cleared down if you have tmpwatch enabled, usually via cron in:

/etc/cron.daily/tmpwatch

However, that only clears down to 720 hours, which is a long time so you might want to reduce that down to say, 48 if you want /tmp kept more tidy.
 

dory36

Well-Known Member
Aug 30, 2003
179
0
166
Thanks much. I'll work on the user.

In the meantime, is there any problem with deleting files in /tmp owned by "nobody" that are over 24 hours old?
 

dory36

Well-Known Member
Aug 30, 2003
179
0
166
Thanks. Looks like I'll have to rely on the php script deleting day-old stuff when it runs.

Surprisingly (to me at least) the tempnam() function works slightly differently than "advertised" on a Linux server. The php documentation says the function tempnam(dir,prefix) will create a temp file in dir -- but in Linux, it will only create it in /tmp, and anything in the function call to the contrary is ignored.