Log rotate using perl Logfile::Rotate

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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?



#!/usr/bin/perl
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.
$logfile->rotate();
# make sure the log file is unlocked (destroying object unlocks file)
undef $logfile;






share|improve this question


























    up vote
    3
    down vote

    favorite












    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?



    #!/usr/bin/perl
    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.
    $logfile->rotate();
    # make sure the log file is unlocked (destroying object unlocks file)
    undef $logfile;






    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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?



      #!/usr/bin/perl
      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.
      $logfile->rotate();
      # make sure the log file is unlocked (destroying object unlocks file)
      undef $logfile;






      share|improve this question














      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?



      #!/usr/bin/perl
      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.
      $logfile->rotate();
      # make sure the log file is unlocked (destroying object unlocks file)
      undef $logfile;








      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 7 at 22:16









      Jeff Schaller

      31.4k846105




      31.4k846105










      asked Feb 1 at 2:50









      Santhosh S T

      186




      186




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          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) 
          $log->rotate();



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






          share|improve this answer




















          • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
            – Santhosh S T
            Feb 1 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 at 4:36











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f421120%2flog-rotate-using-perl-logfilerotate%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          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) 
          $log->rotate();



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






          share|improve this answer




















          • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
            – Santhosh S T
            Feb 1 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 at 4:36















          up vote
          4
          down vote



          accepted










          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) 
          $log->rotate();



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






          share|improve this answer




















          • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
            – Santhosh S T
            Feb 1 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 at 4:36













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          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) 
          $log->rotate();



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






          share|improve this answer












          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) 
          $log->rotate();



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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 at 3:24









          dhag

          10.7k32642




          10.7k32642











          • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
            – Santhosh S T
            Feb 1 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 at 4:36

















          • Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
            – Santhosh S T
            Feb 1 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 at 4:36
















          Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
          – Santhosh S T
          Feb 1 at 3:38




          Thanks for the reply dhag.. Can you check the edited script .. earlier I posted wrong script
          – Santhosh S T
          Feb 1 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 at 4:36





          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 at 4:36













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f421120%2flog-rotate-using-perl-logfilerotate%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay