#! /usr/bin/python # # A script to check for large and/or old files in the temp directories # and remove them. # # The script looks for files or directories within the temp directories # owned by users who are not logged in. If the files are older than the # delete age they are deleted. # # 23-feb-06 - davidbbs - first version # 28-feb-06 - davidbbs - modified to have a list of regexp to ignore # 01-jun-06 - davidbbs - modified to deal with numeric user IDs on files # files by name. # import sys, re, os, time, datetime DEBUG = False # Directories to check tempDirectories = ["/tmp/", "/state/partition1/"] # Age of the file before it is deleted deleteAgeHours = 24*8 # only delete files belonging to users in these groups groupsToCheck = ["students", "faculty", "staff", "regression"] # If this regexp matches the file then it is skipped namesToSkip = [".X[0-9]+-lock"] # Commands lsCommand = "/bin/ls -Alc --full-time -GQ --time-style=long-iso" wCommand = "/usr/bin/w -h" groupsCommand = "/usr/bin/groups" rmCommand = "/bin/rm -rf" # Get the users who are currently logged in usersLoggedIn = [] child = os.popen(wCommand, "r") for line in child.readlines(): match = re.search("(\S+).*", line) if (match): user = match.group(1) if user not in usersLoggedIn: usersLoggedIn.append(match.group(1)) if (DEBUG): print "Users logged in:" for user in usersLoggedIn: print user users = {} dates = {} files = [] # Go through the files in the directories for tempDir in tempDirectories: if (DEBUG): print "** Checking directory: " + tempDir # Get the files lsChild = os.popen(lsCommand + " " + tempDir, "r") for line in lsChild: match = re.search("(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2})\s+\"(\S+)\"", line) if (match): validUser = False # Extract the data user = match.group(3) fileName = match.group(6) date = match.group(5) file = tempDir + fileName # If it's just a numeric ID then it's fine to delete. (I.e., the user has been changed/removed.) matchNumericID = re.search("[0-9]+", user) if (matchNumericID): validUser = True else: # Check if the user is a real user groupsChild = os.popen(groupsCommand + " " + user, "r") result = groupsChild.readline() groupsChild.close() for group in groupsToCheck: groupMatch = re.search(group, result) if (groupMatch): validUser = True # Check if the user is logged in if (user in usersLoggedIn): validUser = False # Check if the file matches an ignore pattern validFileName = True for name in namesToSkip: match = re.search(name, file) if (match): validFileName = False if (validUser & validFileName): files.append(file) users[file] = user epochSeconds = time.mktime(time.strptime(date, "%Y-%m-%d %H:%M")) dates[file] = datetime.datetime.fromtimestamp(epochSeconds) if (DEBUG): print "Found: " + fileName.ljust(40) + " for user " + user.ljust(20) + " last accessed at " + date else: if (DEBUG): print "Ignored: " + fileName.ljust(40) + " for user " + user.ljust(20) + " last accessed at " + date # For each file check the date now = datetime.datetime.now() for file in files: user = users[file] date = dates[file] ageDifference = now - date ageInHours = ageDifference.days*24.0+ageDifference.seconds/3600.0 # If the file is too old delete it. if (ageInHours > deleteAgeHours): if (DEBUG): print "Remove File: (" + user + " age: %0.1f" % ageInHours + "h)\t\"" + rmCommand + " " + file + "\"" else: print "Remove File: (" + user + " age: %0.1f" % ageInHours + "h)\t\"" + rmCommand + " " + file + "\"" os.system(rmCommand + " " + file)