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
Docker-Compose Up and User Inputs on Stdin
How to Monitor the Thread Count of a Process on Linux
Run Script with Rc.Local: Script Works, But Not at Boot
How to Compile and Link a 32-Bit Windows Executable Using Mingw-W64
How to Use Gdb in Eclipse for C/C++ Debugging
Http Post and Get Using Curl in Linux
How to Delete All Lines in a File Starting from After a Matching Line
Sed with Literal String--Not Input File
How to Escape Colon (:) in $Path on Unix
Insert Multiple Lines into a File After Specified Pattern Using Shell Script
Running 32 Bit Assembly Code on a 64 Bit Linux & 64 Bit Processor:Explain the Anomaly
How to Run Crontab Job Every Week on Sunday
Switching Users Inside Docker Image to a Non-Root User
Linux Shell Sort File According to the Second Column
How to Convert Linux 32-Bit Gcc Inline Assembly to 64-Bit Code