展会信息港展会大全

CentOS6下解决光盘无法退出
来源:互联网   发布日期:2015-08-20 21:58:37   浏览:1756次  

导读: 一、简介 当您想退出光盘时,须使用umout命令,否则光驱就会一直处于死锁状态,#umount /dev/sr0,或者直接退出光盘#eject。但当光盘正在被打开或者使用,使用上述命令都无法成功提示device is busy,这时就需要使用lsof或fuser命令了(不想了解命令参数的详...

一、简介

当您想退出光盘时,须使用umout命令,否则光驱就会一直处于死锁状态,#umount /dev/sr0,或者直接退出光盘#eject。但当光盘正在被打开或者使用,使用上述命令都无法成功提示device is busy,这时就需要使用lsof或fuser命令了(不想了解命令参数的详细,可直接查看后面的退出光盘操作)。

二、详解

1、lsof命令

lsof(list open files)是一个列出当前系统打开文件的工具,在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,无论这个文件的本质如何,该文件描述符为应用程序与基础操作系统之间的交互提供了通用接口。因为应用程序打开文件的描述符列表提供了大量关于这个应用程序本身的信息,因此通过lsof工具能够查看这个列表对系统监测以及排错将是很有帮助的。

(1)命令格式:

lsof [参数][文件]

(2)命令功能:

用于查看你进程开打的文件,打开文件的进程,进程打开的端口(TCP、UDP)。找回/恢复删除的文件。是十分方便的系统监视工具,因为 lsof 需要访问核心内存和各种文件,所以需要root用户执行。

lsof打开的文件可以是:普通文件、目录、网络文件系统的文件、字符或设备文件、(函数)共享库、管道和命名管道、符号链接、网络文件(例如:NFS file、网络socket,unix域名socket),还有其它类型的文件等等。

(3)命令参数:

-a 列出打开文件存在的进程

-c<进程名> 列出指定进程所打开的文件

-g 列出GID号进程详情

-d<文件号> 列出占用该文件号的进程

+d<目录> 列出目录下被打开的文件

+D<目录> 递归列出目录下被打开的文件

-n<目录> 列出使用NFS的文件

-i<条件> 列出符合条件的进程。(4、6、协议、:端口、 @ip )

-p<进程号> 列出指定进程号所打开的文件

-u 列出UID号进程详情

-h 显示帮助信息

-v 显示版本信息

(4)使用实例:

实例1:无任何参数

命令:#lsof

说明:

lsof输出各列信息的意义如下:

COMMAND:进程的名称

PID:进程标识符

PPID:父进程标识符(需要指定-R参数)

USER:进程所有者

PGID:进程所属组

FD:文件描述符,应用程序通过文件描述符识别该文件。如cwd、txt等

(1)cwd:表示current work dirctory,即:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改

(2)txt :该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序

(3)lnn:library references (AIX);

(4)er:FD information error (see NAME column);

(5)jld:jail directory (FreeBSD);

(6)ltx:shared library text (code and data);

(7)mxx :hex memory-mapped type number xx.

(8)m86:DOS Merge mapped file;

(9)mem:memory-mapped file;

(10)mmap:memory-mapped device;

(11)pd:parent directory;

(12)rtd:root directory;

(13)tr:kernel trace file (OpenBSD);

(14)v86 VP/ix mapped file;

(15)0:表示标准输出

(16)1:表示标准输入

(17)2:表示标准错误

一般在标准输出、标准错误、标准输入后还跟着文件状态模式:r、w、u等

(1)u:表示该文件被打开并处于读取/写入模式

(2)r:表示该文件被打开并处于只读模式

(3)w:表示该文件被打开并处于

(4)空格:表示该文件的状态模式为unknow,且没有锁定

(5)-:表示该文件的状态模式为unknow,且被锁定

同时在文件状态模式后面,还跟着相关的锁

(1)N:for a Solaris NFS lock of unknown type;

(2)r:for read lock on part of the file;

(3)R:for a read lock on the entire file;

(4)w:for a write lock on part of the file;(文件的部分写锁)

(5)W:for a write lock on the entire file;(整个文件的写锁)

(6)u:for a read and write lock of any length;

(7)U:for a lock of unknown type;

(8)x:for an SCO OpenServer Xenix lock on part of the file;

(9)X:for an SCO OpenServer Xenix lock on the entire file;

(10)space:if there is no lock.

TYPE:文件类型,如DIR、REG等,常见的文件类型

(1)DIR:表示目录

(2)CHR:表示字符类型

(3)BLK:块设备类型

(4)UNIX: UNIX 域套接字

(5)FIFO:先进先出 (FIFO) 队列

(6)IPv4:网际协议 (IP) 套接字

DEVICE:指定磁盘的名称

SIZE:文件的大小

NODE:索引节点(文件在磁盘上的标识)

NAME:打开文件的确切名称

实例2:查看谁正在使用某个文件,也就是说查找某个文件相关的进程

命令:#lsof /bin/bash

输出:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

[root@localhost ~]# lsof /bin/bash

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

bash 24159 root txt REG 8,2 801528 5368780 /bin/bash

bash 24909 root txt REG 8,2 801528 5368780 /bin/bash

bash 24941 root txt REG 8,2 801528 5368780 /bin/bash

实例3:递归查看某个目录的文件信息

命令:#lsof +D /opt/soft/test/test3

输出:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

[root@localhost ~]# cd /opt/soft/

[root@localhost soft]# lsof +D /opt/soft/test/test3

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

bash 24941 root cwd DIR 8,2 4096 2258872 test/test3

vi 24976 root cwd DIR 8,2 4096 2258872 test/test

说明:使用了+D,对应目录下的所有子目录和文件都会被列出。

实例4:不使用+D选项,遍历查看某个目录的所有文件信息的方法

命令:# lsof |grep 'test/test3'

输出:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

[root@localhost soft]# lsof |grep 'test/test3'

bash 24941 root cwd DIR 8,2 4096 2258872 /opt/soft/test/test3

vi 24976 root cwd DIR 8,2 4096 2258872 /opt/soft/test/test3

vi 24976 root 4u REG 8,2 12288 2258882 /opt/soft/test/test3/.log2013.log.swp

实例5:列出某个用户打开的文件信息

命令:#lsof -u username

说明: -u 选项,u其实是user的缩写。

实例6:列出某个程序进程所打开的文件信息

命令:#lsof -c mysql

说明:-c 选项将会列出所有以mysql这个进程开头的程序的文件,其实你也可以写成 lsof | grep mysql, 但是第一种方法明显比第二种方法要少打几个字符了。

实例7:列出多个进程多个打开的文件信息

命令:#lsof -c mysql -c apache

实例8:列出某个用户以及某个进程所打开的文件信息

命令:#lsof -u test -c mysql

说明:用户与进程可相关,也可以不相关。

实例9:列出除了某个用户外的被打开的文件信息

命令:#lsof -u ^root

说明:^这个符号在用户名之前,将会把是root用户打开的进程不让显示。

实例10:通过某个进程号显示该进行打开的文件

命令:#lsof -p 1

实例11:列出多个进程号对应的文件信息

命令:#lsof -p 1,2,3

实例12:列出除了某个进程号,其他进程号所打开的文件信息

命令:#lsof -p ^1

实例13:列出所有的网络连接

命令:#lsof -i

实例14:列出所有tcp 网络连接信息

命令:#lsof -i tcp

实例15:列出所有udp网络连接信息

命令:#lsof -i udp

实例16:列出谁在使用某个端口

命令:#lsof -i :3306

实例17:列出谁在使用某个特定的udp端口

命令:#lsof -i udp:55

或者:特定的tcp端口

命令:#lsof -i tcp:80

实例18:列出某个用户的所有活跃的网络端口

命令:#lsof -a -u test -i

实例19:列出所有网络文件系统

命令:#lsof -N

实例20:域名socket文件

命令:#lsof -u

实例21:某个用户组所打开的文件信息

命令:#lsof -g 5555

实例22:根据文件描述列出对应的文件信息

命令:#lsof -d description(like 2)

例如:lsof -d txt

例如:lsof -d 1

例如:lsof -d 2

说明:0表示标准输入,1表示标准输出,2表示标准错误,从而可知:所以大多数应用程序所打开的文件的 FD 都是从 3 开始

实例23:根据文件描述范围列出文件信息

命令:#lsof -d 2-3

实例24:列出COMMAND列中包含字符串" sshd",且文件描符的类型为txt的文件信息

命令:#lsof -c sshd -a -d txt

[html] view plaincopy在CODE上查看代码片派生到我的代码片

[root@localhost soft]# lsof -c sshd -a -d txt

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

sshd 2756 root txt REG 8,2 409488 1027867 /usr/sbin/sshd

sshd 24155 root txt REG 8,2 409488 1027867 /usr/sbin/sshd

sshd 24905 root txt REG 8,2 409488 1027867 /usr/sbin/sshd

sshd 24937 root txt REG 8,2 409488 1027867 /usr/sbin/sshd

实例25:列出被进程号为1234的进程所打开的所有IPV4 network files

命令:#lsof -i 4 -a -p 1234

实例26:列出目前连接主机peida.linux上端口为:20,21,22,25,53,80相关的所有文件信息,且每隔3秒不断的执行lsof指令

命令:#lsof -i @peida.linux:20,21,22,25,53,80 -r 3

(5)退出光盘操作

首先查看光盘挂载信息:#df -lh

[html] view plaincopy在CODE上查看代码片派生到我的代码片

文件系统 容量 已用 可用 已用%% 挂载点

/dev/sda6 237G 134G 91G 60% /

tmpfs 1.9G 288K 1.9G 1% /dev/shm

/dev/sr0 3.8G 3.8G 0 100% /media/centos6.5

然后使用#lsof /dev/sr0或者#lsof /media/centos6.5

[html] view plaincopy在CODE上查看代码片派生到我的代码片

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

bash 5476 aoyang cwd DIR 11,0 2048 1792 /media/centos6.5

AutoSetup 5617 aoyang cwd DIR 11,0 2048 1792 /media/centos6.5

AutoSetup 5617 aoyang txt REG 11,0 7722726 1808 /media/centos6.5/AutoSetup

接着使用#kill -9 5476 5617

最后使用#umount /media/centos6.5或#eject即可卸载光盘。

2、fuser命令

fuser用以由文件或设备去找出使用文件、或设备的进程,对于阻塞特别设备,此命令列出了使用该设备上任何文件的进程。umount光驱的时候,结果系统提示设备正在使用或者正忙,fuser便可进行查询和处理。

(1)fuser 指令

(与manpage中#man fuser相对应)

用途:使用文件或文件结构识别进程。http://write.blog.csdn.net/postedit/42487987

语法:fuser [ -c | -d | -f ] [ -k ] [ -u ] [ -x ] [ -V ] 文件 ...

描述:

fuser displays the PIDs of processes using the specified filesor file systems. In the default display mode, each file name isfollowed by a letter denoting the type of access:

# 注释 :fuser 显示使用指定文件或者文件系统的进程的 pid ,默认会在每个 pid 后面加上一个字符表示访问的方式:

(显示信息的字符,区别于标志)

# -)1、c:表示 current directory 。表示进程是在该目录下启动的。

# -)2、e:表示是可执行的程序

# -)3、f:打开文件。默认不显示

# -)4、r:root 目录,表示该目录是该进程的 / 目录

# -)5、m:表示 mmap 过的文件或者共享库

c:current directory.

e:executable being run.

f:open file. f is omitted in default display mode.

F:open file for writing. F is omitted in default displaymode.

r:root directory.

m:mmap'ed file or shared library.

fuser returns a non-zero return code if none of the specifiedfiles is accessed or in case of a fatal error. If at least oneaccess has been found, fuser returns zero.

# 注释 :如果指定文件没有任何进程访问,或者出现严重错误,fuser 返回非 0 的 exit status。

# 如果至少存在一个进程,则 fuser 返回 0

In order to look up processes using TCP and UDP sockets, thecorresponding name space has to be selected with the -n option. Bydefault fuser will look in both IPv6 and IPv4 sockets. To changethe default, behavior, use the -4 and -6 options. The socket(s) canbe specified by the local and remote port, and the remote address.All fields are optional, but commas in front of missing fields mustbe present:

# 注释:为了查找使用某个 TCP /UDP sockets 的进程,你必须用 -n 指定 (name space)tcp还是 udp 。

# 默认 fuser 会查找 ipv4 和 ipv6 sockets 。你可以使用 -4 或者 -6 来改变选项

# sockets 可以按照 local port:remote_host:remote_port的方式来指定。每个字段都是可选的,但不能省略 ','

[lcl_port][,[rmt_host][,[rmt_port]]]

Either symbolic or numeric values can be used for IP addressesand port numbers.

# 注释 ;在端口方面,你可以用端口名或者数字表示

fuser outputs only the PIDs to stdout, everything else is sentto stderr.

# 注释 :fuser 只输出 PID 到 stdout ,其他都送给 stderr

Options

-a

Show all files specified on the command line. By default, onlyfiles that are accessed by at least one process are shown.

# 注释 :-a 显示所有指定文件的情况,即使没有进程访问它们。

-c

Same as -m option, used for POSIX compatibility.

-f

Silently ignored, used for POSIX compatibility.

-k

Kill processes accessing the file. Unless changed with-signal, SIGKILL is sent. An fuser process never kills itself, butmay kill other fuser processes. The effective user ID of theprocess executing fuser is set to its real user ID beforeattempting to kill.

# 注释 :-k 表示杀死访问指定文件的进程。默认发送 SIGKILL(-9)信号。fuser 进程永远不会杀死自己。

# 但可能杀死其他 fuser 进程。在尝试杀死进程前,fuser 会改变 euid为 ruid

-i

Ask the user for confirmation before killing a process. Thisoption is silently ignored if -k is not present too.

# 注释 :-i 表示交互模式,在杀死进程前会提示。必须和 -k 合用

-l

List all known signal names.

# 注释 :-l 列出所有已知的信号

-m

name specifies a file on a mounted file system or a blockdevice that is mounted. All processes accessing files on that filesystem are listed. If a directory file is specified, it isautomatically changed to name/. to use any file system that mightbe mounted on that directory.

# 注释:-m 表示指定文件所在的文件系统或者块设备(处于 mount状态)。所有访问该文件系统的进程都被列出。

# 假如指定了一个目录,则自动在后面加上 '/' 表示该挂载在该目录上的文件系统

-n space

Select a different name space. The name spaces file (filenames, the default), udp (local UDP ports), and tcp (local TCPports) are supported. For ports, either the port number or thesymbolic name can be specified. If there is no ambiguity, theshortc utnotation name/Ispace (e.g. 80/tcp ) can be used.

# 注释 :-n 指定 sockets 的协议类型。可以是file(默认)、udp、tcp 。

#你甚至可以使用'<port>/<proto>'的简写方式,只要不存在异义的话

-s

Silent operation. -u and -v are ignored in this mode. -a mustnot be used with -s.

# 注释 :-s 表示安静模式。-u 和 -v 会被忽略,-a 不能和 -s一起使用

-signal

Use the specified signal instead of SIGKILL when killingprocesses. Signals can be specified either by name (e.g. -HUP) orby number (e.g. -1). This option is silently ignored if the -koption is not used.

#注释:-<signal> 指定信号

-u

Append the user name of the process owner to each PID.

# 注释 :-u 表示在 PID 后面加上进程的 owner

-v

Verbose mode. Processes are shown in a ps-like style. Thefields PID, USER and COMMAND are similar to ps. ACCESS shows howthe process accesses the file. If the access is by the kernel (e.g.in the case of a mount point, a swap file, etc.), kernel is showninstead of the PID.

# 注释 :-v 表示 verbose 模式。进程以 ps 的方式显示,包括 PID、USER、COMMAND、ACCESS字段

# 如果是内核访问的(例如 mount point、swap 文件),则显示为kernel 而不是 pid

-V

Display version information.

-4

Search only for IPv4 sockets. This option must not be usedwith the -6 option and only has an effect with the tcp and udpnamespaces.

#仅查询IPV4套接字

-6

Search only for IPv6 sockets. This option must not be usedwith the -4 option and only has an effect with the tcp and udpnamespaces.

#仅查询IPV6套接字

-

Reset all options and set the signal back to SIGKILL.

# 注释: - 表示重置所有选项并把信号设置为 SIGKILL

Files

/proc location of the proc file system

Examples

fuser -km /home

kills all processes accessing the filesystem /home in any way.

# 注释 :fuser -km /home 杀死所有访问 /home文件系统的进程

if fuser -s /dev/ttyS1; then :; else something; fi

invokes something if no other process isusing /dev/ttyS1.

fuser telnet/tcp

shows all processes at the (local) TELNETport.

要列出使用 /etc/passwd 文件的本地进程的进程号,请输入:

fuser /etc/passwd要列出使用 /etc/filesystems 文件的进程的进程号和用户登录名,请输入:

fuser -u /etc/filesystems要终止使用给定文件系统的所有进程,请输入:

fuser -k -x -u -c /dev/hd1 或者

fuser -kxuc /home任一命令都列出了进程号和用户名,然后终止每个正在使用 /dev/hd1 (/home) 文件系统的进程。仅有 root 用户能终止属于另一用户的进程。如果您正在试图卸下 /dev/hd1 文件系统,而一个正在访问 /dev/hd1 文件系统的进程不允许这样,您可能希望使用此命令。

要列出正在使用已从给定文件系统删除的文件的全部进程,请输入:

fuser -d /usr文件

/dev/kmem 用于系统映像。

/dev/mem 也用于系统映像。

# fuser -vm /usr/local/

[html] view plaincopy在CODE上查看代码片派生到我的代码片

USER PID ACCESS COMMAND

r/local/: daemon 5227 F..e. httpd

root 8332 F..e.httpd

root 14402 f.c..mysqld_safe

mysql 14502 F.ce. mysqld

daemon 14611 F..e. httpd

daemon 27591 F..e. httpd

daemon 27619 F..e. httpd

在使用fuser -km 就可以杀死进程,同时也可以使用交互式的-i参数,一个一个杀死。

(2)退出光盘操作

首先查看光盘挂载信息:#df -lh

然后使用#fuser -km /media/centos6.5或fuser -km /dev/sr0

最后使用#umount /media/centos6.5或#eject即可卸载光盘。

三、总结

(1)lsof和fuser对卸载光盘过程中的操作虽然简单,但是详细的参数也得了解。

(2)其他方面的应用暂时没有很深的接触,以后再加以总结。

赞助本站

人工智能实验室

相关热词: CentOS6 解决 光盘 无法 退出

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港