LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job for cleaning files (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-for-cleaning-files-723614/)

ericli 05-04-2009 12:41 PM

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

sycamorex 05-04-2009 12:50 PM

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.

ericli 05-04-2009 01:04 PM

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?

sycamorex 05-04-2009 01:30 PM

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)?

ericli 05-04-2009 01:57 PM

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?

sycamorex 05-04-2009 03:00 PM

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?

ericli 05-04-2009 05:00 PM

# ------------- properties -----------


metric.jdbc.url=jdbc:oracle: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=jdbc:oracle: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=jdbc:oracle: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 ------------

sycamorex 05-04-2009 05:06 PM

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?

sycamorex 05-04-2009 05:10 PM

In that case, you'll need to change the awk command to:
Code:

awk -F= '/returned.logfile.folder/ { print $2 }'

ericli 05-05-2009 09:30 AM

Thanks a lot.

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


All times are GMT -5. The time now is 04:51 AM.