Linux-fuser命令詳解

Michael_DD發表於2014-12-05
Linux-fuser命令詳解


fuser:使用檔案或者套節字來表示識別程式。

Options

-a   # 註釋 :-a 顯示所有指定檔案的情況,即使沒有程式訪問它們。

-c
Same as -m option, used for POSIX compatibility.

-f
Silently ignored, used for POSIX compatibility.

-k
# 註釋 :-k 表示殺死訪問指定檔案的程式。預設傳送 SIGKILL (-9)訊號。fuser 程式永遠不會殺死自己。
# 但可能殺死其他 fuser 程式。在嘗試殺死程式前,fuser 會改變 euid 為 ruid

-i
# 註釋 :-i 表示互動模式,在殺死程式前會提示。必須和 -k 合用

-l
# 註釋 :-l 列出所有已知的訊號

-m
# 註釋:-m 表示指定檔案所在的檔案系統或者塊裝置(處於 mount 狀態)。所有訪問該檔案系統的程式都被列出。
# 假如指定了一個目錄,則自動在後面加上 '/'  表示該掛載在該目錄上的檔案系統

-n space
# 註釋 :-n 指定 sockets 的協議型別。可以是 file(預設)、udp、tcp 。
# 你甚至可以使用 '/' 的簡寫方式,只要不存在異義的話

-s
# 註釋 :-s 表示安靜模式。-u 和 -v 會被忽略,-a 不能和 -s 一起使用

-signal
# 註釋:- 指定訊號

-u
# 註釋 :-u 表示在 PID 後面加上程式的 owner
 
     
-v
# 註釋 :-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.

-
# 註釋:‘-’ 表示重置所有選項並把訊號設定為 SIGKILL




常用的兩個功能:檢視需要的程式和要殺死我查到的程式。

比如想umount光碟機或者其他檔案系統的的時候,結果系統提示裝置正在使用或者正忙,可是又找不到到底誰使用了。這個時候fuser可派上用場了。

[root@rac1 tmp]#  fuser -vm /usr/local/

                     USER        PID ACCESS COMMAND
/usr/local/:         root          1 .rce. init
                     root          2 .rc.. migration/0
                     root          3 .rc.. ksoftirqd/0
                     root          4 .rc.. migration/1
                     root          5 .rc.. ksoftirqd/1
                     root          6 .rc.. migration/2
                     root          7 .rc.. ksoftirqd/2
                     root          8 .rc.. migration/3
                     root          9 .rc.. ksoftirqd/3
                     root         10 .rc.. events/0
                     root         11 .rc.. events/1
                     root         12 .rc.. events/2
                     root         13 .rc.. events/3

在使用fuser -km 就可以殺死程式,同時也可以使用互動式的-i引數,一個一個殺死。
 

# 註釋 :fuser 顯示使用指定檔案或者檔案系統的程式的 pid ,預設會在每個 pid 後面加上一個字元表示訪問的方式 :
 
#     -)1、c :表示 current directory 。表示程式是在該目錄下啟動的。
 
#     -)2、e :表示是可執行的程式
 
#     -)3、f :開啟檔案。預設不顯示
 
#     -)4、r :root 目錄,表示該目錄是該程式的 / 目錄
 
#     -)5、m :表示 mmap 過的檔案或者共享庫


# 註釋 :如果指定檔案沒有任何程式訪問,或者出現嚴重錯誤,fuser 返回非 0 的 exit status。

# 如果至少存在一個程式,則 fuser 返回 0

# 註釋:為了查詢使用某個 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 addresses and port numbers.

# 註釋 ;在埠方面,你可以用埠名或者數字表示

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

# 註釋 :fuser 只輸出 PID 到 stdout ,其他都送給 stderr


舉例:

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.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1357511/,如需轉載,請註明出處,否則將追究法律責任。