2

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

2 Answers 2

3

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.

3
  • Thanks for the suggestion, I have got it almost working, I just have problem with it automatically running when the log file has exceeded the size limit
    – Boardy
    Feb 5, 2014 at 12:13
  • logger is the best solution, and I want to mention newsyslog that actually rotate syslog files.
    – Kondybas
    Feb 5, 2014 at 12:14
  • @Boardy as I read the logrotate man page it is designed to run from cron, typically as a daily cron-job. It should be safe to run multiple times a day, in that case it will only rotate log files where the rotation criterion is based on size and that size has been exceeded. It doesn't actively monitor the log file and doesn't trigger exactly at the moment your 2M has been exceeded.
    – HBruijn
    Feb 5, 2014 at 12:29
0

Thanks everyone for your help.

I have used the standard log rotate built into the OS and set up the cronjob to run every 5 minutes, and this appears to be working in the way that I wanted.

Thanks

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .