Problems with hourly removal in cron


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Problems with hourly removal in cron
# 1  
Old 04-04-2011
Problems with hourly removal in cron

In my cron job on one system I have log files removed older than 3 days, like so:
Code:
find /u01/arch/PRD8 -mtime +3 -a -name "*.log" -exec rm {} \;

On another system I would like to check and remove anything older than 2 hours.

Any help would be greatly appreciated Smilie

Last edited by Yogesh Sawant; 04-09-2011 at 07:40 AM.. Reason: added code tags
# 2  
Old 04-04-2011
find's -mtime measures in days, as you surely know. What other options are availble depends on what your system is.

---------- Post updated at 12:30 PM ---------- Previous update was at 12:26 PM ----------

A solution from a thread currently immediately below this one is as follows:
Code:
#!/bin/sh

[ -f /path/to/Flag_File ] &&
  find /where/files/are/ ! -newer Flag_File |
  while read mFile
  do
    mv "${mFile}" New_Directory
  done

touch /path/to/Flag_File

The very first time it runs, it removes nothing, but it creates a file to refer to. Next time, it moves everything older than the file then updates the timestamp. So, run it every 10 minute and it moves files older than 10 minutes. Run it every 3 hours and it moves files older than 3 hours. etc.
# 3  
Old 04-04-2011
Running AIX 5.3
# 4  
Old 04-04-2011
Thanks.

Adapting the above script may do what you want, put it in a crontab that runs it every two hours, like

Code:
* 0,2,4,6,8,10,12,14,16,18,20,22 * * * /path/to/script.sh

# 5  
Old 04-04-2011
Then within the script I would have something like:
Code:
find /u01/arch/PRD8 -a -name "*.log" -exec rm {} \;

Is this correct?

Last edited by Yogesh Sawant; 04-09-2011 at 07:40 AM.. Reason: added code tags
# 6  
Old 04-04-2011
Then within the script you'd have the script I already posted,
Code:
#!/bin/sh

[ -f /path/to/Flag_File ] &&
  find /where/files/are/ ! -newer Flag_File |
  while read mFile
  do
    mv "${mFile}" New_Directory
  done

touch /path/to/Flag_File

Where /path/to/Flag_File is wherever you want to keep the flag file, and /where/files/are is where you want to search in, and mv "${mFile}" New_Directory is changed to whatever you want to do to old files.
# 7  
Old 04-04-2011
There a couple of us that watch the systems and I would like to keep the scripts pretty standard so everyone knows what's going on. Would the contents of the script like I have it now work with the cron set to run every two hours?
Code:
find /path/to/file -a -name "*.log" -exec rm {} \;


Last edited by Yogesh Sawant; 04-09-2011 at 07:41 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problems with set up Cron

Hi I want to set up a cron for every 15 minutes between some hours. Can you tell whether the below command will work. Getting confuse because my time goes from night to next day morning. i want to setup 8 PM to next day 10 AM. */15 20-10 * * * sh abc.sh (1 Reply)
Discussion started by: raju2016
1 Replies

2. UNIX for Dummies Questions & Answers

Execution Problems with Cron

Good evening, ive got this cron to be run: if i run this manually it doesnt work,it takes me to the prompt again /export/app/CO/opge/scr/Informe_parametros_colombia.ksh >/dev/null 2>&1 here is the code fragment: coopge@coopge: opge PRODUCCION>more... (1 Reply)
Discussion started by: alexcol
1 Replies

3. Shell Programming and Scripting

Execution problems with cron

I am new to creating crontab file , i just wrote below (40 19 * 3 * /root/maths/practisecron.sh), the script just prints "Hi". When ever i save the above file i am getting this . # crontab -e crontab: installing new crontab Can you please check where it went wrong.. (1 Reply)
Discussion started by: vkiyv05
1 Replies

4. UNIX for Dummies Questions & Answers

cron problems

I am trying to setup a cron that runs a script to update my ip for my domain name every time I reboot and checks if my ip changes every 5 minutes and if it does then I update it. I am running Ubuntu server 12.04. The file /etc/cron.d/ipupdate contains # # # Run ipupdate # # /5 * ... (3 Replies)
Discussion started by: Bros2
3 Replies

5. Ubuntu

Problems with a .sh file in cron

Hi guys. This is my first post so bear with me. I'm trying to get cron to run a shell script in my home directory (/home/server) that checks the temperature of my HDD. The script works fine, however I can't run it in cron. I've checked the syslog and I have only seen two errors: - Exited with... (11 Replies)
Discussion started by: Rautoner
11 Replies

6. UNIX for Dummies Questions & Answers

Execution Problems with Cron

Hi friends, today i created a cron job , registered the crontab file associated but dont know why the cron is not getting executed at the right time.?? content of Crontab file : 21 15 * * * /subrat/myt i wanted to execute the script myt on 15:21 PM of everyday. the script myt... (2 Replies)
Discussion started by: paras.oriental
2 Replies

7. Programming

execution problems with cron

how to store a date into file? and how we can access date from the file? ---------- Post updated at 06:09 AM ---------- Previous update was at 06:08 AM ---------- how we can store date in file? (1 Reply)
Discussion started by: causalmodi777
1 Replies

8. UNIX for Dummies Questions & Answers

nfs mount and links removal problems.

Ok, so I have an nfs mount setup and within it there are symbolic links to other directories and such. So anyways I created a link to a directory like so ln -s /var/stuff/more/stuff/here/ stuff/ and i ended up with directory stuff with link 'here' inside. so i was pieved and decided... (1 Reply)
Discussion started by: VRoemer
1 Replies

9. UNIX for Dummies Questions & Answers

cron problems

Hi there, this time a have a other problem :) i will start a script over the cron. if i start the script manual (per command) it works without any problems, but over the cron it doesent work. my cron looks like this: 26 15 * * * /path/scriptname.sh 2> /path/error.log there is no... (3 Replies)
Discussion started by: scotty
3 Replies

10. UNIX for Advanced & Expert Users

Cron problems

Hi, I am having a few problems with cron. I have created a shell script (to rotate www logs - this works) and then did the following (as root) #crontab -e 2 0 * * 3 /cron_scripts/wwwlogs.sh ? w ? q # crontab -l entry not shown e.g. it did not show what I had just writern I then... (6 Replies)
Discussion started by: warrend
6 Replies
Login or Register to Ask a Question