Linux Logrotate – How to Rotate Logs of a Dumb Non-Interactive Application

linuxlog-fileslogrotate

Ubuntu 10.4 Server.

I have a dumb non-interactive legacy service which is running constantly on my server.

It is writing its log to a file with fixed name (/var/log/something.log).

It does not handle any signals to let go of the log file. I need to rotate that log file.

Is there a way to do this properly without changing the application and without losing any data in the log?

Best Answer

Ignacio's answer intrigued me so I did some research and came up with the Perl script below. If your service will write to a named pipe it should work and be usable with logrotate.

For it to work you need to make your logfile into a named pipe. Rename the existing file then

mkfifo /var/log/something.log

and to edit the 3 filenames to meet your requirements. Run your service then this daemon which should read the named pipe and write it to a new logfile.

If you rename /var/log/somethingrotateable.log then send a HUP to the daemon it will spawn itself and create a new somethingrotateable.log to write to. If using logrotate a postrotate script of kill -HUP 'cat /var/run/yourpidfile.pid'

#!/usr/bin/perl -w
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions;
#
$|=1;
#
# Change the 3 filenames and paths below to meet your requirements.
#
my $FiFoFile = '/var/log/something.log';
my $LogFile = '/var/log/somethingrotateable.log';
my $PidFile = '/var/run/yourpidfile.pid';

# # make the daemon cross-platform, so exec always calls the script
# # itself with the right path, no matter how the script was invoked.
my $script = File::Basename::basename($0);
my $SELF = catfile $FindBin::Bin, $script;
#
# # POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new('sigHUP_handler',$sigset,&POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGHUP, $action);

sub sigHUP_handler {
#    print "Got SIGHUP";
    exec($SELF, @ARGV) or die "Couldn't restart: $!\n";
   }

#open the logfile to write to
open(LOGFILE, ">>$LogFile") or die "Can't open $LogFile";
open(PIDFILE, ">$PidFile") or die "Can't open PID File $PidFile";
print PIDFILE "$$\n";
close PIDFILE;
readLog();

sub readLog {
sysopen(FIFO, $FiFoFile,0)  or die "Can't open $FiFoFile";
while ( my $LogLine = <FIFO>) {
    print LOGFILE $LogLine;
   }
}