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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-24-2009, 07:03 PM
|
#1
|
LQ Newbie
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6
Rep:
|
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
|
|
|
10-25-2009, 12:47 PM
|
#2
|
Member
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Rep:
|
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?
|
|
|
10-26-2009, 04:32 PM
|
#3
|
LQ Newbie
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6
Original Poster
Rep:
|
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?
|
|
|
10-29-2009, 02:45 PM
|
#4
|
Member
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Rep:
|
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
|
|
|
10-29-2009, 09:54 PM
|
#5
|
Senior Member
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552
Rep:
|
You might also look at using lsof. It's really good for determining what resources are being used by a process.
|
|
|
10-30-2009, 03:41 PM
|
#6
|
LQ Newbie
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6
Original Poster
Rep:
|
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)
|
|
|
10-31-2009, 07:47 PM
|
#7
|
Member
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Rep:
|
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...
|
|
|
11-01-2009, 03:12 AM
|
#8
|
Moderator
Registered: May 2001
Posts: 29,415
|
Quote:
Originally Posted by skiitd
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
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
|
|
|
11-04-2009, 05:00 AM
|
#9
|
LQ Newbie
Registered: Oct 2008
Distribution: RHEL-4
Posts: 6
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 03:52 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|