Log rotate using perl Logfile::Rotate
Clash 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;
perl logrotate size
add a comment |Â
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;
perl logrotate size
add a comment |Â
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;
perl logrotate size
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;
perl logrotate size
edited Aug 7 at 22:16
Jeff Schaller
31.4k846105
31.4k846105
asked Feb 1 at 2:50
Santhosh S T
186
186
add a comment |Â
add a comment |Â
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).
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 torotate
within a test, as I did in my answer, should do the trick.Logfile::Rotate
does not seem to support asize
key as in your edited question.
â dhag
Feb 1 at 4:36
add a comment |Â
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).
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 torotate
within a test, as I did in my answer, should do the trick.Logfile::Rotate
does not seem to support asize
key as in your edited question.
â dhag
Feb 1 at 4:36
add a comment |Â
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).
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 torotate
within a test, as I did in my answer, should do the trick.Logfile::Rotate
does not seem to support asize
key as in your edited question.
â dhag
Feb 1 at 4:36
add a comment |Â
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).
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).
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 torotate
within a test, as I did in my answer, should do the trick.Logfile::Rotate
does not seem to support asize
key as in your edited question.
â dhag
Feb 1 at 4:36
add a comment |Â
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 torotate
within a test, as I did in my answer, should do the trick.Logfile::Rotate
does not seem to support asize
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password