How long do temporary files made with mktemp last?

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.


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

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


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.