Truncate Log files

Hi Gurus,

I have several log files running in real time and needs to be truncated 50% or all but has to keep the logs piling up. Any ideas?

For example: /var/adm/messages and others apps log files

Thanks in advance!

tail -b number_of_ blocks_to_save /var/adm/messages > /var/tmp/messages
mv /var/tmp/messages /var/adm/messages

cat /dev/null > $logfile

when I do rm or mv .. in this case the process stops writing the log.
now when I am using > xyz.log or cat /dev/null > xyz.log or cp -f /dev/null > xyz.log ..

what happens is the file is truncated to zero ..
du -sh xyz.log and ls -lh xyz.log display zero size but when the process again writes to the log .. du -sh displays the actual size but ls -lh displays the previous size + data written ..

I've done this little script to delete lines from a file without moving it to a temporary file then moving back, it will start deleting line from line 1 to whatever % you choose.
name: truncade

#!/bin/bash
# sysrenan - www.sysrenan.com
# delete a percent of line within a file

if [ "$#" -eq 1 ]
then

    	SRC="$1"
        TOTAL=$(cat $SRC | wc -l)

        if [ "$?" -eq 0 ]
        then
            	echo "This log has $TOTAL lines";
                echo "Enter the % of lines you want to delete:";
                read linestodelete;

                DEL=$(($TOTAL * $linestodelete / 100));
                /bin/sed -i 1,$DEL\d $SRC;
        else
            	echo "Invalid file";
                exit 1;
        fi
else
    	echo "Incorrect number of arguments";
        echo "Usage: ./truncade <logfile>";
fi

hope it helps, let me know

I m getting the above error message ..

m using solaris ...

[SunOS/5.9/sparc]
[JDK1.4.2_15/Sun Microsystems Inc.]

Hey, I don't have a Solaris system to test my script sorry, I usually run RedHat OS.

But, you can try looking at:

sed --help

the purpose of this script was to not use temporary file so it will just delete within the file you mention. But you can try and modify the following:

/bin/sed -i 1,$DEL\d $SRC;

Try using:

/bin/sed 1,$DEL\d $SRC;

Or

/bin/sed -e 1,$DEL\d $SRC;

But again, I can't test since I don't have a solaris system, maybe we can wait to someone else help a little on this.

Hi.

Sed on Solaris doesn't support -i - that's far too modern! Come to think of it, I don't think any / many UNIX's support it. And --help doesn't work either.

Perhaps you could use the original solution, but use cp instead of mv:

tail -b number_of_ blocks_to_save /var/adm/messages > /var/tmp/messages  
cp /var/tmp/messages /var/adm/messages && rm /var/tmp/messages

(the file will retain its inode number and permissions, unlike with mv)

Or truncate the file using

> /var/adm/messages