30

I was wondering how long temp files created with mktemp last. It seems strange to me to rely on a file that could disappear at any time. I'm on ubuntu, if it matters.

3 Answers 3

24

Huh? From man mktemp:

DESCRIPTION
   Create  a  temporary  file  or directory, safely, and
   print its name.  TEMPLATE must  contain  at  least  3
   consecutive  `X's  in last component.  If TEMPLATE is
   not specified, use tmp.XXXXXXXXXX,  and  --tmpdir  is
   implied.   Files  are  created  u+rw, and directories
   u+rwx, minus umask restrictions.

mktemp just adds a random string to create a unique file name. Nobody is going to remove it automatically.


Since there seems to be a bit of an argument, lets go into some more detail. mktemp's man page states that

if TEMPLATE is not specified, --tmpdir is assumed, if --tmpdir is not specified, /tmp is assumed.

This means that simply running mktemp will create a file called tmp.RANDOM_STRING in the /tmp directory. So yes, running mktemp with no parameters will create a file that will be cleared along with everything else in /tmp in a system-dependent manner. For such files, the $TMPTIME variable will be important as explained in Chris's answer below. For many distributions, the default is clearing /tmp every boot because $TMPTIME is set to 0. In most (if not all) distributions, this is set in the file /etc/default/rcS:

$ grep TMPTIME /etc/default/rcS
TMPTIME=0

Now, if you do specify a TEMPLATE, for example mktemp fooXXX then a file called fooXXX is created in the current directory where XXX is replaced by three random characters. This file will never be deleted unless you specifically chose to do so. Only files in the "official" temporary directories such as /tmp are ever automatically deleted by the system, there is no magic bit you can set that specifies a temprary file, files outside /tmp and its ilk are not deleted automatically.

To answer your main question, of course it is strange to depend on a file that can disappear, that's why people do not depend on tmp files, they are created to temporarily hold data and we can then forget about them. There are many ways of creating a persistent, randomly named file. For example:

echo "foo" > $RANDOM.txt
echo "foo" > `mktemp fooXXX.txt`
echo "foo" > $$.txt
2
  • 3
    This doesn't answer my question. It says it makes a "temporary" file, and gives no information on how long it lasts. So it lasts forever? Does the tmp directory get perged on system boot?
    – MYV
    May 29, 2013 at 23:17
  • 2
    @Maksim see updated answer. Also, your question did not ask when the tmp directory is cleared but when files created with mktemp are deleted. As you can see, the two are not necessarily related :).
    – terdon
    May 30, 2013 at 14:12
8

Please remember that temp files might be removed during system bootup or as per TMPTIME. Please see what you have in /etc/cron.daily/tmpwatch.

/var/tmp/ on the other hand is immune usually to tmpwatch.

Temp files are created for (usually) short period of time. They have temp name so it's not easy to execute timing attack and usually they are deleted when not needed by program anymore. Usually your script would keep this file for very short period of time, usually processing some text or other files and it would remove it as soon as it's not needed. If your script would crash then you would have also fall-back on /tmp folder housekeeping procedures (if that's where you have decided to put them).

You don't have to create temp file in /tmp

4
  • Seeing as mktemp can be run in any directory and simply produces an arbitrary file name, I don't see how this has anything whatsoever to do with tmpwatch.
    – terdon
    May 29, 2013 at 21:53
  • 2
    By default mktemp creates files in /tmp/.
    – Chris
    May 30, 2013 at 7:34
  • only if you do not give a TEMPLATE, see my updated answer.
    – terdon
    May 30, 2013 at 14:13
  • Remove during system bootup.. That is so scary because I only reboot my server once a year ^_^
    – MaXi32
    Dec 12, 2022 at 17:36
2

Temp files do not go away on their own. They are called temp files simply because in your script or session or wherever you are making them, you are expected to delete them when you are done. Or leave them laying around if that's your thing. mktemp exists solely to allow you to come up with a file that is named something unique so that it doesn't overwrite some existing file.

3
  • Don't some systems use a TMPTIME?
    – demure
    May 29, 2013 at 20:39
  • @demure yes but only to clear /tmp or other tmp directories. It won't just randomly delete user-created files.
    – terdon
    May 29, 2013 at 21:12
  • By default mktemp creates files in /tmp/.
    – Chris
    May 30, 2013 at 13:23

You must log in to answer this question.

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