1

Based on information for a given file test.txt, I hope to get a pid and its executable path for a process that is operating on that file.

For example, if from the command line someone has run this:

$ cat test.txt

...then I want have something that will give output similar to this:

process 1123 bin/cat is using /home/sam/test.txt
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
WB Lee
  • 641
  • 1
  • 6
  • 10
  • I think your question needs tidying up a bit but you are probably looking for something where 'lsof' is involved in the answer. I'll add it as a tag for you ;) – DanSut Oct 03 '15 at 02:37
  • yes, lsof works. Can I find the information in /proc/ directory? The information is which pid is executing the file? – WB Lee Oct 03 '15 at 02:57

1 Answers1

1

If the file is still open, you can use:

fuser /home/sam/test.txt
davejagoda
  • 2,420
  • 1
  • 20
  • 27
  • Yes, it works. Thanks! Is there any other methods? If I want to use some basic command, for example ls find grep etc to complete this function. Or can I get the information in /proc/ directory? – WB Lee Oct 03 '15 at 02:47
  • Please see what @DanSut wrote above. `fuser /home/sam/test.txt` will get you a PID. `lsof -p PID` will tell you about the process that has the file open. /proc/PID will have other info, but I think /proc is a Linux-only thing. – davejagoda Oct 03 '15 at 07:35
  • If requirement dose not allow me to use lsof and fuser, how can I do it. Thanks!!! – WB Lee Oct 03 '15 at 17:07
  • yes it is linux system. I cannot get the method, which makes me frustrated. – WB Lee Oct 03 '15 at 18:43
  • `file /proc/*/fd/* | grep /home/sam/test.txt` will get you the PID. `/proc/PID/cmdline` will contain the command. See this: http://stackoverflow.com/questions/821837/how-to-get-the-command-line-args-passed-to-a-running-process-on-unix-linux-syste – davejagoda Oct 05 '15 at 17:00