Log rotate log from standard out

logrotatestdout

I have a program which takes an argument to allow extra debug from a piece of hardware we use.

The log is just written to standard out so normally we just pipe the standard out to a file i.e. ./myprog -AddDebug > myfile.txt, however we are now required to keep this logging enabled to find a fault which we don't normally do so are looking for a way to rotate the logs to ensure the servers disk space doesn't get used up.

I've used the built in logrotate function in linux by adding a config file to /etc/logrotate.d/mytrace which contains the following:

/home/myprog/mytrace.txt
{
    rotate 5
    size 2M
    copytruncate
    olddir trace_archive
}

When I run /usr/sbin/logrotate /etc/logrotate.conf the trace file gets moved to the archvie however, the original is re-created but then obviously the pipe is lost so the file is no longer written to. I have then tried replacing create with copytruncate which still places the file in the archive but the original file just keeps growing.

A second problem, the file is only archived when I manually run the logrotate function it doesn't automatically fire when the file size is reached.

Any help you can provide on both problems would be appreciated.

UPDATE

I've managed to get it partly working, in order to get the copytruncate to work the file needs to be opened in append mode so instead of ./myprog -AddDebug > myfile.txt this should be changed to ./myprog -AddDebug >> myfile.txt.

My only problem now is, it will only do the rotation if I run logrotate -v /etc/logrotate.conf so how do I get this to be done automatically?

It looks as if I would have to manually create a cronjob that runs every few minutes to ensure that the log is rotated, its not handled automatically.

Thanks

Best Answer

Either simply pipe to | logger with appropriate flags and use syslog to manage your log file.

Alternative a nice helper program is rotatelogs included in the apache httpd distribution, which takes stdin and will write and rotate logfiles for you eg pipe your output to | /usr/sbin/rotatelogs /home/myprog/myproglog.%Y-%m-%d-%H_%M_%S 2M and create log files of 2 megabytes with unique sequential file names.