[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: daemon vs. savelog ???



On Tue, Jul 09, 2002 at 05:54:21PM -0500, Michael D. Schleif wrote:
[psuedo-daemon doesn't reopen log file after rotation]
|
| How do ``real'' daemon programs get around this issue?  Is it the kill
| -HUP $PID trick?  I've never been too comfortable with signals . . .

The issue is that in UNIX, once a file is open the name no longer
matters.  The program has "file descriptor", which is an integer that
indexes the open files table.  That table has in it a reference to the
inode where the file starts (and probably some other data like the
current location in the file).

Now you run logrotate (or whatever, it doesn't matter) to rename the
file, but any running program is still connected to the original
inode.  What you want is for the program to close the file it is
currently logging to and (re)open the log.  Since the name is used
when opening a file,  the daemon will now be attached to the new inode
with that name.

Depending on the deamon's implementation, there are three ways of doing
this.

One way is to kill and restart the daemon in logrotate's
'postrotate' section.  This isn't terribly pleasant, but works for all
daemons (that can handle being restarted).

The second way is for the daemon to have a signal handler that will
gracefully reopen the logfiles.  Then in logrotate's postrotate, you
simply send it the appropriate signal (SIGHUP is commonly used).  This
rquires support in the daemon.

The third way is to use the 'copytruncate' option in logrotate.  This
tells logrotate that the daemon isn't very well-behaved and won't be
able to reopen the log files once they're rotated.  To work around it,
logrotate will copy (instead of renaming) the existing log to the new
name.  Then it will truncate the existing log to zero size.  The
effect (besides performance degredation and potential race conditions)
is that the "old" inode still refers to the current log file and a new
one has been allocated for the old log.

HTH,
-D

-- 
 
He who belongs to God hears what God says.  The reason you do not hear
is that you do not belong to God.
        John 8:47
 
http://dman.ddts.net/~dman/

Attachment: pgpOzREl2qIEH.pgp
Description: PGP signature


Reply to: