Check File Size For Log Rotation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check File Size For Log Rotation
# 1  
Old 04-28-2010
Check File Size For Log Rotation

Hi Can anyone assist me, I am trying to compate the size of a logfile to a maximum size 1000 and delete if exceeds the limit.
The problem I am getting is the command not found for the line
Code:
if ( $LOGNAME_SIZE >= $MAXSIZE); then

Appreciate your response.


Script:
Code:
LOGDIR="/home/
LOGNAME="log.txt"

MAXSIZE="10000"
cd $LOGDIR

LOGNAME_SIZE=$(ls -l $LOGNAME | tr -s " " | cut -d" " -f5)
if ( $LOGNAME_SIZE >= $MAXSIZE); then
echo "$LOGNAME exceeds max threshold"
rm $LOGNAME
else
echo "$LOGNAME is below max threshold"


Last edited by Scott; 04-29-2010 at 03:15 PM.. Reason: Code tags, please...
# 2  
Old 04-28-2010
What shell are you using?
# 3  
Old 04-28-2010
Quote:
Originally Posted by sureshcisco
The problem I am getting is the command not found for the line
if ( $LOGNAME_SIZE >= $MAXSIZE); then
Adjust it to:
Code:
if [ $LOGNAME_SIZE -ge $MAXSIZE ]; then

Besides you could optimize the following line:
Code:
LOGNAME_SIZE=$(ls -l $LOGNAME | tr -s " " | cut -d" " -f5)

to
Code:
LOGNAME_SIZE=$(ls -l $LOGNAME | awk '{print $5}')

# 4  
Old 04-29-2010
Updated the command - I see no difference. It's the same error. Indeed the file size exceed the MAXSIZE and I get the wrong output.

Code:
#!/bin/sh
LOGDIR="/home/"
LOGNAME="log.txt"
MAXSIZE="10000"

cd $LOGDIR

LOGNAME_SIZE=$(ls -l $LOGNAME | awk '{print $5}')

echo $LOGNAME_SIZE

if ($LOGNAME_SIZE >= $MAXSIZE);
then
echo "$LOGNAME exceeds max threshold"
rm $LOGNAME
else
echo "$LOGNAME is below max threshold"
fi

[root@server ~]# ./simple.sh
131072
./simple.sh: line 12: 131072: command not found
log.txt is below max threshold
[root@server ~]#


Last edited by Scott; 04-29-2010 at 03:16 PM.. Reason: Code tags, PLEASE!
# 5  
Old 04-29-2010
Code:
if ((LOGNAME_SIZE >= MAXSIZE));

maybe?
Quote:
Originally Posted by man sh
Arithmetic Expansion
Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for arithmetic expansion is as follows:

$((expression))
# 6  
Old 04-29-2010
Quote:
Originally Posted by sureshcisco
Updated the command - I see no difference.
Of course you need to change your if statement line too as I suggested.
# 7  
Old 04-29-2010
Thanks you very much Pseudocoder and Tukuyomi....it works!!!.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename the Linux log file to the rotation date

Hi all, could any provide me a solution for the below requirement. I have two files namely abc.log.1 and abc.log.2 The above files have time stamp as Dec 08 and Dec 09 I need to rename the files as abc.log.1_20141208 and abc.log.2_20141209 and move to another bkp directory. Thanks in... (2 Replies)
Discussion started by: bhaskar t
2 Replies

2. Shell Programming and Scripting

File size check

I am trying to check whether two files are empty or not using below if condition but its checking for only one file if ] Again I tried if && ] Need your assistance (2 Replies)
Discussion started by: Aditya_001
2 Replies

3. Shell Programming and Scripting

Check log file size every 10 minute. Alert if log not update

How to check log size every 10min. by script (can use crontab) if log size not change with alert "Log not update" Base run on SunOS 5.8 Generic_Virtual sun4u sparc SUNW,SPARC-Enterprise logFiles="log1.log log2.log" logLocation="/usr/home/test/log/" Out put. Tue Jan 31... (3 Replies)
Discussion started by: ooilinlove
3 Replies

4. Red Hat

check ip rotation

Hi all, I configured ip rotation on exim mail server by modifying the /etc/exim.conf file. I want to check whether the ip address rotating or not. Can any body show me how can i find out is it rotating or not. Thanks, (1 Reply)
Discussion started by: mastansaheb
1 Replies

5. Shell Programming and Scripting

check the file size

if ; then cp /tmp/testfolder/*.* ~/new/logs/ else echo "No files today" exit fi The problem is this doen't work when there is more than 1 file. Please tell me how to take the latest file and check the size of the file in a directory (1 Reply)
Discussion started by: sandy1028
1 Replies

6. Shell Programming and Scripting

Log file rotation

Hi I am trying to create a simple function that implements log rotation rotatelogs () { file_name=$1 archive_dir=$2 retention_period=$3 find $archive_dir -name $file_name -mtime +$retention_period -exec rm -f {} \; } Issue i am facing is the file_name would be something like my... (3 Replies)
Discussion started by: xiamin
3 Replies

7. AIX

audit.log file rotation

Hi guys, I've googled this quite a bit, and tried searching on these forums, but haven't found a solution to my problem. I wanted to inquire about AIX's audit subsystem - more specifically, how to rotate its log file. So far I've been able to find how to rotate AIX syslog log files, and I... (2 Replies)
Discussion started by: w1r3d
2 Replies

8. Shell Programming and Scripting

Check for file size is zero or not.

I have following script on AIX/KSH if ] ; then echo "filename exists and is > 0 bytes" else echo "filename does not exist or is zero length" fi It is not working. What is wrong here??? (3 Replies)
Discussion started by: Hangman2
3 Replies

9. Shell Programming and Scripting

To check file size

Hi All, I am in small problem.. i have one script which transfers some big files to my ftp usign normal command like put .... my problem is how to check whether my file have been transferred successfully on ftp or not... i know only inside ftp we have option like 'size' command which... (2 Replies)
Discussion started by: Shahul
2 Replies

10. HP-UX

Check size and rotate log script.

Hi Can you suggest some perl script. My OS is HP-UX 11.11 I want to it into a cron job. Every night it will backup the file with that day's date and open a dummy file. Thanks Ash (3 Replies)
Discussion started by: ashishT
3 Replies
Login or Register to Ask a Question