Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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.

share|improve this answer
    
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 at 12:13
    
logger is the best solution, and I want to mention newsyslog that actually rotate syslog files. –  Kondybas Feb 5 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 at 12:29
up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.