View Full Version : cron job refresh file on file size

07-12-2011, 03:01 PM
Hi i have been looking for a reference guide to cron commands but all i have found so far is everyone talks about how to set up the times. I need the actual command reference.

What i want to do is set up a cron so that when my simple chat text file grows too large i want to refresh it.

i already have a cron job in place (so i already have those codes) that refreshes at certain times but i want to add one that does it by file size also incase of spammer.

I do know that when using rm you need to use -f to force it past the confirm question. But i dont want to remove it i want to replace it.

the file name is gonna be msg.php and i want to refresh it when it gets to
1 mb is a good number i think. not too small and not too huge.

this is the closest thing i have found so far.

I am assuming what the commands do here so dont hold me to that, thats why i need a reference guide.

command -type f -size -3000c -exec rm -f {}
type is file size 300kb execute remove force ?

here is what i have in place right now to refresh it by time..
how do i modify to do it by file size.

/home/myhost/public_html/sitename/chat/freshcopy.html /home/myhost/public_html/sitename/chat/msg.html


07-12-2011, 04:21 PM
What do you mean by "i dont want to remove it i want to replace it"? You want to zero out the file?

07-12-2011, 04:53 PM
yes what happens is that i have a very simple chat script that i pieced together and it has a file called freshcopy (blank) and 3x a day the msg file (text chat text) is replaced with a fresh version.

basically what it does is it refreshes the chat room content to null twice a day, i did this in case of spam and of course not wanting to get a huge file in there.

but what i was thinking is that what if i get spam hit in the chat by a bunch of links all at once. i dont want it to run a muk and go crazy waiting for the time of day to replace the file. so thats why i wanted to set a file limit size and refresh on size also.

my current time structure works fine it clears it out at 3am, 6am, 1515pm so thats fine, can it do it by file size also?

07-13-2011, 05:26 PM
There are almost infinite options you can pursue, but I prefer to write a shell script to manage it and just call that with cron:

MAXSIZE=307200 # 300k
FILESIZE=$(stat -c%s "$FILENAME")
#echo "Size of $FILENAME = $FILESIZE bytes."
if [ $FILESIZE -gt $MAXSIZE ]; then
echo "Too big"
echo "Good"

If you're going for a one-liner this is one way to do it:

find /path/to/your/dir/ -name "msg.html" -size +300k -execdir cp -f /home/somedir/freshcopy.html /home/somedir/msg.html \;

07-13-2011, 07:14 PM
perfect thanks so much!