automatically emptying /tmp at shutdown
Hi!
I want to empty /tmp folder on shutdown. I think - Code:
rm -rf /tmp/* |
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. |
I do mine at startup with a line in a rc.custom file that runs from rc.local.
rm -r /tmp/* 2&>/dev/null |
since version 11 you can write a rc.local_shutdown file.
Quote:
|
Thank you all for your suggestions. My problem is sorted out.
|
The only question is when can the /tmp directory be safely deleted ? At bootup or shutdown ? I wouldn't know.
|
Either one is just as safe.
|
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/* |
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. |
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.
|
Quote:
|
|
Quote:
|
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. |