You are not logged in.

#1 2009-05-05 06:11:38

sjakub
Member
From: Canada/Poland
Registered: 2008-06-16
Posts: 84

rc.d script for clearing /tmp

Hi.

I haven't found "the right" method for clearing /tmp directory. I used to have it mounted as tmpfs for performance reasons, but when something was misbehaving and creating stupid and very large files there it would bring my system to its knees. So I decided to have it on my disk. But I like the fact that it is cleared when I turn off my system.
So I wrote a small rc.d script, that can be added to DAEMONS in rc.conf (at the very beginning, so it doesn't remove any files that might be created by other daemons).
/etc/rc.d/clear_tmp:

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

do_clear() {
  ret=0
  for f in /tmp/* /tmp/.*; do
    if [ ! "${f}" = "/tmp/." -a ! "${f}" = "/tmp/.." -a ! "${f}" = "/tmp/*" ]; then
      rm -r ${f}
      [ $? -gt 0 ] && ret=1
    fi
  done
  return $ret
}

case "$1" in
  start)
    stat_busy "Clearing /tmp directory"
    do_clear
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    add_daemon clear_tmp
    ;;
  stop)
    stat_busy "Clearing /tmp directory"
    do_clear
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    rm_daemon clear_tmp
    ;;
  *)
    echo "usage: $0 {start|stop}"
esac
exit 0

Maybe someone else would find it useful. Or maybe I am pointed in the right direction if it's wrong tongue

Offline

#2 2009-05-05 06:14:30

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,384
Website

Re: rc.d script for clearing /tmp

Ummm....  /tmp is cleaned on reboot anyway.

Offline

#3 2009-05-05 06:22:09

sjakub
Member
From: Canada/Poland
Registered: 2008-06-16
Posts: 84

Re: rc.d script for clearing /tmp

Ops. For some reason I had an impression it wasn't.
But still, it is done during booting the system, not turning it off wink

Offline

#4 2009-05-05 07:30:49

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,222
Website

Re: rc.d script for clearing /tmp

/etc/rc.sysinit clears it on boot

Adding `rm -Rf /tmp/*` to /etc/rc.shutdown would clear it on shutdown....

neutral

Offline

#5 2009-05-05 15:14:05

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: rc.d script for clearing /tmp

* Or use tmpfs wink

none                   /tmp          tmpfs     nodev,nosuid,mode=1777               0      0

Initially is created with size that is RAM/2
tmpfs is a clean solution, since uses the kernel internal cache, so all files inside are in ram or swap, also can change the size at runtime with a simple mount /tmp -o remount,size=N

* Or use "find -print0 | xargs -print0"

With find can delete files based on times for example, in any way /tmp should be clear at bootup. Different is the case of /var/tmp that must maintaint the content on reboots.

Offline

#6 2009-05-05 16:16:31

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: rc.d script for clearing /tmp

djgera wrote:

* Or use tmpfs wink

none                   /tmp          tmpfs     nodev,nosuid,mode=1777               0      0

Initially is created with size that is RAM/2
tmpfs is a clean solution, since uses the kernel internal cache, so all files inside are in ram or swap, also can change the size at runtime with a simple mount /tmp -o remount,size=N

* Or use "find -print0 | xargs -print0"

With find can delete files based on times for example, in any way /tmp should be clear at bootup. Different is the case of /var/tmp that must maintaint the content on reboots.

I used to have it mounted as tmpfs for performance reasons, but when something was misbehaving and creating stupid and very large files there it would bring my system to its knees. So I decided to have it on my disk.

Offline

#7 2009-05-05 16:30:44

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: rc.d script for clearing /tmp

Fyi, you can use ramfs instead of tmpfs. Ramfs will grow if needed, so there are no space constraints. It will just eat more RAM tongue.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#8 2009-05-05 16:37:44

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: rc.d script for clearing /tmp

Another alternative is to mount /tmp as an encrypted volume with a random key at each boot. If the key isn't stored anywhere, that data is practically unrecoverable once you shut off the system. As you seem concerned about removing the data at shutdown instead of letting the system clear /tmp at boot, it seems that you have sensitive data that you want to remove, so I would recommend this method.

Otherwise, consider using "wipe" or "shred" to make the data on /tmp more difficult to recover when you remove it.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2009-05-05 16:44:37

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: rc.d script for clearing /tmp

Themaister wrote:

I used to have it mounted as tmpfs for performance reasons, but when something was misbehaving and creating stupid and very large files there it would bring my system to its knees. So I decided to have it on my disk.

Ooh but... tmpfs can be tunned, see Documentation/filesystems/tmpfs.txt and adjusting swappiness values wink

Offline

#10 2009-05-06 00:23:33

sjakub
Member
From: Canada/Poland
Registered: 2008-06-16
Posts: 84

Re: rc.d script for clearing /tmp

Thanks for responses smile
I guess my script is not needed afterall ;(

Offline

Board footer

Powered by FluxBB