fuser
"fuser - identify processes using files or sockets" - man fuser
This command is not directly related to the FUSE command fusermount
.
The output here is a bit unusual in that it sends PIDs to stdout and everything else to stderr, but interleaves them so what you see in the terminal is much different from what you get via pipes.
Examples
Show what is using /var/log
$ fuser -m /var/log
/var/log: 2858m 4608rce 4609rce 4749rce
See man fuser
for the meaning of each letter.
But what you get via a pipe is just the pids. The first line of output is all of stderr, and beyond that is stdout.
$ fuser -m /var/log | xargs -n1 echo
/var/log: mrcercerce
2858
4608
4609
4749
Kill all processes accessing a given file
FILE="/path/to/somefile.log"
fuser -km "$FILE"
Show processes accessing the local ssh port
This only works for local ports.
## By service/protocol (see /etc/services)
sudo fuser ssh/tcp
## By arbitrary port/protocol
sudo fuser 22/tcp
Check if a file is being accessed
FILE="/path/to/somefile.vmdk
fuser "$FILE" && echo "Yes, $FILE is being used."