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.

There is a java process that makes logging to some file, e.g. err.log. I renamed it (w/o restarting java process), e.g. "mv err.log err.log0" and then see that java process continues logging to err.log0.

Why is that and what's logic behind it? Is it common for all unix/Linux platforms?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

The process will have opened it's log file when it started and will have kept it open. It will be writing to the file using the file descriptor that was created when the file was opened. The name of the file has no relationship with the file descriptor once it's opened so changing the name doesn't affect the process writing to it. This is normal and expected.

If you want your process to open a new log file then it will have to be written to accept a signal to tell it to close the old file and open a new one.

share|improve this answer
    
is it true both for unix and linux OS? –  yetanothercoder Apr 5 '11 at 11:02
1  
Yes it's the same for both. –  Iain Apr 5 '11 at 11:03

To add to that, you can actually have the file open with no filename at all. Say you deleted that file, the process will continue to write to it, and it will continue to take up space on the disk. When it's closes it will get deleted. You can also have the same file accessible by multiple filenames which you can create with the "ln" command. Opening and immediately deleting the directory entry for a file is a common way to make sure the space for a temporary file gets freed up when the process exits even if it crashes. The "-i" option to "ls" will include the inode number so you can tell which filenames go to the same file.

share|improve this answer

Just in case what you were trying to do was to rotate the logfile without restarting the Java process, here is a way that should work:

cp err.log err.log0
cat /dev/null > err.log

This actually copies the log and then truncates it (sets its size to zero) so you need extra disk space, but the app should continue logging to err.log afterwards.

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.