Re: Atomic log switch?



Jim Leonard <MobyGamer@xxxxxxxxx> wrote:
> I feel somewhat sheepish for asking this, because I feel I should know
> the answer, but is there a way to do a truly atomic logswitch? For
> example, typical novice admins do things like this:

> cp logfile logfile.old ; cat /dev/null > logfile

Are you asking only about non-signalable utilities? In most cases that
won't do anything.

For programs that keep the file open and take a signal to switch, you'd
do this...

mv logfile logfile.old; touch logfile; pkill -HUP <app>

Since the application itself is handling the switch, it's completely
safe.

> ...to switch them. However, that operation isn't atomic; for an
> application still writing to the file, information could be lost during
> the copy, or during the command-line parsing between both commands.

If you can't signall it to do the switch, you're more limited.

> I seem to remember a way to do this atomically using hard links (ln)...
> something like changing the inode a filename refers to in a single
> operation.

Doesn't help here. You can't do anything to the actual inode because
the program has it open and won't change. So even if you made a link to
it, you couldn't do anything special with that link.

> Does anyone know of this procedure (or a better one?)

You can stop the application while you do the copy...

pkill -STOP <app> ; cp logfile logfile.old ; cat /dev/null > logfile ;
pkill -CONT <app>

Now you'll still be getting sparse files this way, and using 'cp' is
going to copy all those spaces and use real disk space for it. Maybe
some other copy method would be better.

--
Darren Dunham ddunham@xxxxxxxx
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
.



Relevant Pages