Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to enable Debian to automatically clear the tmp directory

1,013 views
Skip to first unread message

Ryan Klein

unread,
Aug 28, 2008, 12:10:14 PM8/28/08
to
I have been searching for a way to have Debian automatically remove
files in the /tmp directory after a period of time. We need it mainly
for removing old session files that grow quite rapidly (a rate of 10 per
second). I am hoping Debian has a built in utility, otherwise a package
will suffice if not a simple bash script. Thanks for your input.

--
Regards,

Ryan Klein


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org

Richard Möhn

unread,
Aug 28, 2008, 12:30:18 PM8/28/08
to
On Thu, Aug 28, 2008 at 10:39:05AM -0500, Ryan Klein wrote:
> I have been searching for a way to have Debian automatically remove
> files in the /tmp directory after a period of time. We need it mainly
> for removing old session files that grow quite rapidly (a rate of 10 per
> second). I am hoping Debian has a built in utility, otherwise a package
> will suffice if not a simple bash script. Thanks for your input.
>

It seems to be possible to do this with a cronjob.

With the best greetings

Richard

--
Richard Möhn, Laußnitz in Sachsen
Public Key-ID: 4385C7FB
Fingerprint: D74B F0D6 52BD C802 F5E2 B5BF 78AB 5563 4385 C7FB

signature.asc

Stackpole, Chris

unread,
Aug 28, 2008, 1:10:14 PM8/28/08
to
>From: Richard Möhn [mailto:richar...@gmx.de]
>Subject: Re: How to enable Debian to automatically clear the tmp directory

>
>On Thu, Aug 28, 2008 at 10:39:05AM -0500, Ryan Klein wrote:
>> I have been searching for a way to have Debian automatically remove
>> files in the /tmp directory after a period of time. We need it mainly
>> for removing old session files that grow quite rapidly (a rate of 10 per
>> second). I am hoping Debian has a built in utility, otherwise a package
>> will suffice if not a simple bash script. Thanks for your input.
>>
>
>It seems to be possible to do this with a cronjob.
>
>With the best greetings
>
>Richard

I second the cronjob. Just have something along the lines of:
find /tmp -type f -ctime +1

Obviously, you will need to set ctime to be the number you need it to be. Once you know that the files it returns are indeed the ones you want to delete then add "-exec rm -rf {} \;" to the end and toss it into cron.

If someone has a better way, please comment.

Have fun!
~S~

nate

unread,
Aug 28, 2008, 1:30:12 PM8/28/08
to
Stackpole, Chris wrote:

> If someone has a better way, please comment.

Maybe better, maybe not, I haven't used it, but

Package: tmpreaper
[..]
Description: cleans up files in directories based on their age
This package provides a program that can be used to clean out temporary-file
directories. It recursively searches the directory, refusing to chdir()
across symlinks, and removes files that haven't been accessed in a
user-specified amount of time. You can specify a set of files to protect
from deletion with a shell pattern. It will not remove files owned by the
process EUID that have the `w' bit clear, unless you ask it to, much like
`rm -f'. `tmpreaper' will not remove symlinks, sockets, fifos, or special
files unless given a command line option enabling it to.
.
WARNING: Please do not run `tmpreaper' on `/'. There are no protections
against this written into the program, as that would prevent it from
functioning the way you'd expect it to in a `chroot(8)' environment.
.
The daily tmpreaper run can be configured through /etc/tmpreaper.conf .
Tag: admin::automation, interface::daemon, role::program, scope::utility,
works-with::file

--

nate

Steve Lamb

unread,
Aug 28, 2008, 1:40:06 PM8/28/08
to
On Thu, Aug 28, 2008 at 12:05:08PM -0500, Stackpole, Chris wrote:
> I second the cronjob. Just have something along the lines of:
> find /tmp -type f -ctime +1

mtime or atime. ctime has no bearing on whether the files are still in
use. mtime is not perfect but will show if a file has been update recently.
Recently modified files would be files in use regardless of their age since
creation. atime would be best since it shows when the file was last accessed.
One could surmise that access files are in use. However atime can be turned
off on a per-filesystem basis and, to be honest, I am a tad fuzzy if a stat()
call constitutes an access. I don't believe it is but it would be humorous if
it were. ;)

--
Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
PGP Key: 1FC01004 | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------

signature.asc

John Hasler

unread,
Aug 28, 2008, 2:10:10 PM8/28/08
to
Package: tmpreaper
Priority: optional
Section: admin
Installed-Size: 108
Maintainer: Paul Slootman <pa...@debian.org>
Architecture: amd64
Version: 1.6.10
Replaces: tmpwatch
Depends: debconf (>= 0.5.0) | debconf-2.0, libc6 (>= 2.7-1)
Conflicts: tmpwatch
Filename: pool/main/t/tmpreaper/tmpreaper_1.6.10_amd64.deb
Size: 45346
MD5sum: aed6f74d97b0c76736054c0501179599
SHA1: 06a0f172078516577a9daa218054e2a6d3ee4b53
SHA256: 921ad12f76a64502facfe064822573393b3d439f587c741d35f825923a0e955d

