The below script works when executed on CentOS, but it doesn't rotate logs based on size. A new log is generated every time I execute this script. Can anyone tell me how to make this script work based on size?

use Logfile::Rotate;
my $logfile = new Logfile::Rotate(
  File => '/var/log/remotehost/fakepath/Syslog.log',
  Count => 100,
  Gzip => '/usr/bin/gzip',
  size => 1*1024*1024,
  sub {
    open my $PID, '<', '/usr/lib/systemd/system/rsyslog.service' or
    die "Unable to open pid file:$!\n";
    chomp(my $pid = <$PID>);
    close $PID;
    kill 'HUP', $pid;
# Log file locked (really) and loaded. Now let's rotate it.
# make sure the log file is unlocked (destroying object unlocks file)
undef $logfile;

Indeed, Logfile::Rotate does not rotate based on size, which should be unsurprising since nowhere does its documentation say it does. Perhaps the simplest way to rotate based on size would be to wrap the call to rotate within an if, for example:

if (-s '/var/log/remotehost/fakepath/Syslog.log' > 1048576) {

This should rotate the logs only when the named file is larger than 1MB (the size is given in bytes).

  • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script – Santhosh S T Feb 1 '18 at 3:38
  • The code from your original question looked correct, apart from the fact that it would rotate unconditionally. Wrapping the call to rotate within a test, as I did in my answer, should do the trick. Logfile::Rotate does not seem to support a size key as in your edited question. – dhag Feb 1 '18 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.