1

To the best of my knowledge /tmp is cleared on every boot, since it normally uses tmpfs, which basically is RAM.

During development of ML applications I often need a tmp directory for debugging and other stuff that does not fill up my RAM, but is stored on hard disk. At the moment my scripts automatically clear these directories, but this is not the best solution: sometimes I forget to invoke a cleanup, sometimes the script cleans up too early, and so on...

Is there already some tmp directory that gets cleaned on every bootup on hard disk? If not, what would be the easiest way to create such an directory and ensure it is cleaned on every boot?

EDIT: I am using Manjaro (Arch Linux) on kernel 4.14 with i3wm.

2
  • Is this really a solution considering that the performance of /tmp then will be a lot slower, right? I mean this way I would change the behavior for every application that uses /tmp, not just for my development process?! – daniel451 Mar 3 '18 at 23:42
  • @RuiFRibeiro so /var/tmp is also cleared on every boot and there is no problem when users write tmp files in there? (note: I have added distribution information now) – daniel451 Mar 4 '18 at 0:08
3

You can add an entry in a configuration file under /etc/tmpfiles.d (as shown in the Arch Wiki):

/etc/tmpfiles.d/tmp.conf (from the Wiki)

# see tmpfiles.d(5)
# always enable /tmp folder cleaning
D! /tmp 1777 root root 0

# remove files in /var/tmp older than 10 days
D /var/tmp 1777 root root 10d

# namespace mountpoints (PrivateTmp=yes) are excluded from removal
x /tmp/systemd-private-*
x /var/tmp/systemd-private-*
X /tmp/systemd-private-*/tmp
X /var/tmp/systemd-private-*/tmp

The file from the wiki assumes that you've disabled /tmp being mounted as tmpfs and want to clear out /var/tmp on an occasional basis. Depending on your needs, you can change these to match your requirements:

/etc/tmpfiles/vartmp.conf

# This may be all that you require if you're not changing anything else
D /path/to/my/tmp - app_owner app_group 0

Take a look at the tmpfiles.d (5) documentation for more information.

2
  • Thank you! Instead of /etc/tmpfiles.d/ (I read that this should mainly be used for system administration reasons, since it overwrites .conf files from "lower levels" with the same name) I created my.conf in /usr/lib/tmpfiles.d/. After I finished I tried systemd-tmpfiles --create in order to check if everything is correct and indeed, it automatically created my new tmp directory with the correct permissions. Looks like a good & clean solution! :) – daniel451 Mar 4 '18 at 1:39
  • 2
    No, /etc/tmpfiles.d is where you should put it. Anything in /usr/lib and /lib should be reserved for distribution-related files. /etc/ also is looked at last, so it overrides all the other locations, which is generally what you want for global stuff. If it's per-user, you can put it in ~/.config/user-tmpfiles.d/*.conf or ~/.local/share/user-tmpfiles.d/*.conf. – ErikF Mar 4 '18 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.