Linux – Closing open file without killing the process

gdbjavalinuxlsofprocess

I have java(yeah java…) application running on CentOS 7.
After a while, there are many "deleted" files that bother me.

for deleted files used(not the issue):

lsof | grep "(deleted)"

I found them in /proc/pid/fd/… and my question is how can I kill/delete them without killing the process(process have to run 24/7).

I saw on google that I can use gdb tool, but I don't know how to use it.
Can you please help me(just to write step-by-step manual)?

I will love to hear some other suggestions if you have.

Best Answer

use lsof -p $PID and find the file descriptor (4th column)

root@blah:~# lsof -p 1737 | grep "(deleted)" apache2 1737 root 6w REG 0,25 0 207401 (deleted)/var/log/apache2/other_vhosts_access.log

4th column is 6w, meaning file descriptor 6 and it was opened for writing (w).

Then:

gdb -p $PID p close($FD)

eg:

gdb -p 1737 ..... (gdb) p close(6) $1 = 0 ... Quit anyway? (y or n) y Detaching from program: /usr/lib/apache2/mpm-prefork/apache2, process 1737