How Find Out Which Process Is Using a File in Linux

How find out which process is using a file in Linux?

You can use the fuser command, like:

fuser file_name

You will receive a list of processes using the file.

You can use different flags with it, in order to receive a more detailed output.

You can find more info in the fuser's Wikipedia article, or in the man pages.

how to know which processes accessed a file?

The linux audit system can help you and will provide detailed information:

Here's some documentation on Redhat's site, but should be adaptable to other linux variants. Most distros have the audit system but may be an optional install.

Assuming the audit subsystem is already running, you can add a rule to watch read access on your example file like this:

auditctl -w /etc/AAA -p r -k mywatch

Then you can see the results with the comamnd:

ausearch -k mywatch

or watch the audit.log file (in /var/log/audit on some systems)

How do I find out what process has a lock on a file in Linux?

Use lsof to find out what has what files are open.

man lsof or have a look here

Find out if process / file is running linux

"Determine the PID of a file I specify."

lsof | grep <file> | awk '{print $2}'

"Get the filename if I type the PID."

lsof | grep <PID>
lsof | grep <PID> | awk '{print $NF}'

"Get all the running processes PIDs in a file to work with that in a later script."

ps x | awk '{print $1}' > pid-list.txt # PIDs of all processes run by current user
ps ax | awk '{print $1}' > pid-list.txt # PIDs of all processes run by all users

how to find what processes have written to a file on Linux

Create a small monitoring process which will log periodically who is currently accessing the file.

You can write a small script using fuser. Is here a quick example (to be improved)

#!/bin/bash

log=~/file-access.log

while true
do
fuser your_file >> $log
sleep 0.2s
done

But you will have to be lucky that the process writing to this file takes enough time to have the chance to detect it with fuser.



Related Topics



Leave a reply



Submit