Is there any locking on the content, imposed by the process if at all the process is using the file.

We host a JBoss app server on a RHEL 6.4 OS and due to the disk space constraints we have decided to remove the log files it generated till date.

However, if we delete the log file which is being used there could be few issues with the logging in jboss which eventually leads to the restart of Jboss App Server instance.

We we have decided to nullify the server.log which is being dynamically written by the JBoss app server process (checked with the fuser command) by using the below commands:

-- > server.log
-- echo "" > server.log

None of the above commands worked. The size of the files still shows 500+ MB

How do I remove the contents of the file without deleting the file?


2 Answers 2


Linux only has cooperative locks. Nothing will prevent a process from opening a file that it has permission to open. You would need to call a locking mechanism in all processes using the file to prevent concurrent access. However, what you're trying to do doesn't have anything to do with concurrent access, as far as I can see.

If you remove a file that is open in a process, the file isn't actually removed: only its name is removed. The file itself is actually removed only when it no longer has any name (zero hard link) and it is no longer open. So if you run rm server.log while the JBoss application still has it open, that won't free any disk space until the JBoss application is restarted (or closes the log file for some other reason).

If you run > server.log or echo "" > server.log, that does truncate the file to 0 or 1 bytes respectively, so the disk space is freed. If the JBoss application was writing to the file, it will keep writing at the same position, 500+MB into the file. This results in a sparse file, where the file contains null bytes up to the position where the application started writing to it. For example, if the file was 500MB when you truncated it, and the application has kept writing 1MB, then the file size is 501MB, but the disk space occupied by the file is only 1MB, and the first 500MB of the file are null bytes.

If you want the application to write at the beginning of the file, you'll have to tell it to do that. Typically, you'd tell it to close its current log file and start a new one. As grebneke commented: see How to clear server.log in JBoss?


You can lock the file explicitly by the process (and own the lock - others can then wait for release or give up).

For starters, check out flock and lockf. File locks can even address ranges inside the files.

A link that also addresses some problems with multithreading and attempted solutions.



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .