Can I prevent /tmp being deleted if the computer crashes?
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.
Solution 1:
/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:
-
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. -
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
-
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
Solution 2:
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