[comp.unix] how to see which files are open?

paul@unhtel.uucp (Paul S. Sawyer) (02/23/90)

> I found myself wanting to umount a filesystem which as far as I could
> see no one was in (nor in another directory under it) but nevertheless,
> every time I tried to umount it I got "Device busy".  This was
> frustrating.  There must be a way to see what processes have which
> files open but nothing sprang to my eye when I did a 'man -k' of "files",
> "open", and "table".  Will someone please tell me what it is?  Please post
> (this is said in hopes of keeping the responses in the hundreds instead
> of the thousands).  Thanks!

On System V, it's /etc/fuser - - I don't know if there is a BSD equivalent.

You must be able to read /dev/kmem and /dev/mem, which means user root or 
group sys (on this system, anyway);  "/etc/fuser -u /dev/dsk/0s1" works, 
while "/etc/fuser -u /usr" does not seem to on this system, even though
TFM seems to indicate that it should.  (SysV.2.1)

A -k option will even kill those processes that are preventing the umount.


> -- 
> cole@unix.sri.com
> {hplabs,rutgers}!sri-unix!cole
-- 
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Paul S. Sawyer              uunet!unh!unhtel!paul     paul@unhtel.UUCP
UNH Telecommunications        attmail!psawyer       p_sawyer@UNHH.BITNET
Durham, NH  03824-3523      VOX: 603-862-3262         FAX: 603-862-2030

C.J.Parkin-Lilley@fulcrum.bt.co.uk (Chris Parkin Lilley [cs12]) (02/23/90)

In article <3537@zorba.Tynan.COM> uunet!unix.sri.com!cole (Susan Cole) writes:
>I found myself wanting to umount a filesystem which as far as I could
>see no one was in (nor in another directory under it) but nevertheless,
>every time I tried to umount it I got "Device busy".  This was
>frustrating.  There must be a way to see what processes have which
>files open but nothing sprang to my eye when I did a 'man -k' of "files",
>"open", and "table".  Will someone please tell me what it is?  Please post
>(this is said in hopes of keeping the responses in the hundreds instead
>of the thousands).  Thanks!
>

Does your system have 'fuser'. If so, you should be able to do something like

	fuser -u /dev/dsk/whatever (a block special file)

This should give you the pids of the processes which have a partition open,
and 
	fuser -k /dev/dsk/whatever

should try and kill them off.

Hope this is OK. Of course, if you don't have fuser, then it won't be any help.
Good luck.
	-Chrisl.

--
--------------------------------------------------------------------------------
Chris Parkin Lilley, CS1.2, BT Fulcrum, UK               chrisl@uk.co.bt.fulcrum

bei@puzzle.UUCP (Bob.Izenberg) (02/23/90)

In article <3537@zorba.Tynan.COM> uunet!unix.sri.com!cole (Susan Cole) writes:
>frustrating.  There must be a way to see what processes have which
>files open...

I don't have the man page for fuser, but here's some of what the book says:
(sharp signs inserted for safety's sake)
# fuser - identify processes using a file or file structure

# /etc/fuser -ku (files) (-) (-ku) (files)

For block special devices, all processes using any file on that device are
listed.
If the -k option is specified, the SIGKILL signal is sent to each process.
(If you have permission to kill it.)
If the -u option is specified, the login name, in parentheses, also follows
the process id.
So, try this:
# su
# /etc/fuser -ku /dev/dsk/c1t1d1s8
# umount /dev/dsk/c1t1d1s8

The fine print qualifies the man page thusly:
The command fuser works with a snapshot of the system table, which is
true only for an instant.  It is possible that other processes begin
accessing the specified file(s) after this snapshot is taken.
Does your machine have fuser?
-- Bob
-- 
 ------------------------------------------------------------------------------
                     Bob Izenberg [ ] Ralph Kirkley Associates
              attctc!puzzle!bei    512 346 7019 (home)    Austin, TX
 ------------------------------------------------------------------------------

amoss@batata.huji.ac.il (amos shapira) (02/23/90)

Hello,
	There is a utility writen by a local guru that will do just that,
	you give it a file, a directory or a file system and it will give
	you back which process opened this file/have this directory as the
	current directory or deals with this file system, I'll try to post
	it if there is demand (800+ lines).

Cheers,
- Amos Shapira

amoss@batata.bitnet
amoss@batata.huji.ac.il

abe@mace.cc.purdue.edu (Vic Abell) (03/08/90)

In article <3549@zorba.Tynan.COM> paul@unhtel.uucp (Paul S. Sawyer) writes:
>>               There must be a way to see what processes have which
>> files open but nothing sprang to my eye when I did a 'man -k' of "files",
>> "open", and "table".

Try fstat or ofiles from comp.sources.unix.  Fstat is number 56 in volume
18; ofiles is number 57 in volume 18.  Both require access to /dev/mem, so
they must be installed set[gu]id or run from root.

fk@kos.rci.dk (Fleming Kraglund) (03/08/90)

C.J.Parkin-Lilley@fulcrum.bt.co.uk (Chris Parkin Lilley [cs12]) writes:

>In article <3537@zorba.Tynan.COM> uunet!unix.sri.com!cole (Susan Cole) writes:
>>I found myself wanting to umount a filesystem which as far as I could
>>see no one was in (nor in another directory under it) but nevertheless,
>>every time I tried to umount it I got "Device busy".  This was

>Does your system have 'fuser'. If so, you should be able to do something like
>	fuser -u /dev/dsk/whatever (a block special file)

>This should give you the pids of the processes which have a partition open,
>and 
>	fuser -k /dev/dsk/whatever

>should try and kill them off.

Please note that if you are using process accounting (assuming sysV 3.x)
then the kernel has an open file on /usr, (yes the kernel, it's the inode
on which to write the account records) and you must do a
	/usr/lib/acct/shutacct.
Hope this helps too.
-- 
                             fk@rci.dk                   (I hate .sig files)