Closing open file without killing the process

Solution 1:

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

Solution 2:

Use the following command to find the deleted files file descriptors and you may truncate them afterwards

find /proc/ -mindepth 3 -maxdepth 3 \ -regex '/proc/[1-9][0-9]/fd/[1-9][0-9]' -type l -lname '*(deleted)' \ -printf '%p\n %l\n' 2>/dev/null