Hello. I am running below command to capture ping response from a server:
ping server1 | perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > ping_response_server1 &
Now this generates a log file name ping_response_server1. I want this file to be rotated after reaching certain threshold size. For that i tried to create below shell script:
Code:
#!/bin/bash
MaxFileSize=500000
logfile=$1
if [ ! -f $logfile ]; then
echo "log file not found $logfile"
exit 1
fi
#Get size in bytes**
file_size=`du -b $logfile | tr -s '\t' ' ' | cut -d' ' -f1`
if [ $file_size -gt $MaxFileSize ];then
timestamp=`date +%s`
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
rm $logfile
touch $logfile
fi
In the argument of this script i pass the file name ping_response_server1. But when i run this script it doesn't work properly. It creates the new log file but the old one ie ping_response_server1 doesn't get updated any more and no responses gets captured in log anymore.
Any idea what is wrong with this concept?