Operating System - HP-UX
1819901 Members
2538 Online
109607 Solutions
New Discussion юеВ

Re: Cron Job to Delete files in /tmp directory

 
SOLVED
Go to solution
LM_2
Frequent Advisor

Cron Job to Delete files in /tmp directory

Is it possible to create a job which would be run by cron to delete files within a directory?? I have a /tmp directory - which gets filled with "junk" and the files are not needed. What would be the correct syntec to create the job - and also the syntax to put it into cron to run on a weekly basis??
4 REPLIES 4
Paul Sperry
Honored Contributor
Solution

Re: Cron Job to Delete files in /tmp directory

Here is a cron we user to get rid of oracle transaction loges every day a 6am

00 06 * * * /sbin/find /bkup_mnt/oradev/arch -type f -ctime +1 -name *.ARC -exec rm -f {} \;

all one line
ctime +1 will delete stuff that is on day old or older
*.ARC is the file extension I suppose you could use *
ran weekly you would want say sunday @6

0 6 * * 0

man crontab
Hazem Mahmoud_3
Respected Contributor

Re: Cron Job to Delete files in /tmp directory

First create the script and test it. Here is a sample script that you would then need to modify:

#!/bin/sh

rm /tmp/*.log #remove all files with .log
rm /tmp/report* #remove all report files

Then save the script as cleanup.sh for example. The do the following:

# crontab -e
This will allow you to add cron jobs. Here is the format of each line in the file:
min hr day month day of week command

Therefore, if you want it to run on the 15th of every month, add a line like this for example:

00 00 15 * * /scripts/cleanup.sh

-Hazem
Steven E. Protter
Exalted Contributor

Re: Cron Job to Delete files in /tmp directory

I'm attaching a script that does a lot of what post one does. It is configurable and works wonderfully via cron.

There is an interactive functionality available if you have an add in library. This one cleans pdf files. Its easily modifyable to clean /tmp, just set up the directories you want it to scour in a config file.



SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Tom Dineen_2
Advisor

Re: Cron Job to Delete files in /tmp directory

before you put anything in your cron, test it out !!! what I mean, "*" wild string can be very dangerous. example:

find /tmp -mtime +25 -exec rm -rf *foo \;


note: any mistake with "*" can be dangerous.