
02-24-2008, 03:54 AM
|
|
Junior Guru Wannabe
|
|
Join Date: May 2004
Posts: 83
|
|
Cleaning-up the /tmp partition
Hi,
I'm looking for a (new) script which can be run daily (via a cronjob) and deletes every file in the /tmp partition which has not been used in a week.
I'm currently using the following :
Code:
find /tmp -mtime +8 -exec rm {} \;
but that only deletes those files and does not show the files being deleted and not even the amount of files being deleted.
Any ideas how to extend this script or rewrite it? The server runs CENTOS Enterprise 4.6 x86_64.
Thanks.
Last edited by Bdzzld; 02-24-2008 at 03:57 AM.
|

02-24-2008, 04:30 AM
|
|
Web Hosting Guru
|
|
Join Date: Feb 2008
Location: Longview, Texas
Posts: 272
|
|
find /tmp -mtime +8 | xargs rm -fv
...and then you could use a redirect to log it.
__________________
██ HermeTek Network Solutions
██ Network design, security, and implementation
██ BSD & Linux consulting, training, and hosting
██ https://www.hermetek.com | 1.866.235.1288
|

02-24-2008, 05:26 AM
|
|
Web Hosting Master
|
|
Join Date: Oct 2004
Location: Kerala, India
Posts: 4,617
|
|
Use below. Make a new directory /var/tmplogs. The script will make a file with current date, fill the file with the files that will be removed and will remove those files from /tmp.
Code:
#!/bin/sh
find /tmp -mtime +8 > /var/tmplogs/log-`date -I`
find /tmp -mtime +8 | xargs rm -fv
__________________
David | www.cliffsupport.com
Affordable Server Management Solutions sales AT cliffsupport DOT com
iWebManager | Access WHM from iPhone and Android
|

02-24-2008, 06:58 AM
|
|
Junior Guru Wannabe
|
|
Join Date: May 2004
Posts: 83
|
|
Thanks for the suggestions so far...
If you write all file names of the files to be deleted to a logfile first, ain't it easier to use that logfile to remove the files instead of reissueing the 'find' command? Afaik the 'find' command can be heavy on resources...
|

02-24-2008, 07:45 AM
|
|
Web Hosting Master
|
|
Join Date: Oct 2004
Location: Kerala, India
Posts: 4,617
|
|
Modify the script as follows.
Code:
#!/bin/sh
find /tmp -mtime +8 > /var/tmplogs/log-`date -I`
for i in $(cat /var/tmplogs/log-`date -I`) ; do rm -rf $i ; done
__________________
David | www.cliffsupport.com
Affordable Server Management Solutions sales AT cliffsupport DOT com
iWebManager | Access WHM from iPhone and Android
|

02-24-2008, 09:45 AM
|
|
Junior Guru Wannabe
|
|
Join Date: May 2004
Posts: 83
|
|
I've now rewritten the script based on your suggestions.
Bash scripts can be quite powerfull
Is there also some way to check if the /var/tmplogs/ path has already been created and if not create it?
Thanks.
|

02-24-2008, 01:40 PM
|
|
Newbie
|
|
Join Date: Oct 2007
Posts: 11
|
|
Quote:
Originally Posted by Bdzzld
I've now rewritten the script based on your suggestions.
Bash scripts can be quite powerfull
Is there also some way to check if the /var/tmplogs/ path has already been created and if not create it?
Thanks.
|
Code:
#!/bin/bash
if [ ! -d "/var/tmplogs" ]; then
mkdir /var/tmplogs
fi
|

02-24-2008, 03:08 PM
|
|
<insert something witty>
|
|
Join Date: Apr 2000
Location: California
Posts: 3,051
|
|
First, mtime is the modification time. If you want files that were just not "accessed" for over 8 days, then you'd use -atime instead.
Also, you don't need to run any additional commands, you can just use:
find /tmp -atime +8 -exec rm -vf {} \; > /var/tmplogs/log-`date -I`
This way it will run the rm command in forced, verbose mode and the redirect > will write to the dated log file.
You might want to add some commands to ignore links. Also, make sure the path for the log file exists and is writable and set the cron accordingly.
|

02-26-2008, 03:50 AM
|
|
Junior Guru Wannabe
|
|
Join Date: May 2004
Posts: 83
|
|
If files are scanned on a regular basis in the /tmp path for possible abuses - which is what the csf/lfd combo does f.i. - substituting with as you suggest, will not find any files at all.
Last edited by Bdzzld; 02-26-2008 at 03:59 AM.
|

02-26-2008, 01:38 PM
|
|
<insert something witty>
|
|
Join Date: Apr 2000
Location: California
Posts: 3,051
|
|
A file could not be modified for weeks, but be accessed every hour. This is why I recommended a check for access time instead of modification? Anyway, yeah, if the files are actually opened/read by some exploit checker, then the access time will be updated (and not the modification time). I don't think much of those scanners, but I'd have assumed it only checked the file once, within minutes (or hours) of creation time and shouldn't read them again unless the modification time changed.
Anyway, use -mtime if you want or need to, but I'd save processing with:
find /tmp -mtime +8 -exec rm -vf {} \; > /var/tmplogs/log-`date -I`
Last edited by Tim Greer; 02-26-2008 at 01:42 PM.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|