LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rotating log files through shell script (https://www.linuxquestions.org/questions/linux-newbie-8/rotating-log-files-through-shell-script-4175588937/)

antriksh 09-07-2016 07:47 AM

Rotating log files through shell script
 
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?

keefaz 09-07-2016 07:52 AM

Why not use logrotate?

Habitual 09-07-2016 07:58 AM

Quote:

Originally Posted by keefaz (Post 5601869)
Why not use logrotate?

What ^ said!
Built for it, actually.

keefaz 09-07-2016 08:04 AM

And not too difficult to use, generally it's already installed and set up, just add the wanted log in /etc/logrotate.conf

Code:

/path/to/logfile.log {
        size 500k
}


pan64 09-07-2016 08:12 AM

the old logfile is still opened and in use, the commands removed only the directory entry - and created another one.

antriksh 09-07-2016 08:26 AM

Ok. I am going to use logrotate. To setup this i've created a file "ping" in /etc/logrotate.d as below:

Code:

/tmp/ping_response_server1
/tmp/ping_response_server2
/tmp/ping_response_server3
/tmp/ping_response_server4
{
size 20M
rotate 4
}

Now logrotate runs daily from /etc/cron.daily/logrotate. But if i want to run logrotate hourly i just copy this logrotate script from /etc/cron.daily/ to /etc/cron.hourly and delete it from /etc/cron.daily.

Is that approach correct?

keefaz 09-07-2016 01:05 PM

You could remove the ping file from /etc/cron.hourly, put it on different path
eg: /home/antriksh/etc
Then add a crontab entry like
Code:

00 * * * * /usr/sbin/logrotate /home/antriksh/etc/ping
edit: sorry, misread your post :)

Yes your approach seems correct, leave the script in /etc/cron.hourly directory

antriksh 09-08-2016 09:57 AM

Thank you everyone for the help. logrotate did the trick.

pan64 09-09-2016 12:44 AM

glad to hear that.
(if you really want to say thanks just click on yes)


All times are GMT -5. The time now is 04:45 AM.