LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   automatically emptying /tmp at shutdown (https://www.linuxquestions.org/questions/slackware-14/automatically-emptying-tmp-at-shutdown-523460/)

adityavpratap 01-28-2007 07:45 AM

automatically emptying /tmp at shutdown
 
Hi!
I want to empty /tmp folder on shutdown. I think -
Code:

rm -rf /tmp/*
should do the trick provided it is given in the appropriate script. Can I give it in /etc/rc.d/rc.6?

acid_kewpie 01-28-2007 08:05 AM

well i'm not familiar with slackware really, but i'd suggest a more elegnat solution like using a tmpfs filesystem as /tmp. this will hold all data in memory / swap partition and so won't persist across a boot anyway. just a line in your fstab file like

tmpfs /tmp tmpfs defaults 0 0

in actual fact though, you may well already have a similar line to /dev/shm or such like, ready to do the same job... could just use a symlink to it if you have one (redhat based systems do...)

if you do wish to just delete files, i'd recommend clearing it on startup, not shutdown though, just as simple but with less chance of busting a runlevel.

dive 01-28-2007 08:47 AM

I do mine at startup with a line in a rc.custom file that runs from rc.local.

rm -r /tmp/* 2&>/dev/null

erklaerbaer 01-28-2007 09:20 AM

since version 11 you can write a rc.local_shutdown file.
Quote:

#!/bin/sh
#
# /etc/rc.d/rc.local: Local system initialization script.
#
# Put any local startup commands in here. Also, if you have
# anything that needs to be run at shutdown time you can
# make an /etc/rc.d/rc.local_shutdown script and put those
# commands in there.
regards,

adityavpratap 01-28-2007 11:18 AM

Thank you all for your suggestions. My problem is sorted out.

H_TeXMeX_H 01-28-2007 11:51 AM

The only question is when can the /tmp directory be safely deleted ? At bootup or shutdown ? I wouldn't know.

gnashley 01-28-2007 01:55 PM

Either one is just as safe.

dracolich 01-28-2007 02:40 PM

I added this to my rc.6, after the section that clears /var/log/subsys, when using 10.1 and have never had any issues then, in 10.2 or 11.

#Clean temporary directories
/bin/echo "Cleaning temp files."
/usr/bin/rm -R /tmp/* ; /usr/bin/rm -R /var/tmp/*

MQMan 01-28-2007 04:52 PM

DON'T clear everthing in /tmp from a script in /etc/rc.d/rc.local. You might trash the hidden directories required by X that are checked for, and created in /etc/rc.d/rc.S.

Cheers.

adityavpratap 01-29-2007 12:08 AM

MQMan is right. The best place to put the housekeeping script is either /etc/rc.d/rc.local_shutdown as erklaerbaer has pointed out or in during the startup or better still to mount tmpfs on /tmp as pointed out by acid_kewpie.

ferradura 01-31-2007 07:46 AM

Quote:

Originally Posted by adityavpratap
to mount tmpfs on /tmp as pointed out by acid_kewpie.

i would like to have some info on this...what to change in fstab?

tuxdev 01-31-2007 08:55 AM

http://wiki.linuxquestions.org/wiki/Tmpfs

ferradura 01-31-2007 09:12 AM

Quote:

Originally Posted by tuxdev

thank you ;)

nx5000 01-31-2007 09:25 AM

I use tmpfs but there's also something called bootclean
/etc/init.d/bootclean
that removes old files but not some exceptions.
I post it here because I'm not sure all distro have it:

mkflagfile()
{
# Prevent symlink attack (See #264234.)
[ -L "$1" ] && log_warning_msg "bootclean: Deleting symbolic link '$1'."
rm -f "$1" || { log_failure_msg "bootclean: Failure deleting '$1'." ; return 1 ; }
# No user processes should be running, so no one should be able to introduce
# a symlink here. As an extra precaution, set noclobber.
set -o noclobber
:> "$1" || { log_failure_msg "bootclean: Failure creating '$1'." ; return 1 ; }
return 0
}


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
}


All times are GMT -5. The time now is 09:52 PM.