Description: cleans up files in directories based on their age
This package provides a program that can be used to clean out temporary-file
directories. It recursively searches the directory, refusing to chdir()
across symlinks, and removes files that haven't been accessed in a
user-specified amount of time. You can specify a set of files to protect
from deletion with a shell pattern. It will not remove files owned by the
process EUID that have the `w' bit clear, unless you ask it to, much like
`rm -f'. `tmpreaper' will not remove symlinks, sockets, fifos, or special
files unless given a command line option enabling it to.
.
WARNING: Please do not run `tmpreaper' on `/'. There are no protections
against this written into the program, as that would prevent it from
functioning the way you'd expect it to in a `chroot(8)' environment.
.
The daily tmpreaper run can be configured through /etc/tmpreaper.conf .
Tag: admin::automation, interface::daemon, role::program, scope::utility, works-with::file

--
John Hasler

Andrew Reid

unread,
Aug 28, 2008, 8:50:06 PM8/28/08
to
On Thursday 28 August 2008 11:39, Ryan Klein wrote:
> I have been searching for a way to have Debian automatically remove
> files in the /tmp directory after a period of time. We need it mainly
> for removing old session files that grow quite rapidly (a rate of 10 per
> second). I am hoping Debian has a built in utility, otherwise a package
> will suffice if not a simple bash script. Thanks for your input.

If you're using "etch", you can use the "tmpreaper" package.

-- A.
--
Andrew Reid / rei...@bellatlantic.net

Michelle Konzack

unread,
Sep 20, 2008, 8:20:07 PM9/20/08
to
Am 2008-08-28 10:39:05, schrieb Ryan Klein:
> I have been searching for a way to have Debian automatically remove
> files in the /tmp directory after a period of time. We need it mainly
> for removing old session files that grow quite rapidly (a rate of 10 per
> second). I am hoping Debian has a built in utility, otherwise a package
> will suffice if not a simple bash script. Thanks for your input.

With 10 seonds you will not be very happy...

I think, you are searching for

----[ '/etc/defaults/rcS' ]---------------------------------------------
#
# /etc/default/rcS
#
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.

TMPTIME=0
SULOGIN=no
DELAYLOGIN=no
UTC=no
VERBOSE=no
FSCKFIX=no
RAMRUN=no
RAMLOCK=no
------------------------------------------------------------------------

and set TMPTIME to 1 which mean ONE DAY, but this will only work on
bootup...

and maybe you can use a cronjob executed each minute and the codesniplet

----[ '/etc/init.d/bootclean' ]-----------------------------------------
clean_tmp() {
cd /tmp || { log_failure_msg "bootclean: Could not cd to /tmp." ; return 1 ; }

#
# Only clean out /tmp if it is world-writable. This ensures
# it really is a/the temp directory we're cleaning.
#
[ "$(find . -maxdepth 0 -perm -002)" = "." ] || return 0

if [ ! "$TMPTIME" ]
then
log_warning_msg "Using default TMPTIME 0."
TMPTIME=0
fi

[ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /tmp"

#
# Remove regardless of TMPTIME setting
#
rm -f .X*-lock

#
# Don't clean remaining files if TMPTIME is negative or 'infinite'
#
case "$TMPTIME" in
-*|infinite|infinity)
[ "$VERBOSE" = no ] || log_action_end_msg 0 "skipped"
return 0
;;
esac

#
# Wipe /tmp, excluding system files, but including lost+found
#
# If TMPTIME is set to 0, we do not use any ctime expression
# at all, so we can also delete files with timestamps
# in the future!
#
if [ "$TMPTIME" = 0 ]
then
TEXPR=""
DEXPR=""
else
TEXPR="-mtime +$TMPTIME -ctime +$TMPTIME -atime +$TMPTIME"
DEXPR="-mtime +$TMPTIME -ctime +$TMPTIME"
fi

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 )'

mkflagfile /tmp/.clean || return 1

report_err()
{
if [ "$VERBOSE" = no ]
then
log_failure_msg "bootclean: Failure cleaning /tmp."
else
log_action_end_msg 1 "bootclean: Failure cleaning /tmp"
fi
}

#
# First remove all old files...
# (Use xargs here so that only one additional process gets created)
#
find . -depth -xdev $TEXPR $EXCEPT ! -type d \
-print0 | xargs -0r rm -f -- \
|| { report_err ; return 1 ; }

#
# ...and then all empty directories
# (Don't use xargs here because dirs must be removed one by one from
# the bottom up)
#
find . -depth -xdev $DEXPR $EXCEPT -type d -empty \
-exec rmdir \{\} \; \
|| { report_err ; return 1 ; }

[ "$VERBOSE" = no ] || log_action_end_msg 0
return 0
}
------------------------------------------------------------------------

and you should be careful WHAT you are DELETING!

Thanks, Greetings and nice Day/Evening
Michelle Konzack
Systemadministrator
24V Electronic Engineer
Tamay Dogan Network
Debian GNU/Linux Consultant


--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
##################### Debian GNU/Linux Consultant #####################
Michelle Konzack Apt. 917 ICQ #328449886
+49/177/9351947 50, rue de Soultz MSN LinuxMichi
+33/6/61925193 67100 Strasbourg/France IRC #Debian (irc.icq.com)

signature.pgp

Michelle Konzack

unread,
Sep 20, 2008, 8:20:17 PM9/20/08
to
Am 2008-08-28 12:05:08, schrieb Stackpole, Chris:
> I second the cronjob. Just have something along the lines of:
> find /tmp -type f -ctime +1

CTIME is the wrong thing. You need definitively ATIME since otherwise
you can kill your system

> Obviously, you will need to set ctime to be the number you need it to
> be. Once you know that the files it returns are indeed the ones you
> want to delete then add "-exec rm -rf {} \;" to the end and toss it
> into cron.
>
> If someone has a better way, please comment.

;-)

signature.pgp
0 new messages