Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Nov 2000 10:17:10 -0500
From:      Vivek Khera <khera@kciLink.com>
To:        freebsd-stable@FreeBSD.org
Subject:   Re: periodic and 310.accounting 
Message-ID:  <14864.1526.589801.965839@onceler.kciLink.com>
In-Reply-To: <200011122305.eACN5jB05000@hak.lan.Awfulhak.org>
References:  <tim@futuresouth.com> <20001112161701.A10495@futuresouth.com> <200011122305.eACN5jB05000@hak.lan.Awfulhak.org>

next in thread | previous in thread | raw e-mail | index | archive | help
>>>>> "BS" == Brian Somers <brian@Awfulhak.org> writes:

BS> I think it'd be nice if newsyslog could be told to ``just rotate this 
BS> file with this number of backups with{,out} compression''.  It can be 
BS> given a file name, but still needs the config file to know when to 
BS> rotate....

Here's a script I've used on other systems to rotate logs on demand.
It can optionally send a signal and then optionally compress the file.

I call it "logrotate" ;-)


--cut here--
#! /usr/bin/perl
use strict;

# Time-stamp: "12 July 1999, 15:55:00 (khera@kciLink.com)"
# $Id: logrotate.perl,v 1.3 1999/07/12 19:55:16 khera Exp $

# rotate log files and optionally send a signal (default is HUP) to
# the specified process ID, then optionally compress the newly rotated
# log file.

# Copyright 1999 Vivek Khera <vivek@khera.org>

use constant USAGE => "usage:rotatelogs [-z] [-p PID] [-s SIG] -r N file...\n";

use IO::File;
use Getopt::Std;

my %opt = ();
getopts('zp:s:r:',\%opt);

scalar(@ARGV) or die USAGE;	# are there files specified?
die USAGE unless $opt{'r'};

# first rotate the files
foreach my $file (@ARGV) {
  rotate ($file,$opt{'r'});
}

# now send the signal, if necessary.  Default signal is HUP.
if ($opt{'p'}) {
  my $sig = $opt{'s'} || 'HUP';
  unless (kill $sig, $opt{'p'}) {
    warn "Failed to send $sig to $opt{'p'}\n";
  }
}

# and finally compress the newly rotated files, if requested
if ($opt{'z'}) {
  foreach my $file (@ARGV) {
    system "gzip $file.0";
  }
}


# rotate the named log file and archive $howmany old versions.  eg,
#   rotate("foo",3);
# will move foo -> foo.0 -> foo.1 -> foo.2 -> foo.3
# and remove old foo.3, then create empty foo.

sub rotate ($$) {
  my($file,$howmany) = @_;
  my($cur);

  return if ($howmany < 0);

  unlink ("$file.$howmany","$file.$howmany.gz"); # remove topmost one.

  for ($cur = $howmany; $cur > 0; $cur--) {
    my $prev = $cur - 1;
    rename("$file.$prev","$file.$cur") if (-f "$file.$prev");
    rename("$file.$prev.gz","$file.$cur.gz") if (-f "$file.$prev.gz");
  }

  rename("$file","$file.0");    # move original one

  # create the new one!
  my $fh = new IO::File $file, O_WRONLY|O_CREAT, 0644;
  $fh->close();
}
--cut here--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-stable" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?14864.1526.589801.965839>