9

Ubuntu cleans /tmp on reboot. Suppose my system goes down unexpectedly, leaving some somewhat valuable files in /tmp. What can I do when I then restart the system to prevent the /tmp files from being deleted?

9
  • 2
    You realize that's like keeping "valuable files" in your trash can? Jul 27, 2015 at 20:26
  • Also, which Ubuntu version? The mechanisms vary. Jul 27, 2015 at 20:26
  • What? I keep all of my valuables in my trash can. Just kidding. I'm on Mint 17, so that would correspond to 14.04 Jul 27, 2015 at 20:36
  • 1
    Sometimes I like to try things out in /tmp and if it leads nowhere I just leave it there and not worry about it. If it turns out to be good, I move it to my home folder. I've never needed to dig out something out of the /tmp directory of a crashed system, but I think knowing what to do if such an emergency should arise might be valuable. Jul 27, 2015 at 20:45
  • 2
    For ubuntu, askubuntu.com/questions/20783/…. For other distributions, its possible that /tmp is mounted on tmpfs, which is RAM-based filesystem and thus got cleared automatically. You can run mount or df -h /tmp to check the filesystem of /tmp folder.
    – lex
    Jul 27, 2015 at 20:48

3 Answers 3

9

Credit to Chris.C and lesmana From How is the /tmp directory cleaned up?

On Ubuntu the cleanup of /tmp is done via the script at /etc/init/mounted-tmp.conf

This cleanup is performed on a file if TMPTIME number of days have elapsed since that file was created or modified. The value for TMPTIME is set in /etc/default/rcS

To stop the default cleanup behavior you can either increase TMPTIME to a sufficient value or comment out the final find lines in mounted-tmp.conf script that are responsible for the actual deletion.

Lastly, it may be that your /tmp filesystem is actually mounted as a tmpfs which generally means the filesystem would be mapped to RAM instead of your hard drive and would not persist after reboots.

This is not the default on Ubuntu, but you can check whether this is the case on your system by issuing a mount and looking at the listed filesystem for /tmp (if there is one listed).

To stop the creation of /tmp as a tmpfs, you can remove the entry for /tmp from /etc/fstab

5

@ssnobody answer solves the problem, but this isn't something you should do. Let /tmp and the initscripts do their work.

From Filesystem Hierarchy Standard 3.0 Chapter 3 Section 18 about /tmp directory:

3.18. /tmp : Temporary files

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. […]

Although data stored in /tmp may be deleted in a site-specific manner, it is recommended that files and directories located in /tmp be deleted whenever the system is booted.

FHS added this recommendation on the basis of historical precedent and common practice, but did not make it a requirement because system administration is not within the scope of this standard.

However, FHS defines another directory for this purpose. From Filesystem Hierarchy Standard 3.0 Chapter 5 Section 15 about /var/tmp directory:

5.15. /var/tmp : Temporary files preserved between system reboots

The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent than data in /tmp.

Files and directories located in /var/tmp must not be deleted when the system is booted. Although data stored in /var/tmp is typically deleted in a site-specific manner, it is recommended that deletions occur at a less frequent interval than /tmp.

This is the perfect place to put temporary files not so valuable but you still don't want to loose after an unexpected shutdown.

Anyway, working with valuable files inside temporary directory is always a bad idea. You should work in your home directory. By the way, you usually have a $HOME/tmp directory too which isn't emptied after a reboot that you can use for your personal temporary files.

2
  • This is not useful unless you tell where on Ubuntu you can set up the deletion policy for /var/tmp. Jul 27, 2015 at 23:48
  • 1
    systemd-tmpfiles take care of it in recent Ubuntu releases (and other systemd distribution). The default policy is in /usr/lib/tmpfiles.d/tmp.conf. On older Ubuntu releases, it's not cleaned by default. On older Red Hat-derived, it's a cron job calling tmpwatch which does the job.
    – piernov
    Jul 28, 2015 at 0:07
1

One option would be to hook up the hard drive to another computer and pull the files off out of the tmp folder with that computer... Or, if you have a dual-boot system, boot up into the other system, as long as that system supports the disk fornatting of your Ubuntu partition, and then pull down the files.

You must log in to answer this question.

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