fuser命令--谁打开了我的文件

本文介绍了fuser命令的使用方法及技巧,包括如何查找占用文件或套接字的进程,并提供了实际应用场景示例。同时对比了fuser与lsof命令的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fuser命令学习

fuser:使用文件或者套节字来表示识别进程。我常用的他的两个功能:查看我需要的进程和我要杀死我查到的进程。

比如当你想umount光驱的时候,结果系统提示你设备正在使用或者正忙,可是你又找不到到底谁使用了他。这个时候fuser可派上用场了。

[root@lancy sbin]# eject

umount: /media/cdrom: device is busy

umount: /media/cdrom: device is busy

eject: unmount of `/media/cdrom' failed

[root@lancy sbin]# fuser /mnt/cdrom

/mnt/cdrom: 4561c 5382c

[root@lancy sbin]# ps -ef |egrep '(4561|5382)' |grep -v grep

root 4561 4227 0 20:13 pts/1 00:00:00 bash

root 5382 4561 0 21:42 pts/1 00:00:00 vim Autorun.inf

示例中,我想弹出光驱,系统告诉我设备忙着,于是采用fuser命令,参数是你文件或scoket,fuser将查出那些使用了他。

4561c,5382c表示目前用两个进程在占用着/mnt/cdrom,分别是4561,5382,进程ID后的字母表示占用资源的方式,有下面几种表示:

c 当前路径(current directory.)我的理解是表示这个资源的占用是以文件目录方式,也就是进进入了需要释放的资源的路径,这是最常用的资源占用方式。

e 正在运行可执行文件(executable being run.),比如运行了光盘上的某个程序

f 打开文件( open file),缺省模式下f忽略。所以上面的例子中,虽然是开打了光盘上的Autorun.inf文件,但是给出的标识是c,而不是f。

r root目录(root directory).没有明白什么意思,难道是说进入了/root这个特定目录?

m mmap文件或者共享库( mmap’ed file or shared library).这应该是说某个进程使用了你要释放的资源的某个共享文件。

在查找的同时,你还可定指定一些参数,比如

-k 杀死这些正在访问这些文件的进程。除非使用-signal修改信号,否则将发送SIGKILL信号。

-i 交互模式

-l 列出所有已知的信号名称。

-n 空间,选择不同的名字空间,可是file,udp,tcp。默认是file,也就是文件。

-signal 指定发送的信号,而不是缺省的SIGKILL

-4 仅查询IPV4套接字

-6 仅查询IPV6套接字

- 重置所有的选项,将信息设回SIGKILL

再看下面的例子

[root@lancy sbin]# 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

现在我们试试fuser -k的威力:

[root@lancy sbin]# fuser -k /mnt/cdrom

/mnt/cdrom: 4561c 5382c

kill 5382: 没有那个进程

No automatic removal. Please use umount /media/cdrom

[root@lancy sbin]# eject

套节字方式的使用:

[root@lancy sbin]# fuser -4 -n tcp 3306

here: 3306

3306/tcp: 5595

[root@lancy sbin]# ps -ef |grep 5595 |grep -v grep

mysql 5595 5563 0 22:24 pts/0 00:00:00 /usr/libexec/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --socket=/var/lib/mysql/mysql.sock

[root@lancy sbin]# fuser -4 -n tcp 80

here: 80

80/tcp: 5685 5688 5689 5690 5691 5692 5693 5694 5695

List Open Files: lsof and fuser

The function of these commands is very similar. The goal is to determine what processes have certain files open. lsof is freeware and thus freely compilable under all Unixes. It is available by default under Linux only.

The main difference between lsof and fuser is that lsof takes both files/filesystems and PIDs as arguments whereas fuser only accepts files/filesystems.

lsof:

lsof stands for list open files. It lists information about files that are currently open by processes.

In the absence of any options, lsof lists all open files belonging to all active processes

To list all open files for login name ``abe'', or user ID 1234 you would use the -u option. To list files being used by process 456, process 123, or process 789, you would use the -p flag. Putting these criteria together, we would have a command that looks like this:

lsof -p 456,123,789 -u 1234,abe

The command to list all open files on device /dev/hd4, would look like this:

lsof /dev/hd4

To find the process that has /u/abe/foo open, use:

lsof /u/abe/foo

fuser:

fuser is more widely available (under AIX, HP-UX, IRIX, Linux, Solaris, Tru64 UNIX, and others) and can only be run as root. It displays the process ID numbers of processes using the specified files or file systems. In the default display mode, each file name is followed by a letter denoting the type of access (these codes differ from vendor to vendor):

aif the process is using the file as its trace file in /proc (IRIX)
cif the process is using the file as its current directory
eif the process is using the file as the executable being run
fif the process is using the file as an open file (Linux)
mif the process is using the file as a mmaped or shared lib (Linux)
oif the process is using the file as an open file (IRIX, Solaris, Tru64 UNIX))
pif the process is using the file as the parent of its current directory (Tru64 UNIX)
rif the process is using the file as root directory
sif the process is using the file as a shared lib (AIX)
tif the process is using the file as its text file (Solaris)
yif the process is using the file as its controlling terminal (IRIX)

Here is an example of fuser running under Linux:

linux# /usr/sbin/fuser /

/: 1r 1c 2r 2c 3r 3c 4r 4c

252r 252c 275r 275c 286r 286c 300r 314r 328r 328c 342r

342c 356r 356c 374r 374c 385r 385c 396r 396c 411r 411c

412r 412c 413r 413c 414r 414c 415r 415c 416r 416c 417r

417c 418r 418c 419r 419c 420r 420c 455r 470r 470c 485r

485c 500r 500c 538r 538c 539r 539c 540r 540c 541r 541c

542r 542c 543r 543c 544r 544c 546r 546c 548r 548c 551r

551c 556r 569r 591r 597r 610r 612r 614r 614c 618r 626r

628r 636r 646r 7090r 7090c 7092r 7243r 7288r 7288c 7290r 7303r

<think>首先,用户的问题是:“我想使用fuser命令强制杀死占用/disk/sd/路径的进程请问如何用fuser-km命令终止占用/disk/sd/的进程”用户想用fuser命令杀死占用/disk/sd/路径的进程。具体命令fuser-km。我需要根据系统级指令来回答:-所有行内数学表达式必须用$...$格式。但在这个问题中,似乎没有数学表达式,所以可能不需要。-独立公式必须用$$...$$格式并单独成段。同样,这里可能没有公式。-LaTeX语法正确:确保任何数学部分正确。-使用中文回答:整个回答必须用中文。-生成相关问题:在回答结束后,添加相关问题。-引用的段落末尾自然地添加引用标识:用户提供了一些引用,我需要检查是否相关。引用包括:-[^1]:关于测试网络速度的命令-[^2]:关于磁盘分区的信息。-[^3]:关于制作和挂载ISO文件。用户说:“上述引用仅供参考,并不是用户真正的需求”,所以我不能直接使用这些引用。但用户说“请尽可能多的参考用户上一次的问题和你的回答”,但这是第一次交互,没有上一次问题。所以我应该忽略这些引用,除非有相关部分。用户的问题是具体的:使用fuser-km杀死占用路径的进程。关键点:-fuser命令用于显示哪些进程使用指定的文件或套接字。--k选项表示杀死进程。--m选项指定文件系统或挂载点。-用户指定了路径/disk/sd/,这看起来像是一个目录或挂载点。标准fuser命令用法:-要杀死占用挂载点的进程:fuser-km/mountpoint-例如:fuser-km/disk/sd/-这将发送SIGKILL信号给所有访问该挂载点的进程。在回答中,我应该:1.解释命令。2.给出具体命令。3.警告风险:强制杀死进程可能导致数据丢失或系统不稳定。4.建议先检查哪些进程占用:使用fuser-mv/disk/sd/来查看进程列表。回答结构:-清晰、逐步解决问题。-用中文。最后,生成相关问题。回答大纲:1.介绍fuser命令fuser用于显示或杀死访问特定文件文件系统的进程。2.具体命令:要杀死占用/disk/sd/路径的进程,使用fuser-km/disk/sd/3.解释选项:-k表示杀死进程,-m表示指定挂载点。4.安全提示:这可能会强制终止进程,导致未保存数据丢失。建议在必要时使用。5.建议步骤:首先运行fuser-mv/disk/sd/来识别进程,然后确认是否杀死。6.示例输出。生成相关问题:1.如何安全卸载挂载点?2.fuser命令的其他常见用法?3.如果fuser命令不起作用怎么办?确保回答真实可靠:基于Linux知识。用户提到的引用:[^1]关于网络测试,[^2]关于磁盘分区,[^3]关于ISO挂载。似乎不直接相关,但[^3]提到挂载,可能有点关联,但用户说不是真正需求,所以我只专注于问题。最终命令fuser-km/disk/sd/路径是/disk/sd/,看起来像是一个目录。fuser-m用于挂载点,如果/disk/sd/是一个挂载点,则适用;否则,可能需要使用文件或目录路径。但fuser可以处理目录路径。在Linux中,fuser-m/path用于挂载点,如果/path是挂载点,则列出访问该文件系统的进程。如果/disk/sd/是一个普通目录,fuser也可以工作,但可能会列出访问该目录或子目录的进程。用户指定了路径,所以命令应该有效。</think>### 使用 fuser -km 命令终止占用路径的进程 在 Linux 系统中,`fuser` 命令用于识别或终止访问特定文件、目录或挂载点的进程。`fuser -km` 选项组合可以强制杀死占用指定路径的所有进程,其中 `-k` 表示杀死进程(默认发送 SIGKILL 信号),`-m` 表示处理文件系统或挂载点。以下是如何安全使用此命令终止占用 `/disk/sd/` 路径进程的步骤: #### 步骤 1: 确认路径和权限 - **检查路径类型**:确保 `/disk/sd/` 是一个有效的目录或挂载点。如果它是一个挂载点(如磁盘分区),`fuser -m` 会更精确地处理;如果是普通目录,命令也适用,但可能影响更多进程。 - 使用 `df -h /disk/sd/` 查看是否挂载: ```bash df -h /disk/sd/ ``` 输出示例: ``` Filesystem Size Used Avail Use% Mounted on /dev/sdb1 100G 50G 50G 50% /disk/sd ``` 如果输出显示挂载信息,则路径是挂载点。 - **权限要求**:执行 `fuser` 命令需要 root 权限,否则可能无法杀死某些进程。使用 `sudo` 提升权限: ```bash sudo fuser -km /disk/sd/ ``` #### 步骤 2: 先识别占用进程(推荐安全步骤) 在强制杀死进程前,强烈建议先查看哪些进程占用了路径,以避免误杀关键进程(如数据库或服务)。使用 `fuser -mv` 命令: ```bash sudo fuser -mv /disk/sd/ ``` - **选项解释**: - `-v`:详细模式,显示进程详情。 - `-m`:指定挂载点或文件系统。 - **示例输出**: ``` USER PID ACCESS COMMAND /disk/sd/: root 12345 ..c.. bash mysql 12346 f.... mysqld ``` - 输出中: - `ACCESS` 列:`c` 表示当前目录,`f` 表示打开文件等。 - `COMMAND` 列:显示进程名(如 `bash` 或 `mysqld`)。 - 确认这些进程是否可安全终止。如果有重要进程,尝试正常停止它们(如使用 `service` 命令)。 #### 步骤 3: 执行 fuser -km 杀死进程 确认路径和进程后,运行 `fuser -km` 强制杀死所有占用进程: ```bash sudo fuser -km /disk/sd/ ``` - **命令行为**: -命令会向所有访问 `/disk/sd/` 的进程发送 SIGKILL 信号(强制终止)。 - 完成后,通常无输出(静默成功)。如果失败,会显示错误信息(如路径无效)。 - **验证结果**: - 再次运行 `fuser -mv /disk/sd/`,检查是否无进程占用。 - 尝试访问路径(如 `ls /disk/sd/`),确认无“设备忙”错误。 #### 重要风险提示 - **数据丢失风险**:SIGKILL 信号会立即终止进程,可能导致未保存数据丢失或应用崩溃。仅在生产环境紧急情况下使用。 - **系统稳定性**:如果路径被系统关键进程占用(如 `/` 根分区),强制杀死可能导致系统不稳定。建议在维护模式下操作。 - **备选方案**:如果 `fuser -km` 无效,尝试: - 先卸载挂载点:`sudo umount -l /disk/sd/`(`-l` 表示懒卸载)。 - 使用 `lsof` 查找进程:`sudo lsof /disk/sd/ | grep -v COMMAND`,然后手动杀死(`kill -9 PID`)。 #### 相关问题 1. 如何安全卸载一个被占用的挂载点而不强制杀死进程? 2. `fuser` 命令还有哪些常用选项和场景(如处理网络端口)? 3. 如果 `fuser -km` 无法杀死进程,有哪些备用的故障排除方法?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值