11

This question is about Ubuntu 14.10 on my developer laptop.

I've got a folder in /tmp that is used by an application to put temporary stuff in there. This application usually makes a temporary folder in my homedir and deletes it afterwards. For some reason that doesn't work when the homedir is encrypted. So instead, I made a symlink to /tmp/foo inside my homedir. My application can write there and make it's temporary subfolder.

Now /tmp/foo gets deleted every time I reboot my machine. Until now I've just recreated the folder manually after reboot. Now I learned in How is the /tmp directory cleaned up? that there is a job doing that.

I've looked at /etc/init/mounted-tmp.conf but my bashfu and especially my findfu are not sufficient to do what I want. Here's an excerpt from that file:

   EXCEPT='! -name .
            ! ( -path ./lost+found -uid 0 )
            ! ( -path ./quota.user -uid 0 )
            ! ( -path ./aquota.user -uid 0 )
            ! ( -path ./quota.group -uid 0 )
            ! ( -path ./aquota.group -uid 0 )
            ! ( -path ./.journal -uid 0 )
            ! ( -path ./.clean -uid 0 )
            ! ( -path "./...security*" -uid 0 )'

   # Remove all old files, then all empty directories
   find . -depth -xdev $TEXPR $EXCEPT ! -type d -delete
   find . -depth -xdev $DEXPR $EXCEPT -type d -empty -delete

What I want to do is add a condition that makes it delete everyting inside /tmp/foo, but not /tmp/foo itself. How do I do that?

  • Is /tmp/foo a directory or a file? – terdon Jun 12 '15 at 14:38
  • A directory. There are files inside. – simbabque Jun 12 '15 at 14:42
  • Why not add mkdir /tmp/foo right before end script? – Hagen von Eitzen Jun 12 '15 at 16:07
  • Just FYI (you may already know this): 14.10 reaches EOL in July 2015, so you should really upgrade to Vivid as soon as possible. – Léo Lam Jun 12 '15 at 18:41
20

/etc/init/mounted-tmp.conf is part of the mountall package, so any updates on that package and the suggested changes will be reverted.

$ sudo dpkg -S /etc/init/mounted-tmp.conf 
mountall: /etc/init/mounted-tmp.conf

Instead, according to the Filesystem Hierachy standard (FHS);

Regarding /tmp:

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

Regarding /var/tmp:

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.

So you should change your symbolic link to use /var/tmp instead of /tmp.

  • 3
    Classic example of when you have a hammer, everything looks like a nail. – WernerCD Jun 12 '15 at 18:26
  • @WernerCD could you elaborate what you mean by that? Are you saying that /var/tmp is not a good idea? – simbabque Jun 12 '15 at 18:55
  • 6
    When all you have is a hammer... - If a person is familiar with ... a certain, single instrument, they may have a confirmation bias to believe that it is the answer to/involved in everything. I'm saying that the OP is using /tmp for something it's not meant to be used for - Using /tmp as a hammer to his problem of keeping tmp-but-not-quite-tmp stuff. /var/tmp is MEANT to be kept between reboots, so its the right answer. – WernerCD Jun 12 '15 at 19:17
8

Not strictly an answer to your question, but you might find /var/tmp to be a more suitable location, as it doesn't get cleaned up over a reboot. It's designed for temporary files that should not be automatically discarded after a short time.

What I often do, however, is create myself a folder under /opt to store random things I don't want in home. That's a suitable place to put things that are outside the main OS's control.

  • This is the right idea. If /tmp doesn't do what you want, then store your info somewhere else - even on another drive if there's space available. Don't mess with things that Linux (and other programmers) expects to be a certain way unless it's the only way to get the result you need. – Joe Jun 18 '15 at 1:38
6

Like so:

EXCEPT='! -name .
            ...
            ! ( -path "./foo" )'

   # Remove all old files, then all empty directories
   ...
   find /tmp/foo/* -depth -xdev $TEXPR -delete

Example:

$ cd /tmp/foo/
$ mkdir 1 2 3
$ touch 3 4 5
$ find /tmp/foo/* -depth -xdev $TEXPR -delete
$ ls /tmp/foo/
$

I do agree with user aap: You either should take care of this in the software used by re-creating the directory there if they are tmp files or use another directory if they are not tmp files that is not purged.

  • This doesn't actually work. While the find command works as expected when run manually, the directory is still removed after editing /etc/init/mounted-tmp.conf. It seems like there's something else going on here. – terdon Jun 12 '15 at 16:37
  • wth. ehm. that should mean that the 1st line is faulty. maybe it needs to be relative(?) – Rinzwind Jun 12 '15 at 20:50
  • @terdon yes seems it needs to be relative to /tmp/ – Rinzwind Jun 12 '15 at 21:07
  • I don't think so, I tried various permutations, including -path ./foo and even -name '*foo*' and none of them worked (on Ubuntu Server 14.04). I think that's just not the right file and we're looking in the wrong place. What you're suggesting should work, it just doesn't seem to. – terdon Jun 12 '15 at 22:30
0

It is a bad idea to have files with future value, write to the /tmp directory (/var/tmp, as suggested by others, is a better place). That said, you might want to give a shot at chattr. This should be run after the application exits, but before a shutdown. Remember that this operation won't let anything write to that directory henceforth.

touch /tmp/foo/ddmmyy/.001_immute_me chattr +i /tmp/foo/ddmmyy/.001_immute_me

  • How would that be useful if I want to have a temporary directory? – simbabque Jun 19 '15 at 7:04
-1

Run this:

echo "if [ ! -e /etc/foo/ ]; then mkdir /etc/foo/; fi; rm -rf /etc/foo/*" > /bin/foodel; sudo chmod 755 /bin/foodel 

then Add this to /etc/inittab see here: where is inittab file?:

foo:2345:boot:/bin/foodel

That will create /etc/foo on boot if it doesn't exist, and then empty it.

This will do the same on bash login:

echo "/bin/foodel" >> ~/.bashrc
  • Ubuntu version 14 doesn't use /etc/inittab. This is clearly and unequivocally stated on its manual page. – JdeBP Jun 13 '15 at 15:30
  • *facepalm* Sorry @JdeBP, this is what happens to me after juggling 4 different versions of linux. He can put the code somewhere stated askubuntu.com/questions/34308/where-is-inittab-file – Daniel Jun 13 '15 at 20:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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