fuser does not show files opened in vi
hi,
I've a file "abc" which I open with vi, gedit and also with sleep (sleep 1000 < abc). But fuser -v abc doesn't show vi,gedit though it shows pid of sleep. But if I run fuser -vm DIR_NAME it lists all three which means DIR_NAME is used by these 3 processes. Why doesn't fuser abc show vi and gedit? Is it because vi may not be using "abc" and is rather working on .swp file. But what about gedit? I don't see any temp file for gedit. Or should fuser abc show 3 pids? I'm using ubuntu 9.04. I tried as root as well but got same results. Thanks, Surender |
Most probably, gedit does not open a .swp file but simply opens the file in memory and closes it, on load. Then, when saving, it can open it again and close it after save.
What's the aim of your fuser usage? Knowing when a file is saved? Or what? |
My purpose is to know if a file is already in use by any process and also the pid.
Let's assume that gedit creates a tmp file in-memory. I am not sure then that fuser DIR_NAME should list it. I know ps could help do the same in some way, but i wish if fuser can help by plain "fuser -v abc". Am i missing something here? |
I made an experiment. If I create a file, and then open it with "less", fuser currently reports that "less" has the file open. If I open the file with gedit, fuser does not show anything.
So, gedit does not keep the file open, but most probably retains a copy of it. I don't really understand the other things... could you explain better? Anyway, if other programs do not keep the file open, and you want to prevent these files to be written by someone, use UNIX file locking functions |
You might also look at using lsof. It's really good for determining what resources are being used by a process.
|
thanks for your replies guys!
@clvic, what I meant was if gedit doesn't use the original file (and just keeps a copy in memory) then why does 'fuser' on the directory shows the gedit process. @stickman, I eventually want to use fuser -k to kill the process; with lsof i'll have to do more work :-) (not that i dislike work) |
I think that, if gedit has that directory as current directory, then fuser marks gedit as "using" that directory.
That is, gedit it has been launched from that directory, or as a second possibility, gedit called the system call "chdir" to change to that... |
Quote:
Quote:
Code:
kill -15 $(lsof -Pwn -d3-10 -a -p $(pgrep vi) 2>/dev/null| awk '/vi/ {print $2}') # Yes, -15 will do. Code:
_killViBySwp() { # Find Vi PID, lsof, kill BTW 'vi' also has "-r" to list recovery files, however this requires you to find its CWD first: Code:
pgrep vi | while read VPID; do cd $(readlink -f /proc/${VPID}/cwd 2>/dev/null) && vi -r; done |
Thanks unSpawn for that elaborate reply :-)
But for academic satisfaction, the real answer to 'fuser' still eludes me. |
All times are GMT -5. The time now is 07:41 PM. |