LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12: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, 12:50 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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 01:01 PM.
 
Old 05-04-2009, 01: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, 01:30 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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 02:33 PM.
 
Old 05-04-2009, 01: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, 03:00 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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 03:03 PM.
 
Old 05-04-2009, 05: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 05:02 PM.
 
Old 05-04-2009, 05:06 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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 05:07 PM.
 
Old 05-04-2009, 05:10 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
In that case, you'll need to change the awk command to:
Code:
awk -F= '/returned.logfile.folder/ { print $2 }'
 
Old 05-05-2009, 09: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



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 03:42 AM
Cron job files by date Kumado Linux - General 4 04-26-2006 05:02 PM
cron job to detect new files rblampain Programming 8 12-28-2005 08:51 AM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 12:06 PM
howto delete files via ftp from cron job ? cccc Linux - Networking 2 01-31-2004 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:12 PM.

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
Open Source Consulting | Domain Registration