Welcome Guest | Login

Clearing logs

I am trying to set up a cron job to clear the logs in my Rails app every 31 days.  I used the following command in the cron job setup in cpanel:

cd "app folder" rake clear_logs

But it doesn't seem to be doing anything.  And I am not receiving any output reports to the email address I specified.  If I change the command to:

cd /app-folder rake clear_logs

then I receive output emails telling me that /app-folder does not exist.

2007-01-02 03:36 PM

Hi there - you need to make sure that full paths are specified when you use cron

cd /home/username/railsapp && /usr/local/bin/rake clear_logs

Let me know if this works for you.

~William

2007-01-02 03:48 PM

Hi William,

That did the trick.

Thanks a million!

2007-01-02 04:01 PM

Here's my version of backing up and clearing the log files on a nightly basis (mostly because I hate having to sift through long log files).

I created a new directory called backup_log in my root directory (/home/username/backup_log) to store the nightly log backups. To help keep things organized in the backup_log folder I'll dynamically create a subfolder each time the script is run to hold the final zipped files. This is the script I ended up with (I'm sure it could be improved upon but at least it works).

backup_log.sh
======================================================

#!/bin/sh
# Set the backup date to be appended to the filename
date=`date +%Y_%m_%d`

# create subfolder with the backup date in the backup_log directory
cd /home/username/backup_log
mkdir $date

cd /home/username/railsapp/log/

# Copy Production, Server and FastCGI.crash logs
cp production.log production_$date.log
cp server.log server_$date.log
cp fastcgi.crash.log fastcgi.crash_$date.log

# Move the copied logs to home/username/backup_log directory
mv production_$date.log /home/username/backup_log/$date
mv server_$date.log /home/username/backup_log/$date
mv fastcgi.crash_$date.log /home/username/backup_log/$date

# gzip the logs
/usr/bin/gzip /home/username/backup_log/$date/production_$date.log
/usr/bin/gzip /home/username/backup_log/$date/server_$date.log
/usr/bin/gzip /home/username/backup_log/$date/fastcgi.crash_$date.log

# clear the logs in the rails application
cd /home/username/railsapp && /usr/local/bin/rake log:clear

exit

======================================================

You can copy and paste this into your text editor and name it backup_log.sh and store this in your root directory as well (/home/username/). You'll need to make sure that it has the correct permissions so do a chmod 755 backup_log.sh in your terminal. To test it out simply run ./backup_log.sh in your terminal session. You should now have a dated subfolder in your backup_log directory that contains the dated and zipped log files. Also check your log files in your rails application to verify that they have been reset as well.

Once you have a working script you can set up a cron job to automate the backup. For my purposes I have the cron run the script every night at midnight. So my cron job looks something like this:

0 0 * * * /home/username/backup_log.sh

To set up your cron job, go to your cPanel and choose cronjobs in the advanced panel. Choose the Advanced (Unix Style) button and enter your necessary time requirements and the path to your backup_log.sh script. For a brush up course on cron jobs you can refer to this site: http://www.linuxhelp.net/guides/cron/

Anyways I hope that helps anybody looking for help.

2008-02-20 02:07 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!