LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-24-2009, 07:03 PM   #1
skiitd
LQ Newbie
 
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6

Rep: Reputation: 0
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
 
Old 10-25-2009, 12:47 PM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
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?
 
Old 10-26-2009, 04:32 PM   #3
skiitd
LQ Newbie
 
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 10-29-2009, 02:45 PM   #4
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
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
 
Old 10-29-2009, 09:54 PM   #5
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
You might also look at using lsof. It's really good for determining what resources are being used by a process.
 
Old 10-30-2009, 03:41 PM   #6
skiitd
LQ Newbie
 
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6

Original Poster
Rep: Reputation: 0
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)
 
Old 10-31-2009, 07:47 PM   #7
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
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...
 
Old 11-01-2009, 03:12 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
Quote:
Originally Posted by skiitd View Post
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 View Post
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.

Last edited by unSpawn; 11-01-2009 at 03:16 AM. Reason: //more *is* more
 
Old 11-04-2009, 05:00 AM   #9
skiitd
LQ Newbie
 
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks unSpawn for that elaborate reply :-)
But for academic satisfaction, the real answer to 'fuser' still eludes me.

Last edited by skiitd; 12-17-2009 at 07:14 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Increase the no of Files opened titusabraham Linux - Newbie 1 10-03-2009 05:00 PM
fuser /dev/dsp show nothing. but no way to play audio ... it happens after some time masakrator Linux - Desktop 2 01-15-2009 03:51 PM
why .php files cant be opened in browser...... jestinjoy Programming 5 05-24-2008 03:38 PM
how to know the files and directories which are opened? sridhar_yerram Linux - Newbie 1 01-11-2008 03:59 AM
apache logs files can't be opened? benrose111488 Linux - Software 14 06-26-2007 01:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration