WSL: /tmp never cleared
Ubuntu deletes all files in /tmp when booting. Other distributions (eg. RedHat) have a cronjob tmpwatch that deletes these files or even use tmpfs to mount /tmp.
WSL does not ‘boot’ nor has anacron running. The Windows Disk cleanup program does not consider the files in %USERPROFILE%/AppData/Local/lxss/rootfs/tmp. Hence these are never deleted (automatically).
My suggestion is to clean /tmp when the last lxss process closes. It already terminates background processes when there is no more bash session. At that point no process can access files in /tmp anymore and by Filesystem Hierarchy Standard files cannot be expected to be kept.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 24
- Comments: 35 (5 by maintainers)
FYI if you enable the new systemd support in WSL2, afterwards you can enable
tmp.mountto get a proper tmpfs /tmp mount when WSL initializes, like so:sudo systemctl enable /usr/share/systemd/tmp.mountAfter the above and restarting WSL, you can check if it worked via
mount | grep /tmpand you should see a line liketmpfs on /tmp type tmpfs (rw,nosuid,nodev).You can obviously confirm further by “touching” a file (ie
touch /tmp/potato) then exit WSL (wsl --shutdown) and then go back in a wsl shell and the file should be gone (iels /tmp/potatoshould fail.)Knowledge credit: https://github.com/microsoft/WSL/issues/6999#issuecomment-1316014788
@therealkenc #344 did not address this issue on ubuntu, the /tmp directory still grows indefinitely on a default ubuntu wsl instance.
Sorry, why was this issue closed? I’m not really sure the problem is solved here…
One potential caveat: doing this replaces your
/tmpmount, it doesn’t clear data in your existing (old) mount.In my case, I had to disable
/usr/share/systemd/tmp.mount, purge the old data, then re-enable it.I second this. /tmp is not cleaned on Ubuntu 22.04
People who feel strongly about it can start
cronin their.bashrc, guarding to check if it is running already. As withsshd,rsyslogd, and whatever their fancy. There is no blocking surface fortmpreaper(or, call me surprised if there is).I keep meaning to make a run at
systemdlike I did with Chrome but just never get around to it because like you implied it just isn’t that blocking. Well that and becausesystemdis broken by design and making it work feels like enabling (in the mental health sense).The ask in the original post here was that WSL
/tmpis not being scrubbed, but should be, like Real Ubuntu. The OP then went and made the terribly unfortunate mistake of using the words “Windows Disk cleanup” and mentioning win32 paths. All hope was pretty much lost at that point.On recent versions of Ububtu
/tmpis cleaned bytmpreaper, which of course does not run on WSL because nocron. More here.This is a dupe of #511, or, alternately, a feature request (with the ‘init’ label) if one subscribes to the idea that WSL
initshould do somesystemdthings like #2530.Absolutely.
It should be made as general as possible though, to address Rich’s tough questions. It should probably be able to run arbitrary scripts, be it
tmpreaperortmpwatch(which are the same thing forked over a spat nearly 20 years ago), or any other script for that matter. Maybe there could be some kind of flexible syntax in/etc/wsl.conffor scheduling when the scripts are run. Users should probably be able to set the year, the month of the year, day of the week, hour, minute. Perhaps there could be some kind of special character, asterisk maybe, to indicating running the script every week/day/hour/etc instead of a specified one. There could also be a different character, question mark maybe, to indicate running it only at startup.Something like that.
There, you used the words again. Can’t say I didn’t try to help you out…
@fpqc FYI –
/tmpis not in-RAM on my Ubuntu 16.04 VM. It’s not mounted at all; it’s just a directory under/, which is a regular ext4 filesystem.