LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   TMP folder, is everything deletable in there? (https://www.linuxquestions.org/questions/linux-newbie-8/tmp-folder-is-everything-deletable-in-there-298690/)

ginda 03-07-2005 09:26 AM

TMP folder, is everything deletable in there?
 
I was just wondering the /tmp folder is everything just temp file sin there?what is it used for?

Technoslave 03-07-2005 09:42 AM

Not everything in there is deletable, it would do bad things. /tmp is used as a "storage" area for currently running programs. I use it for when I want to just store something in and don't care if I forget about it or not ( even though I'm usually good in my clean up ). Things that are placed in /tmp will be removed when the machine is rebooted.

Typically, you don't want to just start deleting things that are in there on a whim...but who knows, maybe you can, give it a try, that's the only way you'll know for sure! :-D

jxi 03-07-2005 09:55 AM

First of all, don't attempt to delete everything in /tmp if you have gui sessions and/or a number of services running...
but, (if you are asking whether it is safe to delete everything in /tmp before a shutdown or reboot), the answer is yes, though you would typically perform the delete with a privelged account /process.

While one or more gui sessions are running there are lock files in /tmp that are necessary to keep things in order. also, If your're running something like mysql it may maintain a socket in /tmp for communication client to server.

if your box is running all the time and you are concerned about /tmp getting cluttered you could run (as root)

LASTCHNG='+2' # set to the number of days back files are considered 'old'
find /tmp/* -ctime $LASTCHNG -exec rm -rfv {} \;

20061216 edited the above line, taking out `#' at the beginning (it was supposed to represent the root prompt :/

Regards,
JOhn I.

syinx 12-15-2006 10:01 AM

"Another query on /tmp"
 
I am using suse 10.0 , It has created many temporary files in /tmp folder around 3 GB , How do I know which files i could delete while i am running. or can i delete all while running in safe mode.

any suggestions please.

jxi 12-16-2006 12:09 PM

Did you try the above code I posted?

Since then I added checking for open files. The following bash script seems to work :
Code:

#!/bin/bash
TMPMSG='/root/Tmpclean.msg'
cat /dev/null >$TMPMSG
for I in /tmp /var/tmp
do
    tmpLSOF=`mktemp /tmp/SnapLSOF.XXXXXXX`
#safety kludge
    if [ -z $tmpLSOF ]
    then
        tmpLSOF='/tmp/heyINeedToInstallmktemp'
    fi 
#end safety kludge 
    lsof > $tmpLSOF
    find $I ! -type d -a -cmin +500 -print 2>>$TMPMSG | while read J
    do
        echo "processing found item $J"
#Check for open files before deleting from tmp
        if [ 0 -eq $(cat $tmpLSOF |grep "$J" 2>/dev/null | wc -l) ]
        then
            echo "File $J can be deleted"
            rm -v "$J"
        else
            echo '*------------------------------------*'
            echo "File CANNOT be deleted: $J"
            lsof |grep "$J"
            echo '*------------------------------------*'
        fi
    done
    echo "$0 running empty directory check in $I ..."
    find $I/* -depth -type d -empty -exec rmdir {} \; 2>>$TMPMSG
done

This is run from root's cron entry specifying every 8 hours. Tweak the frequency (along with `-cmin +500' on line 8) to your style.

Caveats:
o You must have mktemp installed to run without the kludge for the output file of `lsof'

o On some systems /var/tmp is a symlink to /tmp so a separate loop for it would be a waste.

o The echo statements are just for a sanity check. They can be safely deleted (in face the entire else stanza inside the `do' can be).

o Some might question putting the output of `lsof` into a file on /tmp, where stuff is being deleted. -cmin + <minutes> protects you.

o the `rm' on line 21 could also need the -f (force) flag.

unSpawn 12-16-2006 12:46 PM

Weird nobody mentioned tmpwatch yet.

jxi 12-16-2006 01:43 PM

Good Point
 
unSpawn: yes, tmpwatch would be the smarter way to go, if it's installed. When I got fedora going again recently I saw it in /etc/cron.daily but went ahead and copied my script from the slack box. `tmpwatch' didn't seem to be doing anything. Now looking at it, i see why, the default settings are very conservative. 10 days since file accessed, 30 days for /var/tmp.

tmpwatch is no doubt safer (checks for possible race conditions). guess i'll adjust the settings and re-enable it on fedora.

syinx: if tmpwatch is not installed it should be available thru yast


All times are GMT -5. The time now is 02:25 AM.