手邊剛好在處理一個大量 process 未結束於系統上的一個問題. 該 server 不知什麼原因, 在起動 cgi 程式時, 發生了這個現象, 偶發的不會自行結束, 於是在系統內就留下了許多該 process 的名稱. 利用 task manager 會看到大量的 process, 雖未佔用 cpu, 不過會吃掉一部分的記憶.
在不方便重開 server 的狀況下, 如何快速地將相同名稱的 process 一起刪除呢? 於是上網想查看看有沒有什麼方便的小工具可以做這件事. 結果很順利地找到系統內建的一個指令: taskkill
這個指令是給定要刪除的 pid 或 image name (就是執行檔名稱), 來進行 process kill 的動作. 簡單研究一下, 進行了測試, 效果相當不錯, 800多個 process 一下就刪完了, 使用的指令如下:
taskkill /IM mytest.exe /F
如此一來, 所有 mytest.exe 的 process 一口氣就被 kill 完成了.
另外有許多方便的參數可供使用, 詳情可以利用 taskkill /? 來取得說明, 列表如下:
TASKKILL [/S system [/U username [/P [password]]]]
{ [/FI filter] [/PID processid | /IM imagename] } [/T] [/F]Description:
This tool is used to terminate tasks by process id (PID) or image name.Parameter List:
/S system Specifies the remote system to connect to./U [domain\]user Specifies the user context under which the
command should execute./P [password] Specifies the password for the given user
context. Prompts for input if omitted./FI filter Applies a filter to select a set of tasks.
Allows “*” to be used. ex. imagename eq acme*/PID processid Specifies the PID of the process to be terminated.
Use TaskList to get the PID./IM imagename Specifies the image name of the process
to be terminated. Wildcard ‘*’ can be used
to specify all tasks or image names./T Terminates the specified process and any
child processes which were started by it./F Specifies to forcefully terminate the process(es).
/? Displays this help message.
Filters:
Filter Name Valid Operators Valid Value(s)
———– ————— ————————-
STATUS eq, ne RUNNING |
NOT RESPONDING | UNKNOWN
IMAGENAME eq, ne Image name
PID eq, ne, gt, lt, ge, le PID value
SESSION eq, ne, gt, lt, ge, le Session number.
CPUTIME eq, ne, gt, lt, ge, le CPU time in the format
of hh:mm:ss.
hh – hours,
mm – minutes, ss – seconds
MEMUSAGE eq, ne, gt, lt, ge, le Memory usage in KB
USERNAME eq, ne User name in [domain\]user
format
MODULES eq, ne DLL name
SERVICES eq, ne Service name
WINDOWTITLE eq, ne Window titleNOTE
—-
1) Wildcard ‘*’ for /IM switch is accepted only when a filter is applied.
2) Termination of remote processes will always be done forcefully (/F).
3) “WINDOWTITLE” and “STATUS” filters are not considered when a remote
machine is specified.Examples:
TASKKILL /IM notepad.exe
TASKKILL /PID 1230 /PID 1241 /PID 1253 /T
TASKKILL /F /IM cmd.exe /T
TASKKILL /F /FI “PID ge 1000” /FI “WINDOWTITLE ne untitle*”
TASKKILL /F /FI “USERNAME eq NT AUTHORITY\SYSTEM” /IM notepad.exe
TASKKILL /S system /U domain\username /FI “USERNAME ne NT*” /IM *
TASKKILL /S system /U username /P password /FI “IMAGENAME eq note*”
給大家參考!
參考資料: XP: kill a Windows process from the command line with taskkill