Discussion:
Rotating a nohup.out file whitout stopping the app
(too old to reply)
igor
2004-07-07 23:17:09 UTC
Permalink
I am looking for ideas on how to solve a problem with a nohup.out file
that is growing faster than expected.

While we change to a better logging mechanism we're using the stdout
to dump a bunch of debugging messages (a bunch is actually a lot!!).
Any hows, when the application runs for a few days the nohup.out file
could be 1G in size and to search for something on that file becomes a
pain in the neck.

The way I am doing that is pretty much manually. Stop the app, move
the file out and restart the app (using nohup).

I don't know if any of you guys know a better way to either rotate the
nohup.out (by date or size, don't care) or to redirect the stdout to a
logging application.

I have been looking around in the groups and on the net but can't find
anything that will give me some lights on how to do it.

Ciao,
Igor
Jonathan Kamens
2004-07-08 00:46:00 UTC
Permalink
If you use shell output redirection to send the output of an
application to a file, you can't rotate out that file and expect the
application to start writing to a new one.

You might consider looking at "logger", which will read input on stdin
and log it to the system log; then you can have logrotate or some
similar utility rotate the log periodically or when it reaches a
certain size (you can run logrotate periodically out of cron or run it
by hand when you want the file to rotate.

Another option would be to write a simple script to change it output
file whenever it has reached a certain size. Off the top of my head
(untested)....

In shell:

#!/bin/sh

# Read input until killed and redirect it to the file name specified
# on the command line with a suffix indicating the file number. No
# error checking.

FILENO=1
FILENAME=$1
SIZEMB=100

while true; do
dd bs=1MB count=$SIZEMB of=$FILENAME.$FILENO
FILENO=`expr $FILENO + 1`
done

In Perl:

#!/usr/bin/perl

$fileno = 1;
($filename = shift) || die;
$size_mb = 100;
$so_far = 0;

open(STDOUT, '>', "$filename.$fileno") || die;

$size = $size_mb * 1024 * 1024;

while (<>) {
$so_far += length;
print || die;
if ($so_far > $size) {
close(STDOUT) || die;
$fileno++;
open(STDOUT, '>', "$filename.$fileno") || die;
}
print || die;
}

close(STDOUT);
Tim Smith
2004-07-18 20:15:47 UTC
Permalink
The general thing I do with log files that I will need to rotate, if the
program or script that generates them supports writing to a pipe, is pipe
them to a program that does this:

1. Open the log file in append mode
2. Read a line on stdin
3. Lock the log file
4. Write the line
5. Unlock the log file
6. goto #2

I then have another program that basically does this:

1. Open the log file in read mode
2. Lock the log file
3. Dump the log file to stdout
4. Use ftruncate to truncate the log to 0 bytes
5. Unlock the log file
--
--Tim Smith
Loading...