linux之fuser命令

fuser:使用文件或者套節字來表示識別進程。我經常使用的他的兩個功能:查看我須要的進程和我要殺死我查到的進程。mysql

好比當你想umount光驅或者其餘文件系統的的時候,結果系統提示你設備正在使用或者正忙,但是你又找不到到底誰使用了他。這個時候fuser可派上用場了。sql

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

USER        PID ACCESS COMMAND

/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. httpdsocket

在使用fuser -km 就能夠殺死進程,同時也可使用交互式的-i參數,一個一個殺死。tcp

Descriptionui

fuser displays the PIDs 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:this

註釋 :fuser 顯示使用指定文件或者文件系統的進程的 pid ,默認會在每一個 pid 後面加上一個字符表示訪問的方式 :

-)一、c :表示 current directory 。表示進程是在該目錄下啓動的。

-)二、e :表示是可執行的程序

-)三、f :打開文件。默認不顯示

-)四、r :root 目錄,表示該目錄是該進程的 / 目錄

-)五、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 display mode. r root directory. m mmap'ed file or shared library. fuser returns a non-zero return code if none of the specified files is accessed or in case of a fatal error. If at least one access has been found, fuser returns zero.spa

註釋 :若是指定文件沒有任何進程訪問,或者出現嚴重錯誤,fuser 返回非 0 的 exit status。

若是至少存在一個進程,則 fuser 返回 0

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

註釋:爲了查找使用某個 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]]]orm

Either symbolic or numeric values can be used 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 command line. By default, only files 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, but may kill other fuser processes. The effective user ID of the process executing fuser 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 before killing 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 file system or a block device that is mounted. All processes accessing files on that file system are listed. If a directory file is specified, it is automatically changed to name/. to use any file system that might be mounted on that directory. # 註釋:-m 表示指定文件所在的文件系統或者塊設備(處於 mount 狀態)。全部訪問該文件系統的進程都被列出。

# 假如指定了一個目錄,則自動在後面加上 '/'  表示該掛載在該目錄上的文件系統

-n space Select a different name space. The name spaces file (file names, the default), udp (local UDP ports), and tcp (local TCP ports) are supported. For ports, either the port number or the symbolic name can be specified. If there is no ambiguity, the shortc ut notation 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 must not be used with -s. # 註釋 :-s 表示安靜模式。-u 和 -v 會被忽略,-a 不能和 -s 一塊兒使用 -signal Use the specified signal instead of SIGKILL when killing processes. Signals can be specified either by name (e.g. -HUP) or by number (e.g. -1). This option is silently ignored if the -k option 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. The fields PID, USER and COMMAND are similar to ps. ACCESS shows how the 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 shown instead 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 used with the -6 option and only has an effect with the tcp and udp namespaces. -6 Search only for IPv6 sockets. This option must not be used with the -4 option and only has an effect with the tcp and udp namespaces.

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 file system /home in any way.

    # 註釋 :fuser -km /home 殺死全部訪問 /home 文件系統的進程

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

invokes something if no other process is using /dev/ttyS1.

fuser telnet/tcp

shows all processes at the (local) TELNET port.
相關文章
相關標籤/搜索