How to check if a file is being used by any process in Linux?

There is a file I want to edit/delete but unable to do it because it says file is being used by process. That file was created  when an application was running, but I would like to know if that process has already finished writing into the file,


or if the file is now basically finished writing. Without knowing anything about the original process, what command can I use to check if it's safe to start reading the data from the file without the danger of it being incomplete?

To find and check if file is being used by any process we can use command called lsof

Use 'lsof' command with the following syntax:

# lsof -f -- <path of file name>

Example:

root@TecGeeks:/var/log# lsof -f -- /usr/bin/ssh-agent
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh-agent 7299 tecgeeks txt REG 252,0 358624 797238 /usr/bin/ssh-agent

The file '/usr/bin/ssh-agent' is being accessed by "tecgeeks" with the PID 7299 and it is being written now.
How to check if a file is being used by any process in Linux? How to check if a file is being used by any process in Linux? Reviewed by Unknown on April 28, 2017 Rating: 5

No comments:

Get Your Business Online Now


Powered by Blogger.