FreeBSD - fuser checking processes that are using a filesystem
fuser -- list IDs of all processes that have one or more files open
man fuser:
Quote:
DESCRIPTION
The fuser utility shall write to stdout the process IDs of processes that
have one or more named files open. For block and character special
devices, all processes using files on that device are listed. A file is
considered open by a process if it was explicitly opened, is the working
directory, root directory, jail root directory, active executable text,
kernel trace file for that process or controlling tty of the process. If
-m option is specified, the fuser utility will search through mmapped
files also.
The following options are available:
-c Treat files as mount point and report on any files open in the
file system.
-f The report must be only for named files.
-k Send signal to reported processes (SIGKILL by default).
-m Report on mmapped files too.
-u Write the user name, associated with each process, to stdout.
-C Use given kernel core file instead of default /dev/kmem.
-K Use specified kernel image instead of the default one, which is
the image the system has booted from.
-s Use given signal name instead of default SIGKILL.
FreeBSD fuser utility will output on stderr some symbols for each pid. These symbols can be:
Quote:
The following symbols, written to stderr will indicate how files is used:
r The file is the root directory of the process.
c The file is the current workdir directory of the process.
j The file is the jail-root of the process.
t The file is the kernel tracing file for the process.
x The file is executable text of the process.
y The process use this file as its controlling tty.
m The file is mmapped.
w The file is open for writing.
a The file is open as append only (O_APPEND was specified).
d The process bypasses fs cache while writing to this file (O_DIRECT
was specified).
s Shared lock is hold.
e Exclusive lock is hold.
Finding processes using a filesystem:Code:
# fuser -c /storage
/storage: 12766c 13275c 13943c 14562c 16067c 32273c 33826c 35474c 35977c 59598c
As you can see
fuser -c displays a list of pids using a specified filesystem. But let's say we want to see the full command of those processes:
Code:
# ps alxw | grep -E "`fuser -c /storage 2>/dev/null | sed 's/.\(.*\)/\1/g;s/\ /|/g'`"
0 12766 1 0 96 0 3664 1784 select Ss ?? 0:04.65 screen
4016 13943 12766 56 8 0 3612 0 wait IWs p4 0:00.00 /usr/local/bin/bash --login /usr/local/bin/bash2
4016 33826 13943 0 5 0 3716 0 ttyin IW+ p4 0:00.00 /usr/local/bin/bash
4016 13275 12766 56 8 0 3612 0 wait IWs p3 0:00.00 /usr/local/bin/bash --login /usr/local/bin/bash2
4016 32273 13275 0 8 0 3720 0 wait IW p3 0:00.00 /usr/local/bin/bash
4016 59598 32273 64 104 0 115192 5404 - R+ p3 5:06.18 rtorrent
4016 14562 12766 60 8 0 3612 0 wait IWs p5 0:00.00 /usr/local/bin/bash --login /usr/local/bin/bash2
4016 35474 14562 0 5 0 3716 0 ttyin IW+ p5 0:00.00 /usr/local/bin/bash
4016 16067 12766 60 8 0 3612 0 wait IWs p6 0:00.00 /usr/local/bin/bash --login /usr/local/bin/bash2
4016 35977 16067 0 5 0 3716 0 ttyin IW+ p6 0:00.00 /usr/local/bin/bash
In the above command the symbols are stripped out from the output by sending
stderr to /dev/null, leaving pids separated by empty spaces. Next, the spaces are being replaced by pipes: "12766|13275|13943|14562|16067|32273|33826|35474|35977|59598". Then this string is given to
grep -E (Interpret PATTERN as an extended regular expression). The output of
ps is filtered for the PIDs from the
fuser command. The result is the full
ps output of the processes using a filesystem.
There are plenty of other methods of finding out the full command of each process from the output of the
fuser command, but they are not the scope of this post.