LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 11-23-2012, 11:04 PM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Script - Remove old log file


I have a script which runs every 10 min. and updates a log file named /home/jack/logsfile.
Code:
....
........
if [ condition true ];
echo "Working fine." >> /home/jack/logsfile
fi
Since this script runs every 10 min and keep appending the result to /home/jack/logsfile, so this log file is getting larger day by day, so I have to manually remove it everyday. I want to add some condition in my script so that, at end of day (like at 11:50 PM or at beginning of that day like at 12:00 midnight) once it remove the old existing file (last day's logsfile) and then when it runs again, it generates a new logsfile and keep appending the result for whole day and so on...

I am not getting a clue that how to do it? If I add a
Code:
rm /home/jack/logsfile
at the begining of the script, then it will remove the logsfile everytime it will run. But I want to keep logs (only) for at least a day.

Last edited by shivaa; 12-07-2012 at 01:06 PM.
 
Old 11-23-2012, 11:27 PM   #2
arun5002
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 549
Blog Entries: 5

Rep: Reputation: Disabled
These the script im running everyday once to backup my log

#!/bin/bash

#Script -Purpose:Backup script
# Exits with zero if no error.

# Step -1 Function to archieve file using date and time

date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`

# Step -2 Function to create folder on /root/logs with Timestamp

mkdir -p /root/logs/$date

# Step -3 Function to find & move logs from /data to /root/logs

find /data/ -type f -iname *.logs -mtime +30 -print | xargs -I {} mv {} /root/logs/$date

#Step -4 Function to compress the log Backup Content

tar -cvzf /root/log/$date /root/logarchive/$date.tar.gz

#Step -5 Function to remove the Log

rm -rf /root/log/*

#Step -4 Function to Print the logs Backup status

echo "$(date) log Backuped successfully ">>/root/logs/logbackup-status-$date.log

#Step -5 Function to call the Mail trigger to user

mailid= bkupxx@gmail.com

#Step -5 Function to trigger mail after log backup status

mail -s '$(date) Logs Moved Successfully $(hostname) - Successful' $mailid

Last edited by arun5002; 11-23-2012 at 11:30 PM.
 
Old 11-24-2012, 10:37 AM   #3
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Rep: Reputation: Disabled
You can just redirect the black hole to the log file. It won't delete the file but will just clear the contents of the file.
Code:
cat /dev/null > filename.log
Add a condition to run the above code when a file size goes larger than, or number of lines go above x or everyday at midnight.

Last edited by HaydeezPluto; 11-24-2012 at 10:39 AM.
 
Old 11-24-2012, 01:16 PM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
Code:
#!/usr/bin/perl -w

umask(022);

$f=$ARGV[0];
die("Usage: $0 filename-in-cwd") unless (defined($f));
die("filename contains improper chars") unless ($f =~ /^\w[\w\.-]+$/);

$max_size=1000000;
if ( (defined($ARGV[1])) && ($ARGV[1] =~ /^\d+$/) && ($ARGV[1] > 5000) ) {
    $max_size=$ARGV[1];
}

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)=();
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)=lstat($f);

if ($size > $max_size) {
    rename("$f.old1", "$f.old2");
    rename("$f.old", "$f.old1");
    rename($f, "$f.old") or die("could not rename file $f to $f.old");
    open(F, ">$f");
    close(F);
}
exit(0);
 
Old 11-26-2012, 02:15 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Why not use the built-in tool 'logrotate' ?
http://linux.die.net/man/8/logrotate
 
4 members found this post helpful.
Old 12-07-2012, 01:03 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Used a simple workaround, side-by-side created one more script & added it to cron, which moves such log file (although I have more such scripts) once in a day at some point of time.
Code:
mv /home/jack/archive_logs/logfile /home/jack/archived_logs/logfile.$(date +%Y%m%d)
 
Old 12-08-2012, 02:22 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,582

Rep: Reputation: 480Reputation: 480Reputation: 480Reputation: 480Reputation: 480
use the find command paired with -mtime:

ex:

Code:
find /path/to/logfile.log -mtime +1 -exec rm '{}' \;
put that as the last line right before the exit, this will look for any log that is older then 24hr and remote it from the system. As mentioned above setting the DATE as a timestamp for the logs is also a great idea. If you only want to keep one days worth then you might use something like this in the log name:

Code:
### Setting up variables. 
dow=`date +%A`
LOG=/path/to/logfile/${dow}.log

### find command to clear log files older then one day

find ${LOG} -mtime +1 -exec rm '{}' \;
 
Old 12-25-2012, 10:55 PM   #8
slowerogue
Member
 
Registered: Oct 2012
Posts: 89

Rep: Reputation: Disabled
hi but this is sh file.
which means once i run it it wont quit even it done the job?
or should i cron this?
 
Old 12-26-2012, 12:00 AM   #9
descendant_command
Member
 
Registered: Mar 2012
Posts: 861

Rep: Reputation: 193Reputation: 193
At top of script you could:
Code:
tail -n 23 /home/jack/logsfile > tmpfile && mv tmpfile /home/jack/logsfile
Then you always have the last 24 hour log.
 
1 members found this post helpful.
  


Reply

Tags
logs, rotation, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 09:23 AM
script to remove text from file brixtoncalling Programming 19 10-04-2009 02:04 AM
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 08:08 AM
remove text from file with script paul_mat Linux - Software 3 11-17-2005 01:21 PM
How to remove ssh headers from log file? sswaminath Programming 1 07-25-2005 12:43 AM


All times are GMT -5. The time now is 06:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration