Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts

    cron job refresh file on file size

    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.

    Code:
    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.

    Code:
    /home/myhost/public_html/sitename/chat/freshcopy.html /home/myhost/public_html/sitename/chat/msg.html
    thanks
    Last edited by durangod; 07-12-2011 at 03:03 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    What do you mean by "i dont want to remove it i want to replace it"? You want to zero out the file?

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    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?
    Last edited by durangod; 07-12-2011 at 04:57 PM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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:
    Code:
    #!/bin/bash
    FILENAME=/home/somedir/msg.html
    FRESHCOPY=/home/somedir/freshcopy.html
    MAXSIZE=307200 # 300k
    FILESIZE=$(stat -c%s "$FILENAME")
    #echo "Size of $FILENAME = $FILESIZE bytes."
    if [ $FILESIZE -gt $MAXSIZE ]; then
            echo "Too big"
            cp -f $FRESHCOPY $FILENAME
    else
            echo "Good"
    fi
    If you're going for a one-liner this is one way to do it:
    Code:
    find /path/to/your/dir/ -name "msg.html" -size +300k -execdir cp -f /home/somedir/freshcopy.html /home/somedir/msg.html \;

  • Users who have thanked Inigoesdr for this post:

    durangod (07-13-2011)

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    perfect thanks so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •