LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-07-2016, 07:47 AM   #1
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 83

Rep: Reputation: 0
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?
 
Old 09-07-2016, 07:52 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Why not use logrotate?
 
Old 09-07-2016, 07:58 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Why not use logrotate?
What ^ said!
Built for it, actually.

Last edited by Habitual; 09-07-2016 at 07:59 AM.
 
Old 09-07-2016, 08:04 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
}
 
Old 09-07-2016, 08:12 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
the old logfile is still opened and in use, the commands removed only the directory entry - and created another one.
 
Old 09-07-2016, 08:26 AM   #6
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 83

Original Poster
Rep: Reputation: 0
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?
 
Old 09-07-2016, 01:05 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 09-07-2016 at 04:32 PM.
 
Old 09-08-2016, 09:57 AM   #8
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 83

Original Poster
Rep: Reputation: 0
Thank you everyone for the help. logrotate did the trick.
 
Old 09-09-2016, 12:44 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

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


Reply



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
rotating apache logs.....looking for a good script to delete old log file ringding Linux - Server 6 04-25-2020 12:59 PM
Simple shell script that will delete every 5 minutes all files with extension log Alexis2156 Programming 3 11-06-2014 02:10 PM
[SOLVED] Using bash script to purge rotating tcpdump files merixon Programming 5 10-20-2010 11:19 AM
[SOLVED] I need a shell script to delete files wich location should be read from a log file jorgemarmo Programming 24 07-02-2010 07:07 AM
LXer: Rotating Linux Log Files LXer Syndicated Linux News 0 06-07-2006 06:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:14 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
Open Source Consulting | Domain Registration