#!/usr/bin/perl -w
#
# Logrotate 0.1
#
# Uses an external configuration file with the following syntax:
#
# [logfile] [versions] [pidfile]
# /somedir/logfile 5 /var/pid/someapp.pid
#
#
use strict;
use Logfile::Rotate;
# Change between these lines to change the default config file.
#-------------------------------------------
my $config = '/etc/logrotate.conf';
#-------------------------------------------
our $confignr;
our @config;
our $appid;
if (defined $ARGV[0]) { $config = $ARGV[0] }
else {
print "Using default configfile: $config\n";
}
if (! -e $config) { syntax () }
else {
open(CONFIG, $config) or syntax ();
@config = <CONFIG>;
@config = grep !/^#/, @config;
chomp @config;
$confignr = @config;
}
for (my $nr = 0; $nr < $confignr; $nr++) {
$_ = $config[$nr];
/^(\b)\s+(\b)\s+(\b)/;
my $file = $1;
my $versions = $2;
my $pidfile = $3;
print $file;
print $versions;
print $pidfile;
my $log = new Logfile::Rotate(
File => $file,
Count => $versions,
Gzip => 'lib',
Post => sub{
open(IN, $pidfile);
$appid = <IN>;
chomp $appid;
kill("HUP", $appid); },
Dir => '/var/log/old',
Flock => 'yes',
Persist => 'yes',);
if (defined $log) { print "Looks ok..\n";
} else {
die "An error accured processing config data\n";
}
$log->rotate();
undef $log;
}
sub syntax {
print <<eof;
Make sure the configfile exists and is
readable by the script.
Correct use is:
$0 configfile
if no argument is given the script
will use the default:
/etc/logrotate.conf
eof
exit;
}
In reply to logrotate
by enaco
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.