Tuesday, February 12, 2013

How to check if a file is being accessed by any process ?

Use 'lsof' command with the following syntax:
# lsof -f -- <path of file name>

Example:
linuxmach# lsof -f -- /var/log/secure
COMMAND  PID USER   FD   TYPE DEVICE  SIZE   NODE NAME
syslogd 3065 root    3w   REG  253,0 40558 169083 /var/log/secure
linuxmach#
The file '/var/log/secure' is being accessed by "syslogd" dameon with the PID 3065 and it is being written now.