7

I often stick temporary files that I'm working on in /tmp, so I don't have to delete them once I'm done. However, if the computer crashes, then I'm out of luck, because that directory will be wiped.

Is there a way to prevent /tmp being wiped if the computer is restarting from a crash? If not, is there another solution, such as creating another temporary directory elsewhere and have it automatically deleted on restart if there is no crash.

2 Answers 2

11

/tmp is cleaned at boot by the Upstart script /etc/init/mounted-tmp.conf. If you look at that file, you see that there are no ways to tell it not to do his job. However you are free to modify it.

Here's how I'd proceed:

  1. At the very end of mounted-tmp.conf (just before end script), place the following:

     touch /tmp/.notclean
    

    This way, every time /tmp is mounted, a file .notclean will be created.

  2. After the following line (which is the line before the script starts removing files)...

     cd "${MOUNTPOINT}" || exit 1
    

    ...check for the existence of .notclean. If the file exists, it means that the computer did not shutdown cleanly.

     cd "${MOUNTPOINT}" || exit 1
     [ -f .notclean ] && exit 0
    
  3. Now you need a new Upstart script that removes .notclean on shutdown. Create /etc/init/mark-tmp-clean.conf and place this code:

     description "some useful description"
     start on starting rc
     task
     script
         rm -f /tmp/.notclean
     end script
    
5
  • 2
    Great answer! Exactly what I was asking for... Having said that, I'm a bit worried now I think about it a bit more. I never really reboot the computer unless it crashes (which happens every few days... when I try to hibernate). My request would mean that /tmp never gets cleaned. I've done a bit more research with the leads you've provided me, and I think that I can get a similar safety net by telling the system to clean /tmp only if files are older than a day. Apparently I can do this by editing /etc/default/rcS and changing $TMPTIME to 1. Thank you anyway!
    – Sparhawk
    Dec 29, 2012 at 9:46
  • 2
    For the sake of completeness, I might as well document a few other things here: one can clean out /tmp automatically (without booting) using tmpreaper. Another option would be to put the files into /var/tmp, which does not get wiped on boot, then clean it out regularly with tmpreaper.
    – Sparhawk
    Dec 29, 2012 at 9:53
  • You could also simply use a tmp directory in your home directory. Then you create a little bash script to clean it when you want.
    – don.joey
    Dec 29, 2012 at 10:10
  • @Private Good point. Perhaps I'm overthinking this...
    – Sparhawk
    Dec 29, 2012 at 11:50
  • I also use /tmp for Q&D files that I want culled at reboot. However, I find that Ubuntu very occasionally hangs coming out of suspend, and I want to keep these files, so I have added a loop in /etc/default/rcS to scan /proc/cmdline to look for a tmptime=N parameter and set TMPTIME accordingly. Now if I need to keep this work on boot, I simple set the boot option tmptime=2 say. OK, I still loose non-auotsaved stuff in RAM, but at least this problem is solved.
    – TerryE
    Oct 27, 2013 at 10:43
3

This really just a codicil to Andrea's post and my comment. Here is the code fragment that I've added after the TMPTIME=0 line in /etc/default/rcS. No other changes are needed.

# scan the boot cmdline for tmptime parameter and overide TMPTIME if it is set 
for opt in $(cat /proc/cmdline); do
    case $opt in
    tmptime=*)
        TMPTIME="${opt#tmptime=}"
        ;;
    esac
done

You must log in to answer this question.

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