LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   fuser does not show files opened in vi (https://www.linuxquestions.org/questions/linux-general-1/fuser-does-not-show-files-opened-in-vi-764246/)

skiitd 10-24-2009 06:03 PM

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

clvic 10-25-2009 11:47 AM

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?

skiitd 10-26-2009 03:32 PM

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?

clvic 10-29-2009 01:45 PM

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

stickman 10-29-2009 08:54 PM

You might also look at using lsof. It's really good for determining what resources are being used by a process.

skiitd 10-30-2009 02:41 PM

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)

clvic 10-31-2009 06:47 PM

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...

unSpawn 11-01-2009 02:12 AM

Quote:

Originally Posted by skiitd (Post 3738348)
I eventually want to use fuser -k to kill the process;

Unless you have compelling reasons you shouldn't do that as it forces people to recover files. Any particular reason for doing it that can't be solved otherwise?


Quote:

Originally Posted by skiitd (Post 3738348)
with lsof i'll have to do more work

Not much. Here's a simple example for 'vim' (vi):
Code:

kill -15 $(lsof -Pwn -d3-10 -a -p $(pgrep vi) 2>/dev/null| awk '/vi/ {print $2}') # Yes, -15 will do.
but adding some checks and reporting would be better as unprvileged users shouldn't try to fsck up other users processes:
Code:

_killViBySwp() { # Find Vi PID, lsof, kill
do() { echo "${FUNCNAME}: ${VPROC}: ${VOPEN//.swp/}"; echo "kill -15 $VPID; kill -9 $VPID;" }
dont() { echo "${VPROC} is not owned by ${VUSER}."; }; /usr/sbin/lsof -Pwn -d3-10 -a -p `pgrep vi`\
|while read LINE; do LINE=(${LINE}); case "${LINE[0]}" in COMMAND) ;; vi|vim|gvim) VPROC=${LINE[0]};
VPID=${LINE[1]}; VUSER=${LINE[2]}; VOPEN=${LINE[$[${#LINE[@]}-1]]}; case "${LOGNAME}" in root) do;;
*) [ "${LOGNAME}" = "${VUSER}" ] && do || dont;; esac;; esac; done; # End _killViBySwp

* While it's your responsibility to understand foreign code before you run it I'll add that this code block is safe to run as it will only echo commands and not kill anything. To see the difference run 'vi /some/file' as unprivileged user then run the code as unprivileged user and as root.


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
but problems may arise when a same file has multiple recovery swaps so you prolly don't want to pursue this approach anyway.

skiitd 11-04-2009 04:00 AM

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.