
myName
我需要終止其描述中包含的進程。目前我正在做:
ps -ax |grep myName
I see PID
kill -9 PID
如何在不輸入 PID 的情況下用一條指令執行相同的操作?
答案1
如果myName
是您要終止的進程/執行檔的名稱,您可以使用:
pkill myName
pkill
預設發送SIGTERM
訊號(訊號15)。如果您想要SIGKILL
或訊號 9,請使用:
pkill -9 myName
如果myName
不是進程名稱,或例如另一個(長)指令的參數,則pkill
(或pgrep
) 可能無法如預期般運作。所以你需要使用該-f
選項。
從man kill
:
-f, --full
The pattern is normally only matched against the process name.
When -f is set, the full command line is used.
NOTES
The process name used for matching is limited to the 15 characters present
in the output of /proc/pid/stat. Use the -f option to match against the
complete command line, /proc/pid/cmdline.
所以:
pkill -f myName
或者
kill -9 $(pgrep -f myName)
答案2
使用指令名稱:
pkill -9 myscript
如果您正在命令列中查找字串:
kill -9 $(ps ax | grep myName | fgrep -v grep | awk '{ print $1 }')
我必須警告你:上面的命令可以SIGKILL
向多個進程發送一個訊號。
答案3
有兩個非常簡潔的命令pgrep
,pkill
允許輸入搜尋字詞或命令名稱的一部分,並且它將提供進程的 PID 或(如果是pkill
)殺死該進程。
$ pgrep -f firefox
23699
pkill
使用和標誌運行相同的命令-f
將關閉所有 Firefox 視窗。
專門需要該-f
標誌來搜尋進程的完整命令列。
答案4
for loop
您可以對與特定進程名稱關聯的所有 PID進行簡單處理,如下所示:
$ for i in $( ps ax | awk '/[m]yName/ {print $1}' ); do kill ${i}; done
這將殺死所有包含單字:的進程myName
。