LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2009, 01:41 PM   #1
ericli
LQ Newbie
 
Registered: May 2009
Posts: 14

Rep: Reputation: 0
cron job for cleaning files


I want to write a script, which run as a cronjob.

It looks for a folder (/tmp/cache) every hour. If any files's one day old, delete them.

I'm new to script. My questions are:

1. how to check time stamp of a file?
2. how to make the folder (/tmp/cache) configurable? read from properties file?

Thanks in advance
 
Old 05-04-2009, 01:50 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
1. how to check time stamp of a file?
You'll need to use the 'find' command, eg.
Code:
find /tmp/cache -mtime +1 -exec rm -f {} \;
It should do the job - be careful though not to delete the staff that you don't want to delete.

As to question 2, I am not sure I understand what you mean.


Edit: the find command that I mentioned before will find files that +1 day old and DELETE them.

Last edited by sycamorex; 05-04-2009 at 02:01 PM.
 
Old 05-04-2009, 02:04 PM   #3
ericli
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you for the quick response!

the +1 after -mtime is number of days, right? Instead, how to specify in hours, like 24 hours?

I googled the following result. Any differece?

/usr/bin/find /tmp/cache -type f -mtime +1 | xargs -r /bin/rm

Question 2
folder /tmp/cache is configured in a properties file. A Java program looks at this value from the properties file and write files to the location.

I can't hard code /tmp/cache in the script (otherwise it gonna fail when "/tmp/cache" is changed in the properties file.). Can you suggest how to deal with this?
 
Old 05-04-2009, 02:30 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
the +1 after -mtime is number of days, right? Instead, how to specify in hours, like 24 hours?
+1 means at least 24hrs * 1. You could express it in minutes:

1. -mmin 5 - it would find files modified exactly 5 minutes ago
2. -mmin +5 - it would find files modified at least 5 minutes ago
3. -mmin -5 - less than 5 minutes ago

Check 'man find' for all other options.

As to the differences between xargs and exec, I believe xargs will be a more efficient option as -exec will
create a new process for each file that is sent to it by 'find'.

When it comes to the property file, have you got access to it? How does it look like? Perhaps, there's
a way of extracting the cache folder path from the property folder through sed or awk. Can you post the output of the property file (or the relevant part of it)?

Last edited by sycamorex; 05-04-2009 at 03:33 PM.
 
Old 05-04-2009, 02:57 PM   #5
ericli
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks
Now I understand -mtime, -mmin, and xargs is better.

----------
Question 2:

The properties file (called config.properties) has the format:

servername.cachetype1.folder=/tmp/cachetype1
servername.cachetype2.folder=/tmp/cachetype2
servername.cachetype3.folder=/tmp/cachetype3

Java program loads the properties and set name/value pairs to System Properties.

In Java, I can access the value by
Code:
System.getProperty("servername.cachetype1.folder");
Is there a way for linux to access System Properties? or reading from config.properties directly?
 
Old 05-04-2009, 04:00 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I don't know if that's exactly what you want:
Code:
#!/bin/bash
cache_folders=$(awk -F= '/cachetype/ { print $2 }' /path/to/config.properties)
for i in $cache_folders
do
find $i -type f -mtime +1 | xargs -r rm
done
exit 0
The awk command might need some modifications depending on whether there are more "=" signs in the config.properties file. At the moment it will only process everything after the '=' sign on the lines containing the pattern 'cachetype'.
Then it will enter each of those directories:
/tmp/cachetype1
/tmp/cachetype2
/tmp/cachetype3

and remove selected files in them.

EDIT: Test it on some temporary directories/files first!!! What's the whole output of the properties file?

Last edited by sycamorex; 05-04-2009 at 04:03 PM.
 
Old 05-04-2009, 06:00 PM   #7
ericli
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
# ------------- properties -----------


metric.jdbc.url=jdbcracle:thin:@10.12.23.190:1521:METRIC
metric.jdbc.driver=oracle.jdbc.driver.OracleDriver
metric.jdbc.user=pw
metric.jdbc.pwd=pw


vista.jdbc.url=jdbcracle:thin:@10.14.49.103:1521:VISTA
vista.jdbc.driver=oracle.jdbc.driver.OracleDriver
vista.jdbc.user=pw
vista.jdbc.pwd=pw

vista.returned.logfile.folder=/tmp/vista

nss.jdbc.url=jdbcracle:thin:@10.23.56.189:1521:NSS
nss.jdbc.driver=oracle.jdbc.driver.OracleDriver
nss.jdbc.user=pw
nss.jdbc.pwd=pw

nss.returned.logfile.folder=/tmp/nss

# ---------------- End of properties ------------

Last edited by ericli; 05-04-2009 at 06:02 PM.
 
Old 05-04-2009, 06:06 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Is it the whole output? Where are the lines with the tmp/cachetype? Also, can you add the quote tags to the output? It will make it more readable.

EDIT: oh, are the lines in bold the ones in question?

Last edited by sycamorex; 05-04-2009 at 06:07 PM.
 
Old 05-04-2009, 06:10 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
In that case, you'll need to change the awk command to:
Code:
awk -F= '/returned.logfile.folder/ { print $2 }'
 
Old 05-05-2009, 10:30 AM   #10
ericli
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks a lot.

You anwsered all my questions! I'd appreciate it very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job files vikas027 Linux - General 4 11-12-2008 04:42 AM
Cron job files by date Kumado Linux - General 4 04-26-2006 06:02 PM
cron job to detect new files rblampain Programming 8 12-28-2005 09:51 AM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 01:06 PM
howto delete files via ftp from cron job ? cccc Linux - Networking 2 01-31-2004 07:49 PM


All times are GMT -5. The time now is 01:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration