Detailed Explanation of the Usage of fuser Command in Linux

  • 2021-08-12 04:21:56
  • OfStack

Description:

fuser shows which program is currently using a file on disk, a mount point, or even a network port, and gives details of the program's progress.

fuser displays the process ID that uses the specified file or file system.

By default, each file name is followed by a letter to indicate the access type.

In the zkfc log, there is 1 warn: PATH = $PATH:/sbin:/usr/sbin fuser-v-k-n tcp 8090 via ssh: bash: fuser: No command found

The reason is that there is no fuser command when minimizing the installation of centos

yum install -y psmisc

Syntax:

fuser (option) (parameter)

The access types are as follows:

c: Represents the current directory
e: Use this file as an executable object for your program
f: Open file. Not displayed by default.
F: Open file for write operation. Not displayed by default.
r: Indicates that this directory is the root of the process.
m: Indicates whether the process uses the file for memory mapping or whether the file is a shared library file mapped into memory by the process.
s: Use this file as a shared library (or other loadable object)

Common options

-a: Displays all files specified on the command line. By default, the files that are accessed will be displayed.
-c: and-m1-like for POSIX compatibility.
-k: Kill the process accessing the file. If-signal is not specified, an SIGKILL signal is sent.
-i: Ask the user before killing the process. If there is no-k, this option will be ignored.
-l: Lists all known signal names.
-m: name specifies a file on the mount file system or a block device to be mounted (name name). In this way, all processes accessing this file or file system will be listed. If you specify a directory, it automatically converts to "name/" and uses all file systems mounted under that directory.
-n: space specifies a different namespace (space). Different space files (filename, default here), tcp (local tcp port), udp (local udp port) are supported here. For ports, you can specify a port number or name, and if there is no ambiguity, you can use a simple representation, such as name/space (that is, a representation like 80/tcp).
-s: Silent mode, in which case-u,-v will be ignored. -a cannot be used with-s1.
-signal: Use the specified signal instead of SIGKILL to kill the process. Signals can be represented by name or number (e.g.-HUP,-1). This option should be used with-k1, otherwise it will be ignored.
-u: Add the user name of the process owner after each PID.
-v: Verbose mode. Outputs output similar to the ps command, including PID, USER, COMMAND and many other domains. PID is kernel if accessed by the kernel.-V outputs version number.
-4: Use the IPV4 socket, cannot be applied with-61, and is not ignored only when the names of tcp and udp of-n exist.
-6: Use the IPV6 socket, cannot be applied with-41, and is not ignored only when the names of tcp and udp of-n exist.
-Reset all options and set the signal to SIGKILL.

Parameter

File: It can be a file name or TCP or UDP port number.

Use example:

Displays process information using a file

This command is useful with umount, and you can find out what else is using this device.


$ fuser -um /dev/sda2 
/dev/sda2:      6378c(quietheart) 6534c(quietheart) 6628(quietheart) 
6653c(quietheart) 7429c(quietheart) 7549c(quietheart) 7608c(quietheart) 

Kill the program that opens the readme file

Here, you will be asked if you are sure before kill. It is better to add-v to know which process is going to be killed.

$fuser -m -k -i readme

See which programs use port 80 of tcp

$fuser-v-n tcp 80 or $fuser-v 80/tcp

Application of Different Signals of fuser

The signals known to fuser can be listed with the-l parameter


[root@_mongodb_117 ~]# fuser -l
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

The fuser can send a known signal to the accessing specified file process instead of the SIGKILL sent by default by the-k parameter, for example, if the process is only suspended, then send the HUP signal will do

[root@_mongodb_117 ~]# fuser -v /root/install.log

User process number permission command


 /root/install.log:  root    3347 f.... tail
[root@_mongodb_117 ~]# fuser -k -SIGHUP /root/install.log
 /root/install.log:  3347
[root@_mongodb_117 ~]# fuser -v /root/install.log

To list the process numbers of local processes that use the/etc/passwd file, enter:

fuser /etc/passwd

To list the process number and user login name of the process using the/etc/filesystems file, enter:

fuser -u /etc/filesystems

Summarize


Related articles: