设为首页 收藏本站
查看: 1720|回复: 0

[经验分享] Linux之fuser命令

[复制链接]

尚未签到

发表于 2018-5-25 11:12:54 | 显示全部楼层 |阅读模式
  

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


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


[root@lijun ~]# fuser -vm /usr/local/


          USER    PID ACCESSCOMMAND
/usr/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参数,一个一个杀死。


Description


fuser displays the PIDs of processes usingthe specified files or file systems. In the default display mode, each filename is followed by a letter denoting the type of access:


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


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


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


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


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


#   -5m :表示 mmap 过的文件或者共享库
c
current directory.
e
executable being run.
f
open file. f is omitted in default displaymode.
F
open file for writing. F is omitted indefault display mode.
r
root directory.
m
mmap'ed file or shared library.
fuser returns a non-zero return code ifnone of the specified files is accessed or in case of a fatal error. If atleast one access has been found, fuser returns zero.


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


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


In order to look up processes using TCP andUDP sockets, the corresponding name space has to be selected with the -noption. By default fuser will look in both IPv6 and IPv4 sockets. To change thedefault, behavior, use the -4 and -6 options. The socket(s) can be specified bythe local and remote port, and the remote address. All fields are optional, butcommas in front of missing fields must be present:


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


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


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


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


Either symbolic or numeric values can beused for IP addresses and port numbers.


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


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


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


Options


-a
Show all files specified on the commandline. By default, only files that are accessed by at least one process areshown.
      # 注释:-a 显示所有指定文件的情况,即使没有进程访问它们。
-c
Same as -m option, used for POSIXcompatibility.
-f
Silently ignored, used for POSIXcompatibility.
-k
Kill processes accessing the file. Unlesschanged with -signal, SIGKILL is sent. An fuser process never kills itself, butmay kill other fuser processes. The effective user ID of the process executingfuser is set to its real user ID before attempting to kill.
      # 注释:-k 表示杀死访问指定文件的进程。默认发送 SIGKILL -9)信号。fuser 进程永远不会杀死自己。


      # 但可能杀死其他 fuser 进程。在尝试杀死进程前,fuser 会改变 euid ruid
-i
Ask the user for confirmation beforekilling a process. This option 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 filesystem or a block device that is mounted. All processes accessing files on thatfile system are listed. If a directory file is specified, it is automaticallychanged to name/. to use any file system that might be mounted on thatdirectory.
      # 注释:-m 表示指定文件所在的文件系统或者块设备(处于 mount 状态)。所有访问该文件系统的进程都被列出。


      # 假如指定了一个目录,则自动在后面加上 '/' 表示该挂载在该目录上的文件系统
-n space
Select a different name space. The namespaces file (file names, the default), udp (local UDP ports), and tcp (localTCP ports) are supported. For ports, either the port number or the symbolicname can be specified. If there is no ambiguity, the shortc  utnotation name/Ispace (e.g. 80/tcp ) can be used.
      # 注释:-n 指定 sockets 的协议类型。可以是 file(默认)、udptcp


      # 你甚至可以使用'<port>/<proto>' 的简写方式,只要不存在异义的话
-s
Silent operation. -u and -v are ignored inthis mode. -a must not be used with -s.
      # 注释:-s 表示安静模式。-u -v 会被忽略,-a 不能和 -s 一起使用
-signal
Use the specified signal instead of SIGKILLwhen killing processes. Signals can be specified either by name (e.g. -HUP) orby number (e.g. -1). This option is silently ignored if the -k option is notused.
      # 注释:-<signal> 指定信号
-u
Append the user name of the process ownerto each PID.
      # 注释:-u 表示在 PID 后面加上进程的 owner




-v
Verbose mode. Processes are shown in aps-like style. The fields PID, USER and COMMAND are similar to ps. ACCESS showshow the process accesses the file. If the access is by the kernel (e.g. in thecase of a mount point, a swap file, etc.), kernel is shown instead of the PID.
      # 注释:-v 表示 verbose 模式。进程以 ps 的方式显示,包括 PIDUSERCOMMANDACCESS 字段


      # 如果是内核访问的(例如 mount pointswap 文件),则显示为 kernel 而不是 pid
-V
Display version information.
-4
Search only for IPv4 sockets. This optionmust not be used with the -6 option and only has an effect with the tcp and udpnamespaces.
-6
Search only for IPv6 sockets. This optionmust not be used with the -4 option and only has an effect with the tcp and udpnamespaces.
-
Reset all options and set the signal backto SIGKILL.
      # 注释:‘-’ 表示重置所有选项并把信号设置为 SIGKILL
Files


/proc  location of the procfile system
Examples


fuser -km /home


  kills all processesaccessing the file system /home in any way.


    # 注释:fuser -km /home 杀死所有访问 /home 文件系统的进程
if fuser -s /dev/ttyS1; then :; elsesomething; fi


  invokes something if noother process is using /dev/ttyS1.


fuser telnet/tcp


  shows all processes atthe (local) TELNET port.
  http://blog.sina.com.cn/s/blog_59af21260100tv1m.html
  

fuser 概述
fuser命令是用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息。
例一:
  #fuser –m –u /mnt/usb1
/mnt/usb1: 1347c(root) 1348c(guido) 1349c(guido)
在例子一中,使用了-m和-u选项,用来查找所有正在使用/mnt/usb1的所有进程的PID已经该进程的OWNER,如1347c(root),其中1347是进程PID,root是该进程的OWNER。
fuser会显示正在使用指定的file,file system 或者 sockets的进程的PID。在默认的显示模式下,每个文件名之后会跟随一个字符,用来指示当前的访问类型。
如下所示:
c
current directory.
e
executable being run.
f
open file.
F
open file for writing.
r
root directory.
m
mmap'ed file or shared library
同时fuser 可以用来查找哪些进程正在使用指定的network port。
[root@bl25p-19 /]# fuser -v -n tcp 111
              USER    PID  ACCESS  COMMAND
111/tcp:       rpc    2848  F....     portmap
fuser 的返回值:
fuser如果没有找到任何进程正在使用指定的file, filesystem 或 socket, 或者在查找过程中发生了fatal error,则返回non-zero 值。
fuser如果找到至少一个进程正在使用指定的file, filesystem 或 socket,则返回zero。
fuser 常用场景
fuser通常被用在诊断系统的“resource busy”问题,通常是在你希望umount指定的挂载点得时候遇到。如果你希望kill所有正在使用某一指定的file, file system or sockets的进程的时候,你可以使用-k option。
fuser –k /path/to/your/filename
这时fuser会向所以正在使用/path/to/your/filename的进程发送SIGKILL。如果你希望在发送之前得到提示,可以使用-i 选项。
fuser –k –i /path/to/your/filename
fuser的其他有用的参数
-k
kills all processaccessing a file. For example fuser -k /path/to/your/filename kills all processes accessing thisdirectory without confirmation. Use -i for confirmation
-i
interactive mode. Promptbefore killing process
-v
verbose.
-u
append username
-a
display all files
-m
name specifies a file on a mounted filesystem or a block device that is mounted. All processes accessing files on thatfile system are listed. If a directory file is specified, it is automaticallychanged to name/. to use any file systemthat might be mounted on that directory.
  http://www.cnblogs.com/yuboyue/archive/2011/07/18/2109838.html
  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-480945-1-1.html 上篇帖子: linux shell shift 下篇帖子: Linux下安装vmware
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表