Many of the system administrator tend to miss the use to
fuser in linux, which in fact is a very helpful command. It can do the job which one can accomplish with the combining two or three different commands.
Lets start with locating the processes using a file mounted on certain mount point or processes accessing a certain mount point.
List processes using a file/filesystem/partitionQuote:
[root@gagan ~]# fuser -m /media/usb-drive
Where,
Code:
-m name specifies a file on a mounted file system or a block device that is mounted. All processes accessing files on that file system are listed. If a directory file is specified, it is automatically changed to name/. to use any file system that might be mounted on that directory.
Output:
Code:
/dev/sdc1: 6487c 6657c
So you can see that pid 6487 & 6657 are accessing the file/filesystem/paritition.
You can also specify the device name instead of mount point to locate the processes being access. So the above processes can also be located like below.
List processes using a deviceQuote:
[root@gagan ~]# fuser -m /dev/sdc1
Output:
Code:
/dev/sdc1: 6487c 6657c
You can use the
ps command to verify what the application/process is utilizing the device/partition.
You don't need to use the
kill command to terminate the process, you have the ability to kill all the processes accessing a partition/device using the fuser command itself.
Terminate all the processes accessing partition/device using fuserQuote:
[root@gagan ~]# fuser -km /dev/sdc1
You can also use interactive option to kill the processes
Quote:
[root@gagan ~]# fuser -kmi /dev/sdc1
Where,
Code:
-k to initiate the kill
-i to ask for verification
Output:
Code:
dev/sdc1: 6487c 6657c
Kill process 6487 ? (y/N) y
Kill process 6657 ? (y/N) y
You can also use the different signals for
fuser to terminate the processes
Quote:
[root@gagan ~]# fuser -kmi -TERM /dev/sdc1
The above commands can be used to kill all the processes accessing a partition allowing it to be unmount.
You can find the list of all the signals which can be used when passing the -k argument to fuserQuote:
[root@gagan ~]# fuser -l
Output:
Code:
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED
Display process information with username and PID using fuserQuote:
[root@gagan ~]# fuser -muv /dev/sdc1
Where,
Code:
-u display user IDs
-v verbose output
Output:
Code:
USER PID ACCESS COMMAND
/dev/sda6: root 6487 .rce. (root)cat
root 6657 .rc.. (root)top
Finding PIDs for a service using fuserYou can use fuser to find the process details for a service. The following an example:
Quote:
[root@gagan ~]# fuser 22/tcp
Output:
Code:
22/tcp: 2261
Or you can make the output more elaborative with the user information:
Quote:
[root@gagan ~]# fuser -u 22/tcp
Output:
Code:
22/tcp: 2261(root)
Or
Quote:
[root@gagan ~]# fuser -uv 22/tcp
Output:
Code:
USER PID ACCESS COMMAND
22/tcp: root 2261 F.... (root)sshd
The above result can also be accomplished using the following:
Quote:
[root@gagan ~]# fuser -nuv tcp 22
Where,
Code:
-n SPACE search in this name space (file, udp, or tcp)