linux fuser命令_带有示例Linux fuser命令教程

linux fuser命令

linux fuser命令

Linux have a lot of tools to manage processes and network. But fuser is one of the most popular tool which combines process and network management in a single command. fuser can display process according to their directory, file and network handles. Also it can kill these process according to various parameter.

Linux有很多工具来管理进程和网络。 但是, fuser是最流行的工具之一,它在单个命令中将过程和网络管理结合在一起。 fuser可以根据其目录,文件和网络句柄显示进程。 也可以根据各种参数杀死这些进程。

句法 (Syntax)

fuser [options] [file|socket]

帮帮我 (Help)

$ fuser -h
Help
Help
帮帮我

使用目录显示进程(Display Processes Using Directory)

In daily usage of a server there will be a lot of users and processes. Finding related process about opened directory can be hard. fuser command provides feature simply find related processes according to directory usage. In the example we will provide the directory path we want to inspect which is /home/ismail . Best way to use fuser is with root privileges

在服务器的日常使用中,会有很多用户和进程。 查找有关打开目录的相关过程可能很困难。 fuser命令提供的功能只需根据目录使用情况查找相关进程即可。 在示例中,我们将提供要检查的目录路径/home/ismail 。 使用fuser最佳方法是具有root特权

$ fuser /home/ismail
Display Processes Using Directory
Display Processes Using Directory
使用目录显示进程

From output we can see that some process ID are printed. There is also some characters after PIDs. These are used to specify access type and information about the process. Here is what they mean

从输出中,我们可以看到打印了一些进程ID。 PID之后还有一些字符。 这些用于指定访问类型和有关该过程的信息。 这就是他们的意思

  • c – current directory

    c –当前目录

  • e – executeable is running

    e –执行中正在运行

  • f – open file

    f –打开文件

  • F – open file for writing

    F –打开文件进行写入

  • r – root directory

    r –根目录

  • m – mmaped file or shared library

    m –映射文件或共享库

显示更多信息,例如用户和命令(Display More Information Like User And Command)

In the previous example we have only listed PID’s. But this information can not be enough some time. We may need more information like the user name and related command. To get user name and related command more verbose output is needed. Verbose output can be displayed with -v option. In the example we will list information about directory access of processes with user and command information in a column based output.

在前面的示例中,我们仅列出了PID。 但是这些信息在一段时间内还不够。 我们可能需要更多信息,例如用户名和相关命令。 为了获得用户名和相关命令,需要更多详细的输出。 详细输出可以使用-v选项显示。 在示例中,我们将在基于列的输出中列出有关进程目录访问的信息以及用户和命令信息。

$ fuser -v /home/ismail
Display More Information Like User And Command
Display More Information Like User And Command
显示更多信息,例如用户和命令

In the example out we can see that our looking path is /home/ismail and process id is 2548 which is the PID of bash cmomand with user ismail

在示例中,我们可以看到我们的查找路径为/home/ismail ,进程ID为2548 ,这是bash cmom和用户ismail的PID。

使用文件系统显示进程 (Display Processes Using File System)

Process access information about the file system can be displayed with -m option. This will create enormous amount of output.

可以使用-m选项显示有关文件系统的进程访问信息。 这将产生大量的输出。

$ fuser -v -m /home/ismail/.bashrc
Display Processes Using File System
Display Processes Using File System
使用文件系统显示进程

关于TCP和UDP套接字的显示过程(Display Processes About TCP and UDP Sockets)

Another useful feature of fuser command is listing process information of TCP and UDP sockets. This can be very useful if our Apache web server do not starts because of the TCP 80 is used by another process we do not know. We can find this process easily. Furthermore this process can be killed with a single command which we will look below examples. In the example we want to list processes information listening TCP 22. We need root privileges to get information. We will provide -n option with tcp 22 parameter

fuser命令的另一个有用功能是列出TCP和UDP套接字的进程信息。 如果我们的Apache Web服务器由于另一个未知进程正在使用TCP 80而无法启动,那么这将非常有用。 我们可以很容易地找到这个过程。 此外,可以使用单个命令终止该过程,我们将在以下示例中查看。 在该示例中,我们希望列出侦听TCP 22的进程信息。我们需要root特权才能获取信息。 我们将为-n选项提供tcp 22参数

$ fuser -v -n tcp 22
Display Processes About TCP and UDP Sockets
Display Processes About TCP and UDP Sockets
关于TCP和UDP套接字的显示过程

In order  to use with UDP ports look following command.

为了与UDP端口一起使用,请查看以下命令。

$ fuser -v -n udp 53

使用指定的TCP套接字终止进程 (Kill Process Using Specified TCP Socket)

As previously stated we can kill process by specifying TCP or UDP ports or sockets in a single command. We will provide -k option to kill related process. In the example we will kill all ssh or TCP port 22 process.

如前所述,我们可以通过在单个命令中指定TCP或UDP端口或套接字来终止进程。 我们将提供-k选项以杀死相关进程。 在示例中,我们将终止所有ssh或TCP端口22进程。

$ sudo fuser -k -n tcp 22
Kill Process Using Specified TCP Socket
Kill Process Using Specified TCP Socket
使用指定的TCP套接字终止进程

We can see that we have killed our current ssh connection to our server.

我们可以看到我们已经终止了当前与服务器的ssh连接。

LEARN MORE  Linux Mtr Command Tutorial with Examples To Network Diagnostics
了解更多Linux Mtr命令教程,并带有网络诊断示例

列出信号(List Signals)

In previous example we have killed the ssh process. Killing a process is done by sending signal to the process. Signals are used to communicate with process. To kill a process kill related signals are sent. There is also different type of signals those may send to the related process. We can list signals  list-signals option like below.

在前面的示例中,我们取消了ssh进程。 通过向进程发送信号来终止进程。 信号用于与过程进行通信。 为了终止进程,发送终止信号。 那些可能会发送到相关进程的信号也有不同类型。 我们可以像下面列出信号list-signals选项。

$ fuser -list-signals
List Signals
List Signals
列出信号

使用特定信号杀死进程(Kill Process With Specific Signal)

As we stated we can send specific signal to the process we have specified. In the example we will send -HUP signal to the TCP 22 or ssh port with the following command.

正如我们所说,我们可以向指定的过程发送特定的信号。 在示例中,我们将使用以下命令将-HUP信号发送到TCP 22或ssh端口。

$ sudo fuser -k -HUP -n tcp 22
Kill Process With Specific Signal
Kill Process With Specific Signal
使用特定信号杀死进程

交互式杀死进程(Kill Process Interactively)

While killing process in the critical servers  we have no room for mistakes. But issuing fuser command will execute command in real time without any caution. We can prevent mistakes and kill related processes in a more controlled way with interactive option. Interactive option is provided with -i

在关键服务器中终止进程时,我们没有犯错的余地。 但是发出fuser命令将实时执行命令,而不会引起任何警告。 我们可以通过交互式选项以更可控的方式防止错误并杀死相关进程。 -i提供了交互式选项

$ sudo fuser -i -k -n tcp 22
Kill Process Interactively
Kill Process Interactively
交互式杀死进程

翻译自: https://www.poftut.com/linux-fuser-command-tutorial-with-examples/

linux fuser命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值