Fuser
is a utility available in Linux which shows what processes are using
a file ,file system and socket. The output of "fuser" may
be useful in diagnosing "resource busy" messages arising
when attempting to unmount file systems.
Check
Process That are currently using the Current Directory
[root@vx111a
test]# fuser .
.:
3394c 3806c 3807c
[root@vx111a
test]# ps ux | grep vi
root
3540 0.0 0.5 63080 17732 ? Sl 15:44 0:00
/usr/lib/nspluginwrapper/npviewer.bin --plugin
/usr/lib/mozilla/plugins/libflashplayer.so --connection
/org/wrapper/NSPlugins/libflashplayer.so/3468-1
root
3806 0.0 0.0 5032 1460 pts/1 S+ 15:57 0:00 vi test
root
3825 0.0 0.0 4016 684 pts/2 S+ 15:57 0:00 grep vi
It
gives us all the process ID that are currently using the current
directory. You can see the 'vi test' for the current directory is
opened. Hence the Pid 3806.
But
when we observe that every process given in the output contains c
after them (3806c).These characters indicate the type of access. The
type of access can be any one of the following:
*
c current directory (Uses the file as the current directory)
*
e executable being run (Uses the file as a program's executable
object)
Detailed
Report
[root@vx111a
test]# fuser -v .
omhq196f:dwls977-~
$ /sbin/fuser -v .
USER
PID ACCESS COMMAND
.:
root001 6227 ..c.. bash
root001
9839 ..c.. syslog
Fuser
On Executable
If
we need to find details of who is using an executable, we can use
[root@vx111a
test]# fuser -v top.sh
USER
PID ACCESS COMMAND
top.sh:
root 3942 f.... top.sh
The
Access Says it is 'f' (open File) , if you are running a executable
program you can see the access will change (Access to 'e').
Process
Using TCP/IP sockets
One
of the another advantage of using fuser is it can find which process
are using Tcp/Ip scokets like ,
localhost:root-~
$ fuser -v -n tcp 7080
USER
PID ACCESS COMMAND
7080/tcp:
root 17077 F.... java
Check
For Process Currently using the File System
[root@vx111a
soa]# fuser -m /dev/sda7
/dev/sda7:
3807c 4060c 4061c
Find
Owner of the Process
[root@vx111a
test]# fuser -uv top.sh
USER
PID ACCESS COMMAND
top.sh:
root 4226 f.... (root)top.sh
Kill
All process that are currently using a program
Fuser
allows to kill all the process that are currently using a specific
program. We can use
[root@vx111a
test]# fuser top.sh
top.sh:
4158
[root@vx111a
test]# ps ux | grep top
root
4158 0.0 0.0 4576 1000 pts/1 S+ 16:30 0:00 /bin/bash
./top.sh
root
4180 0.0 0.0 4016 688 pts/2 S+ 16:31 0:00 grep top
There
is another top.sh process running
USER
interactively kill the process. This can be done by using
fuser
-v -k -i top.sh
This
will show all the process that are currently using the top.sh and
asks whether to kill that process or not.
fuser
-v -k -i top.sh
This
will show all the process that are currently using the top.sh and
asks whether to kill that process or not.
The
'-k' with fuser sends a SIGKILL signal to the processes using that
particular file or executable. We can use the option -SIGNAL to send
any other signal. The list of signals supported by fuser is given by:
$
fuser -l
HUP
INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
To
list the process numbers of local processes using the /etc/passwd
file, enter:
fuser
/etc/passwd
To
list the process numbers and user login names of processes
usdana","sans-serif"; font-size: 9.0pt; line-height:
115%; mso-bidi-font-size: 11.0pt;">$ fuser -v -k -STOP -i ./
Some
More Examples
To
list the process numbers of local processes using the /etc/passwd
file, enter:
fuser
/etc/passwd
To
list the process numbers and user login names of processes using the
/etc/filesystems file, enter:
fuser
-u /etc/filesystems
To
list all processes that are using a file which has been deleted from
a given file system, enter:
fuser
-d /usr
fuser
-kxuc /home
Either
command lists the process number and user name, and then terminates
each process that is using the /dev/hd1 (/home) file system. Only the
root user can terminate processes that belong to another user. You
might want to use this command if you are trying to unmount the
/dev/hd1 file system and a process that is accessing the /dev/hd1
file system prevents this.
To
list all processes that are using a file which has been deleted from
a given file system, enter:
fuser
-d /usr
Display
Information about multiple files
fuser
-a top.sh sam
Show
what PID is listening on the Specified Port
fuser
-v 7080/tcp
kill
a process when knowing only the port where the process is running
fuser
-k
Display
which user run process from given port name
localhost:root-~
$ /sbin/fuser -nu tcp 7080
7080/tcp:
13077(root)
Find
out what is listening on a series of ports
localhost:root-~
$ /sbin/fuser -n tcp {7000..8000}
7080/tcp:
13077
7443/tcp:
13077
7444/tcp:
13077
7445/tcp:
13077
list
and kill any processes currently using a File System
fuser
-vmk /sam
determine
if tcp port is open
fuser
-n tcp -s 7080 && echo "open"
More
To Come , Happy learning :-)